You are here

public function EntityViewModeRestrictionByRegion::alterSectionDefinitions in Layout Builder Restrictions 8.2

Alter the section definitions.

Overrides LayoutBuilderRestrictionBase::alterSectionDefinitions

File

modules/layout_builder_restrictions_by_region/src/Plugin/LayoutBuilderRestriction/EntityViewModeRestrictionByRegion.php, line 160

Class

EntityViewModeRestrictionByRegion
Controls behavior of the by region plugin.

Namespace

Drupal\layout_builder_restrictions_by_region\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_by_region', []);
      $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;
}