You are here

public function FormAlter::entityFormEntityBuild in Layout Builder Restrictions 8

Same name and namespace in other branches
  1. 8.2 src/Form/FormAlter.php \Drupal\layout_builder_restrictions\Form\FormAlter::entityFormEntityBuild()

Save allowed blocks & layouts for the given entity view mode.

File

src/Form/FormAlter.php, line 241

Class

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

Namespace

Drupal\layout_builder_restrictions\Form

Code

public function entityFormEntityBuild($entity_type_id, LayoutEntityDisplayInterface $display, &$form, FormStateInterface &$form_state) {

  // Set allowed blocks.
  $allowed_blocks = [];
  $categories = $form_state
    ->getValue([
    'layout_builder_restrictions',
    'allowed_blocks',
  ]);
  if (!empty($categories)) {
    foreach ($categories as $category => $category_setting) {
      if ($category_setting['restriction'] === 'restricted') {
        $allowed_blocks[$category] = [];
        unset($category_setting['restriction']);
        foreach ($category_setting as $block_id => $block_setting) {
          if ($block_setting == '1') {

            // Include only checked blocks.
            $allowed_blocks[$category][] = $block_id;
          }
        }
      }
    }
    $display
      ->setThirdPartySetting('layout_builder_restrictions', 'allowed_blocks', $allowed_blocks);
  }

  // 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',
    ])));
  }
  $display
    ->setThirdPartySetting('layout_builder_restrictions', 'allowed_layouts', $allowed_layouts);
}