You are here

public function EntityViewModeRestrictionByRegion::inlineBlocksAllowedinContext in Layout Builder Restrictions 8.2

Returns an array of allowed inline blocks in a given context.

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 An array of allowed inline block types.

Overrides LayoutBuilderRestrictionBase::inlineBlocksAllowedinContext

File

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

Class

EntityViewModeRestrictionByRegion
Controls behavior of the by region plugin.

Namespace

Drupal\layout_builder_restrictions_by_region\Plugin\LayoutBuilderRestriction

Code

public function inlineBlocksAllowedinContext(SectionStorageInterface $section_storage, $delta, $region) {
  $view_display = $this
    ->getValuefromSectionStorage([
    $section_storage,
  ], 'view_display');
  $third_party_settings = $view_display
    ->getThirdPartySetting('layout_builder_restrictions', 'entity_view_mode_restriction_by_region', []);
  $whitelisted_blocks = isset($third_party_settings['whitelisted_blocks']) ? $third_party_settings['whitelisted_blocks'] : [];
  $blacklisted_blocks = isset($third_party_settings['blacklisted_blocks']) ? $third_party_settings['blacklisted_blocks'] : [];
  $layout_id = $section_storage
    ->getSection($delta)
    ->getLayoutId();

  // If restriction behavior is for all regions, then overwrite
  // region with 'all_regions'.
  if (isset($third_party_settings['whitelisted_blocks'][$layout_id]['all_regions']) || isset($third_party_settings['blacklisted_blocks'][$layout_id]['all_regions']) || isset($third_party_settings['restricted_categories'][$layout_id]['all_regions'])) {
    $region = 'all_regions';
  }

  // Check if allowed inline blocks are defined in config.
  if (isset($whitelisted_blocks[$layout_id][$region]['Inline blocks'])) {
    return $whitelisted_blocks[$layout_id][$region]['Inline blocks'];
  }
  else {
    $inline_blocks = $this
      ->getInlineBlockPlugins();
    if (isset($blacklisted_blocks[$layout_id][$region]['Inline blocks'])) {
      foreach ($inline_blocks as $key => $block) {

        // Unset explicitly blacklisted inline blocks.
        if (in_array($block, $blacklisted_blocks[$layout_id][$region]['Inline blocks'])) {
          unset($inline_blocks[$key]);
        }
      }
    }
    return $inline_blocks;
  }
}