You are here

public function AllowedBlocksForm::buildForm in Layout Builder Restrictions 8.2

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

modules/layout_builder_restrictions_by_region/src/Form/AllowedBlocksForm.php, line 191

Class

AllowedBlocksForm
Provides form for designating allowed blocks.

Namespace

Drupal\layout_builder_restrictions_by_region\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $current_request = $this->requestStack
    ->getCurrentRequest();
  $static_id = $current_request->query
    ->get('static_id');
  $entity_view_display_id = $current_request->query
    ->get('entity_view_display_id');
  $layout_plugin = $current_request->query
    ->get('layout_plugin');
  $region_id = $current_request->query
    ->get('region_id');
  $display = $this->entityTypeManager
    ->getStorage('entity_view_display')
    ->load($entity_view_display_id);

  // Load tempstore data.
  $tempstore = $this->privateTempStoreFactory;
  $store = $tempstore
    ->get('layout_builder_restrictions_by_region');
  $temp_data = $store
    ->get($static_id . ':' . $layout_plugin . ':' . $region_id);
  $layout_definition = $this->layoutManager
    ->getDefinition($layout_plugin);
  $regions = $layout_definition
    ->getRegions();
  $regions['all_regions'] = [
    'label' => $this
      ->t('All regions'),
  ];
  $region_label = $regions[$region_id]['label']
    ->render();
  $layout_label = $layout_definition
    ->getLabel();
  $form['config_context_markup'] = [
    '#markup' => $this
      ->t('<strong>Layout:</strong> @layout_label<br><strong>Region:</strong> @region_label', [
      '@layout_label' => $layout_label,
      '@region_label' => $region_label,
    ]),
  ];
  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,
    ];
    $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'],
        ]),
      ],
      '#parents' => [
        'allowed_blocks',
        $category,
        'restriction',
      ],
    ];
    $category_form['restriction_behavior']['#default_value'] = $this
      ->getCategoryBehavior($category, $temp_data);
    $category_form['allowed_blocks'] = [
      '#type' => 'container',
      '#states' => [
        'invisible' => [
          [
            ':input[name="allowed_blocks[' . $category . '][restriction]"]' => [
              'value' => "all",
            ],
          ],
          [
            ':input[name="allowed_blocks[' . $category . '][restriction]"]' => [
              'value' => "restrict_all",
            ],
          ],
        ],
      ],
    ];
    foreach ($data['definitions'] as $block_id => $block) {
      $category_form['allowed_blocks'][$block_id] = [
        '#type' => 'checkbox',
        '#title' => $block['admin_label'],
        '#default_value' => $this
          ->getBlockDefault($block_id, $category, $temp_data),
        '#parents' => [
          'allowed_blocks',
          $category,
          'allowed_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="allowed_blocks[' . $category . '][restriction]"]' => [
              'value' => "restricted",
            ],
          ],
        ],
      ];
    }
    $form['allowed_blocks'][$category] = $category_form;
  }
  $form['static_id'] = [
    '#type' => 'hidden',
    '#value' => $static_id,
  ];
  $form['layout_plugin'] = [
    '#type' => 'hidden',
    '#value' => $layout_plugin,
  ];
  $form['region_id'] = [
    '#type' => 'hidden',
    '#value' => $region_id,
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#ajax' => [
      'callback' => '::ajaxSubmit',
      'event' => 'click',
      'url' => Url::fromRoute("layout_builder_restrictions_by_region.{$display->getTargetEntityTypeId()}_allowed_blocks", [
        'static_id' => $static_id,
        'entity_view_display_id' => $entity_view_display_id,
        'layout_plugin' => $layout_plugin,
        'region_id' => $region_id,
      ]),
      'options' => [
        'query' => [
          FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
        ],
      ],
    ],
  ];
  return $form;
}