You are here

public function FormAlter::alterEntityViewDisplayForm in Layout Builder Restrictions 8.2

Same name in this branch
  1. 8.2 src/Form/FormAlter.php \Drupal\layout_builder_restrictions\Form\FormAlter::alterEntityViewDisplayForm()
  2. 8.2 modules/layout_builder_restrictions_by_region/src/Form/FormAlter.php \Drupal\layout_builder_restrictions_by_region\Form\FormAlter::alterEntityViewDisplayForm()
Same name and namespace in other branches
  1. 8 src/Form/FormAlter.php \Drupal\layout_builder_restrictions\Form\FormAlter::alterEntityViewDisplayForm()

The actual form elements.

File

src/Form/FormAlter.php, line 112

Class

FormAlter
Supplement form UI to add setting for which blocks & layouts are available.

Namespace

Drupal\layout_builder_restrictions\Form

Code

public function alterEntityViewDisplayForm(&$form, FormStateInterface $form_state, $form_id) {
  $display = $form_state
    ->getFormObject()
    ->getEntity();
  $is_enabled = $display
    ->isLayoutBuilderEnabled();
  if ($is_enabled) {
    $form['#entity_builders'][] = [
      $this,
      'entityFormEntityBuild',
    ];

    // Block settings.
    $form['layout']['layout_builder_restrictions']['allowed_blocks'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Blocks available for placement (all layouts & regions)'),
      '#states' => [
        'disabled' => [
          ':input[name="layout[enabled]"]' => [
            'checked' => FALSE,
          ],
        ],
        'invisible' => [
          ':input[name="layout[enabled]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $third_party_settings = $display
      ->getThirdPartySetting('layout_builder_restrictions', 'entity_view_mode_restriction', []);
    $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'] : [];
    $restricted_categories = isset($third_party_settings['restricted_categories']) ? $third_party_settings['restricted_categories'] : [];
    $allowed_block_categories = $display
      ->getThirdPartySetting('layout_builder_restrictions', 'allowed_block_categories', []);
    foreach ($this
      ->getBlockDefinitions($display) as $category => $data) {
      $title = $data['label'];
      if (!empty($data['translated_label'])) {
        $title = $data['translated_label'];
      }
      $category_form = [
        '#type' => 'fieldset',
        '#title' => $title,
        '#parents' => [
          'layout_builder_restrictions',
          'allowed_blocks',
        ],
      ];

      // Check whether this is a newly available category that has been
      // restricted previously.
      $category_is_restricted = !empty($allowed_block_categories) && !in_array($category, $allowed_block_categories);

      // The category is 'restricted' if it's already been specified as such,
      // or if the default behavior for new categories indicate such.
      if (in_array($category, array_keys($whitelisted_blocks))) {
        $category_setting = 'whitelisted';
      }
      elseif (in_array($category, array_keys($blacklisted_blocks))) {
        $category_setting = 'blacklisted';
      }
      elseif ($category_is_restricted) {
        $category_setting = 'restrict_all';
      }
      elseif (in_array($category, $restricted_categories)) {
        $category_setting = 'restrict_all';
      }
      else {
        $category_setting = 'all';
      }
      $category_form['restriction_behavior'] = [
        '#type' => 'radios',
        '#options' => [
          "all" => $this
            ->t('Allow all existing & new %category blocks.', [
            '%category' => $data['label'],
          ]),
          "restrict_all" => $this
            ->t('Restrict all existing & new %category blocks.', [
            '%category' => $data['label'],
          ]),
          "whitelisted" => $this
            ->t('Allow specific %category blocks:', [
            '%category' => $data['label'],
          ]),
          "blacklisted" => $this
            ->t('Restrict specific %category blocks:', [
            '%category' => $data['label'],
          ]),
        ],
        '#default_value' => $category_setting,
        '#parents' => [
          'layout_builder_restrictions',
          'allowed_blocks',
          $category,
          'restriction',
        ],
      ];
      $category_form['available_blocks'] = [
        '#type' => 'container',
        '#states' => [
          'invisible' => [
            [
              ':input[name="layout_builder_restrictions[allowed_blocks][' . $category . '][restriction]"]' => [
                'value' => "all",
              ],
            ],
            [
              ':input[name="layout_builder_restrictions[allowed_blocks][' . $category . '][restriction]"]' => [
                'value' => "restrict_all",
              ],
            ],
          ],
        ],
      ];
      foreach ($data['definitions'] as $block_id => $block) {
        $enabled = FALSE;
        if ($category_setting == 'whitelisted' && isset($whitelisted_blocks[$category]) && in_array($block_id, $whitelisted_blocks[$category])) {
          $enabled = TRUE;
        }
        elseif ($category_setting == 'blacklisted' && isset($blacklisted_blocks[$category]) && in_array($block_id, $blacklisted_blocks[$category])) {
          $enabled = TRUE;
        }
        $category_form['available_blocks'][$block_id] = [
          '#type' => 'checkbox',
          '#title' => $block['admin_label'],
          '#default_value' => $enabled,
          '#parents' => [
            'layout_builder_restrictions',
            'allowed_blocks',
            $category,
            'available_blocks',
            $block_id,
          ],
        ];
      }
      if ($category == 'Custom blocks' || $category == 'Custom block types') {
        $category_form['description'] = [
          '#type' => 'container',
          '#children' => $this
            ->t('<p>In the event both <em>Custom Block Types</em> and <em>Custom Blocks</em> restrictions are enabled, <em>Custom Block Types</em> restrictions are disregarded.</p>'),
          '#states' => [
            'visible' => [
              ':input[name="layout_builder_restrictions[allowed_blocks][' . $category . '][restriction]"]' => [
                'value' => "restricted",
              ],
            ],
          ],
        ];
      }
      $form['layout']['layout_builder_restrictions']['allowed_blocks'][$category] = $category_form;
    }

    // Layout settings.
    $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",
    ];
    $definitions = $this
      ->getLayoutDefinitions();
    foreach ($definitions as $plugin_id => $definition) {
      $enabled = FALSE;
      if (!empty($allowed_layouts) && in_array($plugin_id, $allowed_layouts)) {
        $enabled = TRUE;
      }
      $layout_form['layouts'][$plugin_id] = [
        '#type' => 'checkbox',
        '#default_value' => $enabled,
        '#description' => [
          $definition
            ->getIcon(60, 80, 1, 3),
          [
            '#type' => 'container',
            '#children' => $definition
              ->getLabel() . ' (' . $plugin_id . ')',
          ],
        ],
        '#states' => [
          'invisible' => [
            ':input[name="layout_builder_restrictions[allowed_layouts][layout_restriction]"]' => [
              'value' => "all",
            ],
          ],
        ],
      ];
    }
    $form['layout']['layout_builder_restrictions']['allowed_layouts'] = $layout_form;
  }
}