You are here

public function BetterExposedFilters::exposedFormAlter in Better Exposed Filters 8.4

Same name and namespace in other branches
  1. 8.5 src/Plugin/views/exposed_form/BetterExposedFilters.php \Drupal\better_exposed_filters\Plugin\views\exposed_form\BetterExposedFilters::exposedFormAlter()
  2. 8.3 src/Plugin/views/exposed_form/BetterExposedFilters.php \Drupal\better_exposed_filters\Plugin\views\exposed_form\BetterExposedFilters::exposedFormAlter()

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/BetterExposedFilters.php, line 702

Class

BetterExposedFilters
Exposed form plugin that provides a basic exposed form.

Namespace

Drupal\better_exposed_filters\Plugin\views\exposed_form

Code

public function exposedFormAlter(&$form, FormStateInterface $form_state) {
  parent::exposedFormAlter($form, $form_state);

  // Mark form as Better Exposed Filter form for easier alterations.
  $form['#context']['bef'] = TRUE;

  // These styles are used on all exposed forms.
  $form['#attached']['library'][] = 'better_exposed_filters/general';

  // Add the bef-exposed-form class at the form level so we can limit some
  // styling changes to just BEF forms.
  $form['#attributes']['class'][] = 'bef-exposed-form';

  // Grab BEF options and allow modules/theme to modify them before
  // processing.
  $bef_options = $this->options['bef'];
  $this->moduleHandler
    ->alter('better_exposed_filters_options', $bef_options, $this->view, $this->displayHandler);

  // Apply auto-submit values.
  if (!empty($bef_options['general']['autosubmit'])) {
    $form = array_merge_recursive($form, [
      '#attributes' => [
        'data-bef-auto-submit-full-form' => '',
        'data-bef-auto-submit' => '',
        'data-bef-auto-submit-delay' => $bef_options['general']['autosubmit_textfield_delay'],
      ],
    ]);
    $form['actions']['submit']['#attributes']['data-bef-auto-submit-click'] = '';
    $form['#attached']['library'][] = 'better_exposed_filters/auto_submit';
    if (!empty($bef_options['general']['autosubmit_exclude_textfield'])) {
      $supported_types = [
        'entity_autocomplete',
        'textfield',
      ];
      foreach ($form as &$element) {
        $element_type = $element['#type'] ?? NULL;
        if (in_array($element_type, $supported_types)) {
          $element['#attributes']['data-bef-auto-submit-exclude'] = '';
        }
      }
    }
    if (!empty($bef_options['general']['autosubmit_hide'])) {
      $form['actions']['submit']['#attributes']['class'][] = 'js-hide';
    }
  }

  // Some elements may be placed in a secondary details element (eg: "Advanced
  // search options"). Place this after the exposed filters and before the
  // rest of the items in the exposed form.
  $allow_secondary = $bef_options['general']['allow_secondary'];
  if ($allow_secondary) {
    $form['secondary'] = [
      '#attributes' => [
        'class' => [
          'bef--secondary',
        ],
      ],
      '#type' => 'details',
      '#title' => $bef_options['general']['secondary_label'],
      '#open' => $bef_options['general']['secondary_open'],
      // Disable until fields are added to this fieldset.
      '#access' => FALSE,
    ];
  }

  /*
   * Handle exposed sort elements.
   */
  if (isset($bef_options['sort']['plugin_id']) && !empty($form['sort_by'])) {
    $plugin_id = $bef_options['sort']['plugin_id'];
    $plugin_configuration = $bef_options['sort'];

    /** @var \Drupal\better_exposed_filters\Plugin\BetterExposedFiltersWidgetInterface $plugin */
    $plugin = $this->sortWidgetManager
      ->createInstance($plugin_id, $plugin_configuration);
    $plugin
      ->setView($this->view);
    $plugin
      ->exposedFormAlter($form, $form_state);
  }

  /*
   * Handle exposed pager elements.
   */
  $pager = $this->view
    ->getPager();
  $is_pager_exposed = $pager && $pager
    ->usesExposed();
  if ($is_pager_exposed && !empty($bef_options['pager']['plugin_id'])) {
    $plugin_id = $bef_options['pager']['plugin_id'];
    $plugin_configuration = $bef_options['pager'];

    /** @var \Drupal\better_exposed_filters\Plugin\BetterExposedFiltersWidgetInterface $plugin */
    $plugin = $this->pagerWidgetManager
      ->createInstance($plugin_id, $plugin_configuration);
    $plugin
      ->setView($this->view);
    $plugin
      ->exposedFormAlter($form, $form_state);
  }

  /*
   * Handle exposed filters.
   */

  // Shorthand for all filter handlers in this view.

  /** @var \Drupal\views\Plugin\views\HandlerBase[] $filters */
  $filters = $this->view->display_handler->handlers['filter'];

  // Iterate over all exposed filters.
  if (!empty($bef_options['filter'])) {
    foreach ($bef_options['filter'] as $filter_id => $filter_options) {

      // Sanity check: Ensure this filter is an exposed filter.
      if (empty($filters[$filter_id]) || !$filters[$filter_id]
        ->isExposed()) {
        continue;
      }
      $plugin_id = $filter_options['plugin_id'];
      if ($plugin_id) {

        /** @var \Drupal\better_exposed_filters\Plugin\BetterExposedFiltersWidgetInterface $plugin */
        $plugin = $this->filterWidgetManager
          ->createInstance($plugin_id, $filter_options);
        $plugin
          ->setView($this->view);
        $plugin
          ->setViewsHandler($filters[$filter_id]);
        $plugin
          ->exposedFormAlter($form, $form_state);
      }
    }
  }

  // If our form has no visible filters, hide the submit button.
  $has_visible_filters = !empty(Element::getVisibleChildren($form)) ?: FALSE;
  $form['actions']['submit']['#access'] = $has_visible_filters;

  // Never enable a reset button that has already been disabled.
  if (!isset($form['actions']['reset']['#access']) || $form['actions']['reset']['#access'] === TRUE) {
    $form['actions']['reset']['#access'] = $has_visible_filters;
  }

  // Ensure default process/pre_render callbacks are included when a BEF
  // widget has added their own.
  foreach (Element::children($form) as $key) {
    $element =& $form[$key];
    $this
      ->addDefaultElementInfo($element);
  }
}