You are here

protected function LayoutBuilderRestrictionsSubscriber::alterLayoutChooser in Layout Builder Restrictions 8

Alters the layouts available in Layout Builder's choose_section.

1 call to LayoutBuilderRestrictionsSubscriber::alterLayoutChooser()
LayoutBuilderRestrictionsSubscriber::onKernelView in src/EventSubscriber/LayoutBuilderRestrictionsSubscriber.php
Subscriber for kernel view.

File

src/EventSubscriber/LayoutBuilderRestrictionsSubscriber.php, line 63

Class

LayoutBuilderRestrictionsSubscriber
Layout builder restrictions event subscriber.

Namespace

Drupal\layout_builder_restrictions\EventSubscriber

Code

protected function alterLayoutChooser(&$result) {
  if (empty($result['layouts']['#items'])) {
    return;
  }
  if (interface_exists('Drupal\\Core\\Plugin\\FilteredPluginManagerInterface')) {

    // This will now be altered through the appropriate new hooks.
    return;
  }
  $keys = $this->moduleHandler
    ->invokeAll('layout_builder_restrictions_allowed_layouts');
  $this->moduleHandler
    ->alter('layout_builder_restrictions_allowed_layouts', $keys);
  if (!empty($keys)) {
    foreach ($result['layouts']['#items'] as $delta => $item) {

      /** @var \Drupal\Core\Url $url */
      $url = $item['#url'];
      $params = $url
        ->getRouteParameters();
      if (!in_array($params['plugin_id'], $keys)) {
        unset($result['layouts']['#items'][$delta]);
      }
    }
  }
}