You are here

public function ExposedFormPluginBase::renderExposedForm in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase::renderExposedForm()
  2. 9 core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase::renderExposedForm()

Renders the exposed form.

This method iterates over each handler configured to expose widgets to the end user and attach those widgets to the exposed form.

Parameters

bool $block: (optional) TRUE if the exposed form is being rendered as part of a block; FALSE (default) if not.

Return value

array Form build array. This method returns an empty array if the form is being rendered as a block.

Overrides ExposedFormPluginInterface::renderExposedForm

See also

\Drupal\views\ViewExecutable::build()

File

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

Class

ExposedFormPluginBase
Base class for Views exposed filter form plugins.

Namespace

Drupal\views\Plugin\views\exposed_form

Code

public function renderExposedForm($block = FALSE) {

  // Deal with any exposed filters we may have, before building.
  $form_state = (new FormState())
    ->setStorage([
    'view' => $this->view,
    'display' => &$this->view->display_handler->display,
    'rerender' => TRUE,
  ])
    ->setMethod('get')
    ->setAlwaysProcess()
    ->disableRedirect();

  // Some types of displays (eg. attachments) may wish to use the exposed
  // filters of their parent displays instead of showing an additional
  // exposed filter form for the attachment as well as that for the parent.
  if (!$this->view->display_handler
    ->displaysExposed() || !$block && $this->view->display_handler
    ->getOption('exposed_block')) {
    $form_state
      ->set('rerender', NULL);
  }
  if (!empty($this->ajax)) {
    $form_state
      ->set('ajax', TRUE);
  }
  $form = \Drupal::formBuilder()
    ->buildForm('\\Drupal\\views\\Form\\ViewsExposedForm', $form_state);
  $errors = $form_state
    ->getErrors();

  // If the exposed form had errors, do not build the view.
  if (!empty($errors)) {
    $this->view->build_info['abort'] = TRUE;
  }
  if (!$this->view->display_handler
    ->displaysExposed() || !$block && $this->view->display_handler
    ->getOption('exposed_block')) {
    return [];
  }
  else {
    return $form;
  }
}