Commit 1c7c8ae7 authored by Eric Bremner's avatar Eric Bremner
Browse files

ISTWCMS-4050: restricting the layouts allowed in dashboards

parent 3e0a871e
<?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 {
use PluginHelperTrait;
/** @var \Drupal\Core\Config\ConfigFactoryInterface */
protected $configFactory;
/**
* @inheritDoc
*/
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition
) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('config.factory')
);
}
/**
* Constructs a Drupal\Component\Plugin\PluginBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config factory to load blocks allowed to be displayed on dashboard.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
ConfigFactoryInterface $configFactory
) {
$this->configuration = $configuration;
$this->pluginId = $plugin_id;
$this->pluginDefinition = $plugin_definition;
$this->configFactory = $configFactory;
}
/**
* {@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;
}
}
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