function ViewsExposedForm::buildForm

Same name in other branches
  1. 9 core/modules/views/src/Form/ViewsExposedForm.php \Drupal\views\Form\ViewsExposedForm::buildForm()
  2. 8.9.x core/modules/views/src/Form/ViewsExposedForm.php \Drupal\views\Form\ViewsExposedForm::buildForm()
  3. 10 core/modules/views/src/Form/ViewsExposedForm.php \Drupal\views\Form\ViewsExposedForm::buildForm()

Overrides FormInterface::buildForm

File

core/modules/views/src/Form/ViewsExposedForm.php, line 70

Class

ViewsExposedForm
Provides the views exposed form.

Namespace

Drupal\views\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
    // Make sure that we validate because this form might be submitted
    // multiple times per page.
    $form_state->setValidationEnforced();
    
    /** @var \Drupal\views\ViewExecutable $view */
    $view = $form_state->get('view');
    $display =& $form_state->get('display');
    $form_state->setUserInput($view->getExposedInput());
    // Let form plugins know this is for exposed widgets.
    $form_state->set('exposed', TRUE);
    // Check if the form was already created
    if ($cache = $this->exposedFormCache
        ->getForm($view->storage
        ->id(), $view->current_display)) {
        return $cache;
    }
    $form['#info'] = [];
    // Go through each handler and let it generate its exposed widget.
    foreach ($view->display_handler->handlers as $type => $value) {
        
        /** @var \Drupal\views\Plugin\views\ViewsHandlerInterface $handler */
        foreach ($view->{$type} as $id => $handler) {
            if ($handler->canExpose() && $handler->isExposed()) {
                // Grouped exposed filters have their own forms.
                // Instead of render the standard exposed form, a new Select or
                // Radio form field is rendered with the available groups.
                // When a user chooses an option the selected value is split
                // into the operator and value that the item represents.
                if ($handler->isAGroup()) {
                    $handler->groupForm($form, $form_state);
                    $id = $handler->options['group_info']['identifier'];
                }
                else {
                    $handler->buildExposedForm($form, $form_state);
                }
                if ($info = $handler->exposedInfo()) {
                    $form['#info']["{$type}-{$id}"] = $info;
                }
            }
        }
    }
    $form['actions'] = [
        '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
        // Prevent from showing up in \Drupal::request()->query.
'#name' => '',
        '#type' => 'submit',
        '#value' => $this->t('Apply'),
        '#id' => Html::getUniqueId('edit-submit-' . $view->storage
            ->id()),
    ];
    if (!$view->hasUrl()) {
        // On any non views.ajax route, use the current route for the form action.
        if ($this->getRouteMatch()
            ->getRouteName() !== 'views.ajax') {
            $form_action = Url::fromRoute('<current>')->toString();
        }
        else {
            // On the views.ajax route, set the action to the page we were on.
            $form_action = Url::fromUserInput($this->currentPathStack
                ->getPath())
                ->toString();
        }
    }
    else {
        $form_action = $view->getUrl()
            ->toString();
    }
    $form['#action'] = $form_action;
    $form['#theme'] = $view->buildThemeFunctions('views_exposed_form');
    $form['#id'] = Html::cleanCssIdentifier('views_exposed_form-' . $view->storage
        ->id() . '-' . $display['id']);
    // Labels are built too late for inline form errors to work, resulting
    // in duplicated messages.
    $form['#disable_inline_form_errors'] = TRUE;
    
    /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form_plugin */
    $exposed_form_plugin = $view->display_handler
        ->getPlugin('exposed_form');
    $exposed_form_plugin->exposedFormAlter($form, $form_state);
    // Save the form.
    $this->exposedFormCache
        ->setForm($view->storage
        ->id(), $view->current_display, $form);
    return $form;
}

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