Exposed filters does not works with a Views block display without AJAX

Created on 8 September 2020, over 4 years ago
Updated 26 June 2023, over 1 year ago

Problem/Motivation

Create a Views block display with the "Use AJAX" unchecked and exposed filters (in my test case it was a Fulltext search field from the Search API) does not apply the exposed filters to the view query executed in the exposed filters block.

The views Block display has a method who's not apply because the ajaxEnabled() condition does not match.

  /**
   * Block views use exposed widgets only if AJAX is set.
   */
  public function usesExposed() {
    if ($this->ajaxEnabled()) {
      return parent::usesExposed();
    }
    return FALSE;
  }

Steps to reproduce

  1. Create a View with a block display
  2. Add exposed filters fields
  3. Create a block (plugin views_exposed_filter_blocks_block to render the exposed filters outside the View block
  4. Fill the exposed filters fields, and submit the form
  5. The results are the same as the unfiltered results

Proposed resolution

Alter the hook_views_pre_build / hook_views_post_build to first simulate the AJAX is enable (and allow the Views code to execute the exposed code to apply the exposed data to the query, and rollback to the initial state before the Render API build the view output.

/**
 * Implements hook_views_pre_build().
 */
function views_exposed_filter_blocks_views_pre_build(ViewExecutable $view) {
  $display = $view->getDisplay();
  if ($display instanceof Block) {
    if (!$display->ajaxEnabled()) {
      $display->setOption('use_ajax', TRUE);
      $display->setOption('overwrite_use_ajax', TRUE);
    }
  }
}

/**
 * Implements hook_views_pre_build().
 */
/**
 * Implements hook_views_post_build().
 */
function views_exposed_filter_blocks_views_post_build(ViewExecutable $view) {
  $display = $view->getDisplay();
  if ($display instanceof Block) {
    if ($display->getOption('overwrite_use_ajax')) {
      $view->exposed_widgets = NULL;
      $display->setOption('use_ajax', FALSE);
      $display->setOption('overwrite_use_ajax', FALSE);
    }
  }
}
🐛 Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

🇫🇷France xavier.masson Haute-Normandie

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024