You are here

public function EntityViewModeRestriction::alterBlockDefinitions in Layout Builder Restrictions 8.2

Alter the block definitions.

Overrides LayoutBuilderRestrictionBase::alterBlockDefinitions

File

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

Class

EntityViewModeRestriction
Controls behavior of the per-view mode plugin.

Namespace

Drupal\layout_builder_restrictions\Plugin\LayoutBuilderRestriction

Code

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

  // Respect restrictions on allowed blocks specified by the 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_block_categories = $default
        ->getThirdPartySetting('layout_builder_restrictions', 'allowed_block_categories', []);
      $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'] : [];
      $restricted_categories = isset($third_party_settings['restricted_categories']) ? $third_party_settings['restricted_categories'] : [];
    }
    else {
      $whitelisted_blocks = [];
      $blacklisted_blocks = [];
      $restricted_categories = [];
    }
    if (empty($third_party_settings)) {

      // This entity has no restrictions. Look no further.
      return $definitions;
    }

    // Filter blocks from entity-specific SectionStorage (i.e., UI).
    $content_block_types_by_uuid = $this
      ->getBlockTypeByUuid();
    foreach ($definitions as $delta => $definition) {
      $original_delta = $delta;
      $category = $this
        ->getUntranslatedCategory($definition['category']);

      // Custom blocks get special treatment.
      if ($definition['provider'] == 'block_content') {

        // 'Custom block types' are disregarded if 'Custom blocks'
        // restrictions are enabled.
        if (isset($whitelisted_blocks['Custom blocks']) || isset($blacklisted_blocks['Custom blocks'])) {
          $category = 'Custom blocks';
        }
        else {
          $category = 'Custom block types';
          $delta_exploded = explode(':', $delta);
          $uuid = $delta_exploded[1];
          $delta = $content_block_types_by_uuid[$uuid];
        }
      }
      if (in_array($category, $restricted_categories)) {
        unset($definitions[$original_delta]);
      }
      elseif (in_array($category, array_keys($whitelisted_blocks))) {
        if (!in_array($delta, $whitelisted_blocks[$category])) {

          // The current block is not whitelisted. Remove it.
          unset($definitions[$original_delta]);
        }
      }
      elseif (in_array($category, array_keys($blacklisted_blocks))) {
        if (in_array($delta, $blacklisted_blocks[$category])) {

          // The current block is blacklisted. Remove it.
          unset($definitions[$original_delta]);
        }
      }
      elseif ($this
        ->categoryIsRestricted($category, $allowed_block_categories)) {
        unset($definitions[$original_delta]);
      }
    }
  }
  return $definitions;
}