public function FormAlter::alterEntityViewDisplayForm in Layout Builder Restrictions 8.2
Same name in this branch
- 8.2 src/Form/FormAlter.php \Drupal\layout_builder_restrictions\Form\FormAlter::alterEntityViewDisplayForm()
- 8.2 modules/layout_builder_restrictions_by_region/src/Form/FormAlter.php \Drupal\layout_builder_restrictions_by_region\Form\FormAlter::alterEntityViewDisplayForm()
The actual form elements.
File
- modules/
layout_builder_restrictions_by_region/ src/ Form/ FormAlter.php, line 114
Class
- FormAlter
- Supplement form UI to add setting for which blocks & layouts are available.
Namespace
Drupal\layout_builder_restrictions_by_region\FormCode
public function alterEntityViewDisplayForm(&$form, FormStateInterface &$form_state, $form_id) {
// Create a unique ID for this form build and store it in a hidden
// element on the rendered form. This will be used to retrieve data
// from tempStore.
$user_input = $form_state
->getUserInput();
if (!isset($user_input['static_id'])) {
$static_id = $this->uuid
->generate();
$form['static_id'] = [
'#type' => 'hidden',
'#value' => $static_id,
];
}
else {
$static_id = $user_input['static_id'];
}
$display = $form_state
->getFormObject()
->getEntity();
$is_enabled = $display
->isLayoutBuilderEnabled();
if ($is_enabled) {
$form['layout']['layout_builder_restrictions']['messages'] = [
'#markup' => '<div id="layout-builder-restrictions-messages" class="hidden"></div>',
];
$form['#entity_builders'][] = [
$this,
'entityFormEntityBuild',
];
// Layout settings.
$third_party_settings = $display
->getThirdPartySetting('layout_builder_restrictions', 'entity_view_mode_restriction_by_region', []);
$allowed_layouts = isset($third_party_settings['allowed_layouts']) ? $third_party_settings['allowed_layouts'] : [];
$layout_form = [
'#type' => 'details',
'#title' => $this
->t('Layouts available for sections'),
'#parents' => [
'layout_builder_restrictions',
'allowed_layouts',
],
'#states' => [
'disabled' => [
':input[name="layout[enabled]"]' => [
'checked' => FALSE,
],
],
'invisible' => [
':input[name="layout[enabled]"]' => [
'checked' => FALSE,
],
],
],
];
$layout_form['layout_restriction'] = [
'#type' => 'radios',
'#options' => [
"all" => $this
->t('Allow all existing & new layouts.'),
"restricted" => $this
->t('Allow only specific layouts:'),
],
'#default_value' => !empty($allowed_layouts) ? "restricted" : "all",
];
$entity_view_display_id = $display
->get('id');
$definitions = $this
->getLayoutDefinitions();
foreach ($definitions as $section => $definition) {
$enabled = FALSE;
if (!empty($allowed_layouts) && in_array($section, $allowed_layouts)) {
$enabled = TRUE;
}
$layout_form['layouts'][$section] = [
'#type' => 'checkbox',
'#default_value' => $enabled,
'#description' => [
$definition
->getIcon(60, 80, 1, 3),
[
'#type' => 'container',
'#children' => $definition
->getLabel() . ' (' . $section . ')',
],
],
'#attributes' => [
'data-layout-plugin' => [
$section,
],
],
'#states' => [
'invisible' => [
':input[name="layout_builder_restrictions[allowed_layouts][layout_restriction]"]' => [
'value' => "all",
],
],
],
];
}
$form['layout']['layout_builder_restrictions']['allowed_layouts'] = $layout_form;
// Block settings.
$layout_definitions = $definitions;
foreach ($layout_definitions as $section => $definition) {
$regions = $definition
->getRegions();
$regions['all_regions'] = [
'label' => $this
->t('All regions'),
];
$form['layout'][$section] = [
'#type' => 'details',
'#title' => $this
->t('Blocks available for the <em>@layout_label</em> layout', [
'@layout_label' => $definition
->getLabel(),
]),
'#parents' => [
'layout_builder_restrictions',
'allowed_blocks_by_layout',
$section,
],
'#attributes' => [
'data-layout-plugin' => $section,
],
'#states' => [
'disabled' => [
[
':input[name="layout[enabled]"]' => [
'checked' => FALSE,
],
],
'or',
[
'#edit-layout-builder-restrictions-allowed-layouts :input[data-layout-plugin="' . $section . '"]' => [
'checked' => FALSE,
],
],
],
'invisible' => [
[
':input[name="layout[enabled]"]' => [
'checked' => FALSE,
],
],
'or',
[
'#edit-layout-builder-restrictions-allowed-layouts :input[data-layout-plugin="' . $section . '"]' => [
'checked' => FALSE,
],
],
],
],
];
$default_restriction_behavior = 'all';
if (isset($third_party_settings['whitelisted_blocks'][$section]) && !isset($third_party_settings['whitelisted_blocks'][$section]['all_regions'])) {
$default_restriction_behavior = 'per-region';
}
if (isset($third_party_settings['blacklisted_blocks'][$section]) && !isset($third_party_settings['blacklisted_blocks'][$section]['all_regions'])) {
$default_restriction_behavior = 'per-region';
}
if (isset($third_party_settings['restricted_categories'][$section]) && !isset($third_party_settings['restricted_categories'][$section]['all_regions'])) {
$default_restriction_behavior = 'per-region';
}
$form['layout'][$section]['restriction_behavior'] = [
'#type' => 'radios',
'#options' => [
"all" => $this
->t('Apply block restrictions to all regions in layout'),
"per-region" => $this
->t('Apply block restrictions on a region-by-region basis'),
],
'#attributes' => [
'class' => [
'restriction-type',
],
'data-layout-plugin' => $section,
],
'#default_value' => $default_restriction_behavior,
];
$form['layout'][$section]['table'] = [
'#type' => 'table',
'#header' => [
$this
->t('Region'),
$this
->t('Status'),
$this
->t('Operations'),
],
'#attributes' => [
'data-layout' => $section,
],
];
foreach ($regions as $region_id => $region) {
$form['layout'][$section]['table']['#rows'][$region_id] = [
'data-region' => $region_id,
'data' => [
'region_label' => [
'class' => [
'region-label',
],
'data' => [
'#markup' => $region['label']
->render(),
],
],
'status' => [
'class' => [
'restriction-status',
],
'id' => 'restriction-status--' . $section . '--' . $region_id,
'data' => [
'#markup' => '<span class="data">' . $this
->RegionRestrictionStatusString($section, $region_id, $static_id, $entity_view_display_id) . '</span>',
],
],
'operations' => [
'class' => [
'operations',
],
'data' => [
'#type' => 'dropbutton',
'#links' => [
'manage' => [
'title' => $this
->t('Manage allowed blocks'),
'url' => Url::fromRoute("layout_builder_restrictions_by_region.{$form['#entity_type']}_allowed_blocks", [
'static_id' => $static_id,
'entity_view_display_id' => $entity_view_display_id,
'layout_plugin' => $section,
'region_id' => $region_id,
]),
'attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 800,
]),
],
],
],
],
],
],
];
}
}
// Add certain variables as form state temp value for later use.
$form_state
->setTemporaryValue('static_id', $static_id);
$form['#attached']['library'][] = 'layout_builder_restrictions_by_region/display_mode_form';
}
}