View source
<?php
namespace Drupal\layout_builder_restrictions_by_region\Plugin\LayoutBuilderRestriction;
use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Database\Connection;
use Drupal\layout_builder_restrictions\Plugin\LayoutBuilderRestrictionBase;
use Drupal\layout_builder\OverridesSectionStorageInterface;
use Drupal\layout_builder\SectionStorageInterface;
use Drupal\layout_builder_restrictions\Traits\PluginHelperTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityViewModeRestrictionByRegion extends LayoutBuilderRestrictionBase {
use PluginHelperTrait;
protected $moduleHandler;
protected $database;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, Connection $connection) {
$this->configuration = $configuration;
$this->pluginId = $plugin_id;
$this->pluginDefinition = $plugin_definition;
$this->moduleHandler = $module_handler;
$this->database = $connection;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('module_handler'), $container
->get('database'));
}
public function alterBlockDefinitions(array $definitions, array $context) {
if (!isset($context['delta'])) {
return $definitions;
}
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)) {
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 (isset($whitelisted_blocks['all_regions']) || isset($blacklisted_blocks['all_regions']) || isset($restricted_categories['all_regions'])) {
$region = 'all_regions';
}
$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']);
if ($definition['provider'] == 'block_content') {
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])) {
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])) {
unset($definitions[$original_delta]);
}
}
elseif ($this
->categoryIsRestricted($category, $allowed_block_categories)) {
unset($definitions[$original_delta]);
}
}
}
}
}
return $definitions;
}
public function alterSectionDefinitions(array $definitions, array $context) {
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', []);
$allowed_layouts = isset($third_party_settings['allowed_layouts']) ? $third_party_settings['allowed_layouts'] : [];
if (!empty($allowed_layouts)) {
$definitions = array_intersect_key($definitions, array_flip($allowed_layouts));
}
}
}
return $definitions;
}
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)) {
return TRUE;
}
$has_restrictions = FALSE;
$bundle = $this
->getValuefromSectionStorage([
$section_storage,
], 'bundle');
$section_from = $section_storage
->getSection($delta_from);
$region = $section_storage
->getSection($delta_to);
$layout_id = $region
->getLayoutId();
$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 (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';
}
$component = $section_from
->getComponent($block_uuid)
->toArray();
$block_id = $component['configuration']['id'];
$block_id_parts = explode(':', $block_id);
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])) {
$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") {
$has_restrictions = FALSE;
}
else {
if (isset($whitelisted_blocks[$region_to][$category])) {
if (in_array($block_id, $whitelisted_blocks[$region_to][$category])) {
$has_restrictions = FALSE;
}
}
elseif (isset($blacklisted_blocks[$region_to][$category])) {
if (!in_array($block_id, $blacklisted_blocks[$region_to][$category])) {
$has_restrictions = FALSE;
}
}
}
if (!empty($whitelisted_blocks[$region_to]['Custom block types'])) {
$content_block_types_by_uuid = $this
->getBlockTypeByUuid();
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'])) {
$has_restrictions = FALSE;
}
else {
$has_restrictions = TRUE;
}
}
}
elseif (!empty($blacklisted_blocks[$region_to]['Custom block types'])) {
$content_block_types_by_uuid = $this
->getBlockTypeByUuid();
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'])) {
$has_restrictions = TRUE;
}
else {
$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,
]);
}
}
return TRUE;
}
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 (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';
}
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) {
if (in_array($block, $blacklisted_blocks[$layout_id][$region]['Inline blocks'])) {
unset($inline_blocks[$key]);
}
}
}
return $inline_blocks;
}
}
private function getBlockTypeByUuid() {
if ($this->moduleHandler
->moduleExists('block_content')) {
$query = $this->database
->select('block_content', 'b')
->fields('b', [
'uuid',
'type',
]);
$results = $query
->execute();
return $results
->fetchAllKeyed(0, 1);
}
return [];
}
}