Commit 97154481 authored by Eric Bremner's avatar Eric Bremner
Browse files

ISTWCMS-4050: combining the restriction of blocks and layouts into one class for dashboards

parent 9d67076a
<?php
namespace Drupal\uw_dashboard\Plugin\LayoutBuilderRestriction;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\layout_builder_restrictions\Plugin\LayoutBuilderRestriction\EntityViewModeRestriction;
use Drupal\layout_builder_restrictions\Traits\PluginHelperTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* EntityViewModeRestriction Plugin.
*
* @LayoutBuilderRestriction(
* id = "uw_dashboard_allowed_layouts",
* title = @Translation("UW Dashboard allowed layouts"),
* description = @Translation("Restrict layouts for UW dashboard"),
* )
*/
class UwDashboardAllowedLayouts extends EntityViewModeRestriction {
/**
* {@inheritdoc}
*/
public function alterSectionDefinitions(array $definitions, array $context) {
// If we are on the dashboards, restrict the layouts.
if ($context['section_storage']->pluginId == 'dashboards') {
// The allowed layouts machine names.
$allowed_layouts = ['layout_1', 'layout_2', 'layout_3'];
// Step through each of the definitions and unset anything
// that is not in the allowed layouts array.
foreach ($definitions as $key => $definition) {
// If it is not in the allowed layout array unset the layout.
if (!in_array($key, $allowed_layouts)) {
// Unset the layout.
unset($definitions[$key]);
}
}
}
return $definitions;
}
}
<?php
namespace Drupal\uw_dashboard\Plugin\LayoutBuilderRestriction;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\layout_builder_restrictions\Plugin\LayoutBuilderRestrictionBase;
use Drupal\layout_builder_restrictions\Plugin\LayoutBuilderRestriction\EntityViewModeRestriction;
use Drupal\layout_builder_restrictions\Traits\PluginHelperTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* UW Dashboard Block Restriction Plugin.
* UwDashboardRestrictBlocksLayouts class.
*
* @LayoutBuilderRestriction(
* id = "uw_dashboard_block_restriction",
* title = @Translation("UW Dashboard Block Restriction"),
* description = @Translation("Restrict blocks/layouts for UW Dashboard"),
* id = "uw_dashboard_restrict_blocks_layouts",
* title = @Translation("UW Dashboard restrict blocks/layouts"),
* description = @Translation("Restrict blocks/layouts for UW dashboard"),
* )
*/
class AllowedBlocks extends LayoutBuilderRestrictionBase {
class UwDashboardRestrictBlocksLayouts extends EntityViewModeRestriction {
use PluginHelperTrait;
......@@ -70,6 +68,7 @@ class AllowedBlocks extends LayoutBuilderRestrictionBase {
* {@inheritDoc}
*/
public function alterBlockDefinitions(array $definitions, array $context) {
// Load configuration for allowed blocks.
$allowed_blocks = $this->configFactory->get('uw_dashboard.allowed_blocks')
->get('allowed_blocks');
......@@ -88,4 +87,30 @@ class AllowedBlocks extends LayoutBuilderRestrictionBase {
return $allowed_block_definitions ?: $definitions;
}
/**
* {@inheritdoc}
*/
public function alterSectionDefinitions(array $definitions, array $context) {
// If we are on the dashboards, restrict the layouts.
if ($context['section_storage']->pluginId == 'dashboards' || $context['section_storage']->pluginId == 'dashboards_override') {
// The allowed layouts machine names.
$allowed_layouts = ['layout_1', 'layout_2', 'layout_3'];
// Step through each of the definitions and unset anything
// that is not in the allowed layouts array.
foreach ($definitions as $key => $definition) {
// If it is not in the allowed layout array unset the layout.
if (!in_array($key, $allowed_layouts)) {
// Unset the layout.
unset($definitions[$key]);
}
}
}
return $definitions;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment