You are here

public function EntityViewModeRestriction::alterSectionDefinitions in Layout Builder Restrictions 8.2

Alter the section definitions.

Overrides LayoutBuilderRestrictionBase::alterSectionDefinitions

File

src/Plugin/LayoutBuilderRestriction/EntityViewModeRestriction.php, line 147

Class

EntityViewModeRestriction
Controls behavior of the per-view mode plugin.

Namespace

Drupal\layout_builder_restrictions\Plugin\LayoutBuilderRestriction

Code

public function alterSectionDefinitions(array $definitions, array $context) {

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

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