You are here

public function BlockVisibilityGroupedListBuilder::buildForm in Block Visibility Groups 8

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 BlockListBuilder::buildForm

File

src/BlockVisibilityGroupedListBuilder.php, line 92

Class

BlockVisibilityGroupedListBuilder
Extends BlockListBuilder to add our elements only show certain blocks.

Namespace

Drupal\block_visibility_groups

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $group_options = $this
    ->getBlockVisibilityGroupOptions();
  $default_value = $this
    ->getCurrentBlockVisibilityGroup();
  $current_block_visibility_group = NULL;
  if (!in_array($default_value, [
    BlockVisibilityGroupedListBuilder::ALL_GROUP,
    BlockVisibilityGroupedListBuilder::UNSET_GROUP,
  ])) {
    $current_block_visibility_group = $default_value;
  }
  $options = [];
  foreach ($group_options as $key => $group_option) {
    if ($default_value == $key) {
      $default_value = $group_option['path'];
    }
    $options[$group_option['path']] = $group_option['label'];
  }
  $form['block_visibility_group'] = [
    '#weight' => -100,
  ];
  $form['block_visibility_group']['select'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Block Visibility Group'),
    '#options' => $options,
    '#default_value' => $default_value,
    // @todo Is there a better way to do this?
    '#attributes' => [
      'onchange' => 'this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value)',
    ],
  ];
  $description = $this
    ->t('Block Visibility Groups allow you to control the visibility of multiple blocks in one place.');
  if (!$this
    ->groupsExist()) {
    $description .= ' ' . $this
      ->t('No Groups have been created yet.');
    $form['block_visibility_group']['create'] = [
      '#type' => 'link',
      '#title' => t('Create a Group'),
      '#url' => Url::fromRoute('entity.block_visibility_group.add_form'),
    ];
  }
  else {
    if ($current_block_visibility_group) {
      $form['block_visibility_group']['block_visibility_group_show_global'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Show Global Blocks'),
        '#default_value' => $this
          ->getShowGlobalWithGroup(),
        '#description' => $this
          ->t('Show global blocks when viewing a visibility group.'),
        '#attributes' => [
          'onchange' => 'this.form.submit()',
        ],
      ];

      /** @var \Drupal\block_visibility_groups\Entity\BlockVisibilityGroup $group */
      $group = $this->group_storage
        ->load($current_block_visibility_group);
      $form['block_visibility_group']['help'] = $this
        ->createHelp($group);
      $conditions_element = $this
        ->createConditionsSet($form, $group, 'layout');
      $conditions_element['#type'] = 'details';
      if ($this->request->query
        ->get('show_conditions')) {
        $conditions_element['#open'] = TRUE;
      }
      else {
        $conditions_element['#open'] = FALSE;
      }
      $form['block_visibility_group']['conditions_section'] = $conditions_element;
    }
  }
  $form['block_visibility_group']['select']['#description'] = $description;
  return $form;
}