public function FormAlter::entityFormEntityBuild in Layout Builder Restrictions 8.2
Same name in this branch
- 8.2 src/Form/FormAlter.php \Drupal\layout_builder_restrictions\Form\FormAlter::entityFormEntityBuild()
- 8.2 modules/layout_builder_restrictions_by_region/src/Form/FormAlter.php \Drupal\layout_builder_restrictions_by_region\Form\FormAlter::entityFormEntityBuild()
Save allowed blocks & layouts for the given entity view mode.
File
- modules/
layout_builder_restrictions_by_region/ src/ Form/ FormAlter.php, line 327
Class
- FormAlter
- Supplement form UI to add setting for which blocks & layouts are available.
Namespace
Drupal\layout_builder_restrictions_by_region\FormCode
public function entityFormEntityBuild($entity_type_id, LayoutEntityDisplayInterface $display, &$form, FormStateInterface &$form_state) {
$static_id = $form_state
->getTemporaryValue('static_id');
// @todo: change naming to avoid color-based metaphor.
$restriction_types = [
'whitelisted',
'blacklisted',
];
// Set allowed layouts.
$layout_restriction = $form_state
->getValue([
'layout_builder_restrictions',
'allowed_layouts',
'layout_restriction',
]);
$allowed_layouts = [];
if ($layout_restriction == 'restricted') {
$allowed_layouts = array_keys(array_filter($form_state
->getValue([
'layout_builder_restrictions',
'allowed_layouts',
'layouts',
])));
}
$third_party_settings = $display
->getThirdPartySetting('layout_builder_restrictions', 'entity_view_mode_restriction_by_region');
$third_party_settings['allowed_layouts'] = $allowed_layouts;
// Set allowed blocks.
$tempstore = $this->privateTempStoreFactory;
$store = $tempstore
->get('layout_builder_restrictions_by_region');
$layout_definitions = $this
->getLayoutDefinitions();
foreach ($allowed_layouts as $section) {
$layout_definition = $layout_definitions[$section];
$regions = $layout_definition
->getRegions();
$regions['all_regions'] = [
'label' => $this
->t('All regions'),
];
// Set allowed layouts.
$layout_behavior = $form_state
->getValue([
'layout_builder_restrictions',
'allowed_blocks_by_layout',
$section,
]);
// Handle scenario where all_regions configuration has not been modified
// and needs to be preserved.
$all_regions_temp = $store
->get($static_id . ':' . $section . ':all_regions');
if ($layout_behavior['restriction_behavior'] == 'all' && is_null($all_regions_temp)) {
if (isset($third_party_settings['whitelisted_blocks'][$section]['all_regions'])) {
$all_regions_whitelisted = $third_party_settings['whitelisted_blocks'][$section]['all_regions'];
}
if (isset($third_party_settings['blacklisted_blocks'][$section]['all_regions'])) {
$all_regions_blacklisted = $third_party_settings['blacklisted_blocks'][$section]['all_regions'];
}
if (isset($third_party_settings['restricted_categories'][$section]['all_regions'])) {
$all_regions_restricted_categories = $third_party_settings['restricted_categories'][$section]['all_regions'];
}
foreach ($restriction_types as $logic_type) {
unset($third_party_settings[$logic_type . '_blocks'][$section]);
}
unset($third_party_settings['restricted_categories'][$section]);
if (isset($all_regions_whitelisted)) {
$third_party_settings['whitelisted_blocks'][$section]['all_regions'] = $all_regions_whitelisted;
}
if (isset($all_regions_blacklisted)) {
$third_party_settings['blacklisted_blocks'][$section]['all_regions'] = $all_regions_blacklisted;
}
if (isset($all_regions_restricted_categories)) {
$third_party_settings['restricted_categories'][$section]['all_regions'] = $all_regions_restricted_categories;
}
}
else {
// Unset 'all_regions'. This will be readded if there is tempstore data.
foreach ($restriction_types as $logic_type) {
unset($third_party_settings[$logic_type . '_blocks'][$section]['all_regions']);
}
unset($third_party_settings['restricted_categories'][$section]);
foreach ($regions as $region_id => $region) {
$categories = $store
->get($static_id . ':' . $section . ':' . $region_id);
if (!is_null($categories)) {
// Unset any existing config for region.
foreach ($restriction_types as $logic_type) {
unset($third_party_settings[$logic_type . '_blocks'][$section][$region_id]);
}
foreach ($categories as $category => $settings) {
$restriction_type = $settings['restriction_type'];
// Category is restricted.
if ($restriction_type == 'restrict_all') {
$third_party_settings['restricted_categories'][$section][$region_id][] = $category;
}
elseif (in_array($restriction_type, $restriction_types)) {
if (empty($settings['restrictions'])) {
$third_party_settings[$restriction_type . '_blocks'][$section][$region_id][$category] = [];
}
else {
foreach ($settings['restrictions'] as $block_id => $block_setting) {
$third_party_settings[$restriction_type . '_blocks'][$section][$region_id][$category][] = $block_id;
}
}
}
}
}
}
}
}
// Ensure data is saved in consistent alpha order by region.
foreach ($restriction_types as $logic_type) {
if (isset($third_party_settings[$logic_type . '_blocks'])) {
foreach ($third_party_settings[$logic_type . '_blocks'] as $section => $regions) {
ksort($regions);
$third_party_settings[$logic_type . '_blocks'][$section] = $regions;
}
}
if (isset($third_party_settings[$logic_type . '_blocks'])) {
// Ensure data is saved in alpha order by layout.
ksort($third_party_settings[$logic_type . '_blocks']);
}
}
$display
->setThirdPartySetting('layout_builder_restrictions', 'entity_view_mode_restriction_by_region', $third_party_settings);
}