public function EntityViewModeRestrictionByRegion::alterBlockDefinitions in Layout Builder Restrictions 8.2
Alter the block definitions.
Overrides LayoutBuilderRestrictionBase::alterBlockDefinitions
File
- modules/
layout_builder_restrictions_by_region/ src/ Plugin/ LayoutBuilderRestriction/ EntityViewModeRestrictionByRegion.php, line 79
Class
- EntityViewModeRestrictionByRegion
- Controls behavior of the by region plugin.
Namespace
Drupal\layout_builder_restrictions_by_region\Plugin\LayoutBuilderRestrictionCode
public function alterBlockDefinitions(array $definitions, array $context) {
// If this method is being called by any action other than 'Add block',
// then do nothing.
// @TODO: Re-assess after https://www.drupal.org/node/3099121
// has been addressed.
if (!isset($context['delta'])) {
return $definitions;
}
// 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_by_region', []);
if (empty($third_party_settings)) {
// This entity has no restrictions. Look no further.
return $definitions;
}
$layout_id = $context['section_storage']
->getSection($context['delta'])
->getLayoutId();
$region = $context['region'];
$allowed_block_categories = $default
->getThirdPartySetting('layout_builder_restrictions', 'allowed_block_categories', []);
$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 'all_regions'.
if (isset($whitelisted_blocks['all_regions']) || isset($blacklisted_blocks['all_regions']) || isset($restricted_categories['all_regions'])) {
$region = 'all_regions';
}
// Filter blocks from entity-specific SectionStorage (i.e., UI).
$content_block_types_by_uuid = $this
->getBlockTypeByUuid();
if (!empty($whitelisted_blocks) || !empty($blacklisted_blocks) || !empty($restricted_categories)) {
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[$region]['Custom blocks']) || isset($blacklisted_blocks[$region]['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 (isset($restricted_categories[$region]) && in_array($category, $restricted_categories[$region])) {
unset($definitions[$original_delta]);
}
elseif (isset($whitelisted_blocks[$region]) && in_array($category, array_keys($whitelisted_blocks[$region]))) {
if (!in_array($delta, $whitelisted_blocks[$region][$category])) {
// The current block is not whitelisted. Remove it.
unset($definitions[$original_delta]);
}
}
elseif (isset($blacklisted_blocks[$region]) && in_array($category, array_keys($blacklisted_blocks[$region]))) {
if (in_array($delta, $blacklisted_blocks[$region][$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;
}