You are here

public function EntityViewModeRestriction::blockAllowedinContext in Layout Builder Restrictions 8.2

Determine whether the block being moved is allowed to the destination.

Parameters

\Drupal\layout_builder\SectionStorageInterface $section_storage: The section storage.

int $delta_from: The delta of the original section.

int $delta_to: The delta of the destination section.

string $region_to: The new region for this block.

string $block_uuid: The UUID for this block.

string|null $preceding_block_uuid: (optional) If provided, the UUID of the block to insert this block after.

Return value

mixed TRUE if the block is allowed, or a string error message explaining the restriction.

Overrides LayoutBuilderRestrictionBase::blockAllowedinContext

File

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

Class

EntityViewModeRestriction
Controls behavior of the per-view mode plugin.

Namespace

Drupal\layout_builder_restrictions\Plugin\LayoutBuilderRestriction

Code

public function blockAllowedinContext(SectionStorageInterface $section_storage, $delta_from, $delta_to, $region_to, $block_uuid, $preceding_block_uuid = NULL) {
  $view_display = $this
    ->getValuefromSectionStorage([
    $section_storage,
  ], 'view_display');
  $third_party_settings = $view_display
    ->getThirdPartySetting('layout_builder_restrictions', 'entity_view_mode_restriction', []);
  if (empty($third_party_settings)) {

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

  // There ARE restrictions. Start by assuming *this* block is not restricted.
  $has_restrictions = FALSE;
  $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'] : [];
  $bundle = $this
    ->getValuefromSectionStorage([
    $section_storage,
  ], 'bundle');

  // Get "from" section and layout id. (not needed?)
  $section_from = $section_storage
    ->getSection($delta_from);

  // Get "to" section and layout id.
  $section_to = $section_storage
    ->getSection($delta_to);
  $layout_id_to = $section_to
    ->getLayoutId();

  // Get block information.
  $component = $section_from
    ->getComponent($block_uuid)
    ->toArray();
  $block_id = $component['configuration']['id'];
  $block_id_parts = explode(':', $block_id);

  // Load the plugin definition.
  if ($definition = $this
    ->blockManager()
    ->getDefinition($block_id)) {
    $category = $this
      ->getUntranslatedCategory($definition['category']);
    if (isset($whitelisted_blocks[$category]) || isset($blacklisted_blocks[$category]) || isset($restricted_categories[$category])) {

      // If there is a restriction, assume this block is restricted.
      // If the block is whitelisted or NOT blacklisted,
      // the restriction will be removed, below.
      $has_restrictions = TRUE;
    }
    if (!isset($restricted_categories[$category]) && !isset($whitelisted_blocks[$category]) && !isset($blacklisted_blocks[$category]) && $category != "Custom blocks") {

      // No restrictions have been placed on this category.
      $has_restrictions = FALSE;
    }
    else {

      // Some type of restriction has been placed.
      if (isset($whitelisted_blocks[$category])) {

        // An explicitly whitelisted block means it's allowed.
        if (in_array($block_id, $whitelisted_blocks[$category])) {
          $has_restrictions = FALSE;
        }
      }
      elseif (isset($blacklisted_blocks[$category])) {

        // If absent from the blacklist, it's allowed.
        if (!in_array($block_id, $blacklisted_blocks[$category])) {
          $has_restrictions = FALSE;
        }
      }
    }

    // Edge case: if block *type* restrictions are present...
    if (!empty($whitelisted_blocks['Custom block types'])) {
      $content_block_types_by_uuid = $this
        ->getBlockTypeByUuid();

      // If no specific custom block restrictions are set
      // check block type restrict by block type.
      if ($category == 'Custom blocks' && !isset($whitelisted_blocks['Custom blocks'])) {
        $block_bundle = $content_block_types_by_uuid[end($block_id_parts)];
        if (in_array($block_bundle, $whitelisted_blocks['Custom block types'])) {

          // There are block type restrictions AND
          // this block type has been whitelisted.
          $has_restrictions = FALSE;
        }
        else {

          // There are block type restrictions BUT
          // this block type has NOT been whitelisted.
          $has_restrictions = TRUE;
        }
      }
    }
    elseif (!empty($blacklisted_blocks['Custom block types'])) {
      $content_block_types_by_uuid = $this
        ->getBlockTypeByUuid();

      // If no specific custom block restrictions are set
      // check block type restrict by block type.
      if ($category == 'Custom blocks' && !isset($blacklisted_blocks['Custom blocks'])) {
        $block_bundle = $content_block_types_by_uuid[end($block_id_parts)];
        if (in_array($block_bundle, $blacklisted_blocks['Custom block types'])) {

          // There are block type restrictions AND
          // this block type has been blacklisted.
          $has_restrictions = TRUE;
        }
        else {

          // There are block type restrictions BUT
          // this block type has NOT been blacklisted.
          $has_restrictions = FALSE;
        }
      }
    }
    if ($has_restrictions) {
      return $this
        ->t("There is a restriction on %block placement in the %layout %region region for %type content.", [
        "%block" => $definition['admin_label'],
        "%layout" => $layout_id_to,
        "%region" => $region_to,
        "%type" => $bundle,
      ]);
    }
  }

  // Default: this block is not restricted.
  return TRUE;
}