Commit 5f3cd8c3 authored by Igor Biki's avatar Igor Biki
Browse files

ISTWCMS-3908: Adding new plugin to handle block restrictions for dashboard.

parent 1e55205c
<?php
namespace Drupal\uw_dashboard\Plugin\LayoutBuilderRestriction;
use Drupal\Core\Database\Connection;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\layout_builder\OverridesSectionStorageInterface;
use Drupal\layout_builder_restrictions\Plugin\LayoutBuilderRestrictionBase;
use Drupal\layout_builder_restrictions\Traits\PluginHelperTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* UW Dashboard Block Restriction Plugin.
*
* @LayoutBuilderRestriction(
* id = "uw_dashboard_block_restriction",
* title = @Translation("UW Dashboard Block Restriction"),
* description = @Translation("Restrict blocks/layouts for UW Dashboard"),
* )
*/
class UWDashboardBlockRestriction extends LayoutBuilderRestrictionBase {
use PluginHelperTrait;
/**
* Module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Database connection service.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* @inheritDoc
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('module_handler'),
$container->get('database')
);
}
/**
* 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\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, Connection $connection) {
$this->configuration = $configuration;
$this->pluginId = $plugin_id;
$this->pluginDefinition = $plugin_definition;
$this->moduleHandler = $module_handler;
$this->database = $connection;
}
/**
* {@inheritDoc}
*/
public function alterBlockDefinitions(array $definitions, array $context) {
// Respect restrictions on allowed blocks specified by the section storage.
if (isset($context['section_storage'])) {
$default = $context['section_storage'];
if ($default->getPluginId() === 'dashboards') {
foreach ($definitions as $key => $single_definition) {
if (strpos($key, 'uw') === FALSE) {
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