You are here

protected function FormAlter::setAllowedBlocks in Layout Builder Restrictions 8.2

Helper function to prepare saved allowed blocks.

Parameters

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

Return value

array An array of layout names or empty.

1 call to FormAlter::setAllowedBlocks()
FormAlter::entityFormEntityBuild in src/Form/FormAlter.php
Save allowed blocks & layouts for the given entity view mode.

File

src/Form/FormAlter.php, line 300

Class

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

Namespace

Drupal\layout_builder_restrictions\Form

Code

protected function setAllowedBlocks(FormStateInterface $form_state) {
  $categories = $form_state
    ->getValue([
    'layout_builder_restrictions',
    'allowed_blocks',
  ]);
  $block_restrictions = [];
  $block_restrictions['restricted_categories'] = [];
  if (!empty($categories)) {
    foreach ($categories as $category => $settings) {
      $restriction_type = $settings['restriction'];
      if (in_array($restriction_type, [
        'whitelisted',
        'blacklisted',
      ])) {
        $block_restrictions[$restriction_type][$category] = [];
        foreach ($settings['available_blocks'] as $block_id => $block_setting) {
          if ($block_setting == '1') {

            // Include only checked blocks.
            $block_restrictions[$restriction_type][$category][] = $block_id;
          }
        }
      }
      elseif ($restriction_type === "restrict_all") {
        $block_restrictions['restricted_categories'][] = $category;
      }
    }
  }
  return $block_restrictions;
}