function ExposedFormPluginBase::exposedFormAlter

Same name and namespace in other branches
  1. 9 core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase::exposedFormAlter()
  2. 8.9.x core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase::exposedFormAlter()
  3. 10 core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase::exposedFormAlter()

Overrides ExposedFormPluginInterface::exposedFormAlter

File

core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php, line 199

Class

ExposedFormPluginBase
Base class for Views exposed filter form plugins.

Namespace

Drupal\views\Plugin\views\exposed_form

Code

public function exposedFormAlter(&$form, FormStateInterface $form_state) {
    if (!empty($this->options['submit_button'])) {
        $form['actions']['submit']['#value'] = $this->options['submit_button'];
    }
    // Check if there is exposed sorts for this view
    $exposed_sorts = [];
    $exposed_sorts_options = [];
    foreach ($this->view->sort as $id => $handler) {
        if ($handler->canExpose() && $handler->isExposed() && !empty($handler->options['expose']['field_identifier'])) {
            $exposed_sorts[$handler->options['expose']['field_identifier']] = $id;
            $exposed_sorts_options[$handler->options['expose']['field_identifier']] = $handler->options['expose']['label'];
        }
    }
    if (count($exposed_sorts)) {
        $form['sort_by'] = [
            '#type' => 'select',
            '#options' => $exposed_sorts_options,
            '#title' => $this->options['exposed_sorts_label'],
        ];
        $sort_order = [
            'ASC' => $this->options['sort_asc_label'],
            'DESC' => $this->options['sort_desc_label'],
        ];
        $user_input = $form_state->getUserInput();
        if (isset($user_input['sort_by']) && isset($exposed_sorts[$user_input['sort_by']]) && isset($this->view->sort[$exposed_sorts[$user_input['sort_by']]])) {
            $default_sort_order = $this->view->sort[$exposed_sorts[$user_input['sort_by']]]->options['order'];
        }
        else {
            $first_sort = reset($this->view->sort);
            $default_sort_order = $first_sort->options['order'];
        }
        if (!isset($user_input['sort_by'])) {
            $keys = array_keys($exposed_sorts);
            $user_input['sort_by'] = array_shift($keys);
            $form_state->setUserInput($user_input);
        }
        if ($this->options['expose_sort_order']) {
            $form['sort_order'] = [
                '#type' => 'select',
                '#options' => $sort_order,
                '#title' => $this->t('Order', [], [
                    'context' => 'Sort order',
                ]),
                '#default_value' => $default_sort_order,
            ];
        }
    }
    if (!empty($this->options['reset_button'])) {
        $form['actions']['reset'] = [
            '#value' => $this->options['reset_button_label'],
            '#type' => 'submit',
            '#weight' => 10,
        ];
        // Get an array of exposed filters, keyed by identifier option.
        $exposed_filters = [];
        foreach ($this->view->filter as $id => $handler) {
            if ($handler->canExpose() && $handler->isExposed() && !empty($handler->options['expose']['identifier'])) {
                $exposed_filters[$handler->options['expose']['identifier']] = $id;
            }
        }
        $all_exposed = array_merge($exposed_sorts, $exposed_filters);
        // Set the access to FALSE if there is no exposed input.
        if (!array_intersect_key($all_exposed, $this->view
            ->getExposedInput())) {
            $form['actions']['reset']['#access'] = FALSE;
        }
    }
    $pager = $this->view->display_handler
        ->getPlugin('pager');
    if ($pager) {
        $pager->exposedFormAlter($form, $form_state);
        $form_state->set('pager_plugin', $pager);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.