You are here

public function ChooseBlockController::inlineBlockList in Layout Builder Restrictions 8.2

Provides the UI for choosing a new inline block.

Parameters

\Drupal\layout_builder\SectionStorageInterface $section_storage: The section storage.

int $delta: The delta of the section to splice.

string $region: The region the block is going in.

Return value

array A render array.

Overrides ChooseBlockController::inlineBlockList

File

src/Controller/ChooseBlockController.php, line 41

Class

ChooseBlockController
Defines a controller to choose a new block.

Namespace

Drupal\layout_builder_restrictions\Controller

Code

public function inlineBlockList(SectionStorageInterface $section_storage, $delta, $region) {
  $build = parent::inlineBlockList($section_storage, $delta, $region);

  // Retrieve defined Layout Builder Restrictions plugins.
  $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);

    // Loop through links and remove those for disallowed inline block types.
    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;
}