public function EntityViewModeRestrictionByRegion::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
- modules/
layout_builder_restrictions_by_region/ src/ Plugin/ LayoutBuilderRestriction/ EntityViewModeRestrictionByRegion.php, line 179
Class
- EntityViewModeRestrictionByRegion
- Controls behavior of the by region plugin.
Namespace
Drupal\layout_builder_restrictions_by_region\Plugin\LayoutBuilderRestrictionCode
public function blockAllowedinContext(SectionStorageInterface $section_storage, $delta_from, $delta_to, $region_to, $block_uuid, $preceding_block_uuid = NULL) {
$has_restrictions = FALSE;
$view_display = $this
->getValuefromSectionStorage([
$section_storage,
], 'view_display');
$third_party_settings = $view_display
->getThirdPartySetting('layout_builder_restrictions', 'entity_view_mode_restriction_by_region', []);
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;
$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.
$region = $section_storage
->getSection($delta_to);
$layout_id = $region
->getLayoutId();
// Get region restrictions.
$whitelisted_blocks = isset($third_party_settings['whitelisted_blocks'][$layout_id]) ? $third_party_settings['whitelisted_blocks'][$layout_id] : [];
$blacklisted_blocks = isset($third_party_settings['blacklisted_blocks'][$layout_id]) ? $third_party_settings['blacklisted_blocks'][$layout_id] : [];
$restricted_categories = isset($third_party_settings['restricted_categories'][$layout_id]) ? $third_party_settings['restricted_categories'][$layout_id] : [];
// If restriction applies to all regions, then overwrite region_to
// to '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_to = 'all_regions';
}
// 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[$region_to][$category]) || isset($blacklisted_blocks[$region_to][$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[$region_to]) && in_array($category, array_values($restricted_categories[$region_to]))) {
$has_restrictions = TRUE;
}
elseif (!isset($restricted_categories[$region_to][$category]) && !isset($blacklisted_blocks[$region_to][$category]) && !isset($whitelisted_blocks[$region_to][$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[$region_to][$category])) {
// An explicitly whitelisted block means it's allowed.
if (in_array($block_id, $whitelisted_blocks[$region_to][$category])) {
$has_restrictions = FALSE;
}
}
elseif (isset($blacklisted_blocks[$region_to][$category])) {
// If absent from the blacklist, it's allowed.
if (!in_array($block_id, $blacklisted_blocks[$region_to][$category])) {
$has_restrictions = FALSE;
}
}
}
// Edge case: if block *type* restrictions are present...
if (!empty($whitelisted_blocks[$region_to]['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[$region_to]['Custom blocks'])) {
$block_bundle = $content_block_types_by_uuid[end($block_id_parts)];
if (in_array($block_bundle, $whitelisted_blocks[$region_to]['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[$region_to]['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[$region_to]['Custom blocks'])) {
$block_bundle = $content_block_types_by_uuid[end($block_id_parts)];
if (in_array($block_bundle, $blacklisted_blocks[$region_to]['Custom block types'])) {
// There are block type restrictions AND
// this block type has been blacklostlisted.
$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,
"%region" => $region_to,
"%type" => $bundle,
]);
}
}
// Default: this block is not restricted.
return TRUE;
}