You are here

public function ExposedGroups::exposedFormAlter in Views exposed groups 3.0.x

Alters the exposed form.

The exposed form is built by calling the renderExposedForm() method on this class, and then letting each exposed filter and sort handler add widgets to the form. After that is finished, this method is called to let the class alter the finished form.

Parameters

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

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

Overrides ExposedFormPluginBase::exposedFormAlter

See also

\Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface::renderExposedForm()

\Drupal\views\Form\ViewsExposedForm::buildForm()

File

src/Plugin/views/exposed_form/ExposedGroups.php, line 328

Class

ExposedGroups
Provides an views exposed form plugin that groups filters.

Namespace

Drupal\views_exposed_groups\Plugin\views\exposed_form

Code

public function exposedFormAlter(&$form, FormStateInterface $form_state) {
  parent::exposedFormAlter($form, $form_state);
  if (!isset($form['#attributes']['class'])) {
    $form['#attributes']['class'] = [];
  }
  $form['#attributes']['class'][] = 'views-exposed-form--views-exposed-group';
  if ($this->options['submit_button']) {
    $form['submit']['#value'] = $this->options['submit_button'];
  }
  if ($this->options['exposed_groups_format'] === 'vertical_tabs') {
    $form['filters'] = [
      '#type' => 'vertical_tabs',
      '#weight' => -10,
      '#attached' => [
        'library' => [
          'views_exposed_groups/views_exposed_groups',
        ],
        'drupalSettings' => [
          'viewsExposedGroups' => [
            'id' => $form['#id'],
            'options' => $this->options,
          ],
        ],
      ],
    ];
  }
  else {
    $form['filters'] = [
      '#weight' => -10,
    ];
  }
  $groups = $this->options['exposed_groups'];
  foreach ($groups as $index => $group) {

    // Sets up each group.
    if ($group['name'] !== '_none') {
      $form['filters'][$group['name']] = [
        '#type' => 'fieldset',
        '#title' => Xss::filter($group['label']),
        '#collapsible' => TRUE,
        '#collapsed' => $this->options['exposed_groups_format'] === 'fieldsets_collapsed',
        '#weight' => $group['weight'],
      ];
    }
    else {
      $form['filters'][$group['name']] = [
        '#weight' => $group['weight'],
      ];
    }
    $element =& $form['filters'][$group['name']];
    foreach ($group['filters'] as $id => $filter_info) {

      // Adds each filter into the group.
      if (isset($form[$id]) && is_array($form[$id])) {

        // Copies the exposed filter form element.
        $element[$id] = $form[$id] + [
          '#weight' => $filter_info['#weight'],
        ];
        if (isset($form[$id . '_op']) && is_array($form[$id . '_op'])) {

          // @todo Copies the exposed operator form element.
          unset($form[$id . '_op']);
        }
        unset($form[$id]);
      }
    }
  }
  if (!empty($this->options['reset_button'])) {
    $form['reset'] = [
      '#value' => $this->options['reset_button_label'],
      '#type' => 'submit',
    ];
  }
}