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 alterBlockDefinitions(array $definitions, array $context) { // Load configuration for allowed blocks. $allowed_blocks = $this->configFactory->get('uw_dashboard.allowed_blocks') ->get('allowed_blocks'); $allowed_block_definitions = []; // Filter blocks only for dashboard override, while for admin dashboard // layout page will display all available blocks. if ($allowed_blocks && isset($context['section_storage']) && $context['section_storage']->getPluginId() === 'dashboards_override') { foreach ($allowed_blocks as $key => $allowed_block) { if (isset($definitions[$key])) { $allowed_block_definitions[$key] = $definitions[$key]; } } } 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 = [ 'uw_1_column', 'uw_2_column', 'uw_3_column', 'uw_inverted_l_left', 'uw_inverted_l_right', ]; // 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; } }