ChooseBlockController.php in Layout Builder Restrictions 8.2
File
src/Controller/ChooseBlockController.php
View source
<?php
namespace Drupal\layout_builder_restrictions\Controller;
use Drupal\layout_builder\Controller\ChooseBlockController as ChooseBlockControllerCore;
use Drupal\layout_builder\SectionStorageInterface;
class ChooseBlockController extends ChooseBlockControllerCore {
public function build(SectionStorageInterface $section_storage, $delta, $region) {
$build = parent::build($section_storage, $delta, $region);
$layout_builder_restrictions_manager = \Drupal::service('plugin.manager.layout_builder_restriction');
$restriction_plugins = $layout_builder_restrictions_manager
->getSortedPlugins();
foreach (array_keys($restriction_plugins) as $id) {
$plugin = $layout_builder_restrictions_manager
->createInstance($id);
$allowed_inline_blocks = $plugin
->inlineBlocksAllowedinContext($section_storage, $delta, $region);
if (empty($allowed_inline_blocks)) {
unset($build['add_block']);
}
}
return $build;
}
public function inlineBlockList(SectionStorageInterface $section_storage, $delta, $region) {
$build = parent::inlineBlockList($section_storage, $delta, $region);
$layout_builder_restrictions_manager = \Drupal::service('plugin.manager.layout_builder_restriction');
$restriction_plugins = $layout_builder_restrictions_manager
->getSortedPlugins();
foreach (array_keys($restriction_plugins) as $id) {
$plugin = $layout_builder_restrictions_manager
->createInstance($id);
$allowed_inline_blocks = $plugin
->inlineBlocksAllowedinContext($section_storage, $delta, $region);
foreach ($build['links']['#links'] as $key => $link) {
$route_parameters = $link['url']
->getRouteParameters();
if (!in_array($route_parameters['plugin_id'], $allowed_inline_blocks)) {
unset($build['links']['#links'][$key]);
}
}
}
return $build;
}
}