You are here

function layout_builder_restrictions_plugin_filter_layout__layout_builder_alter in Layout Builder Restrictions 8

Same name and namespace in other branches
  1. 8.2 layout_builder_restrictions.module \layout_builder_restrictions_plugin_filter_layout__layout_builder_alter()

Implements hook_plugin_filter_TYPE__CONSUMER_alter().

Curate the layouts available in the Layout Builder "Add Section" UI.

File

./layout_builder_restrictions.module, line 63
Module file for layout builder restrictions.

Code

function layout_builder_restrictions_plugin_filter_layout__layout_builder_alter(array &$definitions, array $extra) {

  // Invoke deprecated API hook for restricting layouts. @todo: remove.
  $keys = \Drupal::moduleHandler()
    ->invokeAll('layout_builder_restrictions_allowed_layouts');
  \Drupal::moduleHandler()
    ->alter('layout_builder_restrictions_allowed_layouts', $keys);
  if (!empty($keys)) {
    @trigger_error('hook_layout_builder_restrictions_allowed_layouts and hook_layout_builder_restrictions_allowed_layouts_alter is deprecated together with Drupal 8.6.0. Please use hook_plugin_filter_TYPE__CONSUMER_alter and more specifically hook_plugin_filter_layout__layout_builder_alter instead.', E_USER_DEPRECATED);
    foreach ($definitions as $delta => $definition) {
      if (!in_array($delta, $keys)) {
        unset($definitions[$delta]);
      }
    }
  }

  // Allow hook implementations to audit available layouts.
  \Drupal::moduleHandler()
    ->alter('layout_builder_restrictions_allowed_layouts', $definitions);

  // Respect restrictions on allowed layouts specified by section storage.
  if (isset($extra['section_storage'])) {
    $default = $extra['section_storage'] instanceof OverridesSectionStorageInterface ? $extra['section_storage']
      ->getDefaultSectionStorage() : $extra['section_storage'];
    if ($default instanceof ThirdPartySettingsInterface) {
      $allowed_layouts = $default
        ->getThirdPartySetting('layout_builder_restrictions', 'allowed_layouts', []);

      // Filter blocks from entity-specific SectionStorage (i.e., UI).
      if (!empty($allowed_layouts)) {
        $definitions = array_intersect_key($definitions, array_flip($allowed_layouts));
      }
    }
  }
}