Problem/Motivation

The block display doesn't allow exposed filters unless ajax is turned on. This seems to stem form Drupal 7 limitations. For a a view with facets to work the whole page should be refreshed, however.

Proposed resolution

Allow non-ajax blocks to have exposed filters.
This works because views always uses <current> route when view-URL/display-path is not available, for example: http://cgit.drupalcode.org/drupal/tree/core/modules/views/src/Form/Views...

Remaining tasks

Find cases where this would be problematic.
Write tests.

User interface changes

The exposed filters appear and the wtf-moment for site builders disappears.

API changes

none

Data model changes

none

Original Issue:

Exposed filters are getting lost, when view is rendered as block

If I'm creating a block which is based on the content view page (Drupal core view - ID "content"), then the view works except of the exposed filters, e.g. filter by title. You can see the view at the Drupal 8 site's URL /admin/content/node.

I'm displaying the block in my custom template. I'm getting the block markup by my following controller method:

  /**
   * Gets the HTML markup for a block by it's ID.
   *
   * @param $sBlockId The id of the block as string.
   *
   * @return string The HTML markup for the rendered block.
   */
  private function getBlockMarkupById($sBlockId)
  {
    $oBlock = \Drupal\block\Entity\Block::load($sBlockId);
    $aRenderArray = \Drupal::entityManager()
      ->getViewBuilder('block')
      ->view($oBlock);

    return \Drupal::service('renderer')->render($aRenderArray);
  }

Everything except the exposed filters is displayed. What's wrong?

I've also seen, that the exposed filters in block views are already a problem in previous versions of Drupal. See the related issues.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jepSter created an issue. See original summary.

Peter Majmesku’s picture

Issue summary: View changes
Revathi.B’s picture

FileSize
40.61 KB

I have checked this issue in 8.2.x-dev.Its working fine.

bircher’s picture

Title: Exposed filters are getting lost, when view is rendered as block » Allow exposed form for non-ajax block displays
Version: 8.0.5 » 8.1.x-dev
Category: Bug report » Feature request
Issue summary: View changes
Status: Active » Needs review
Issue tags: +Needs tests
Related issues: +#2681947: Allow 'Put the exposed form in a block' setting to be used with views block displays
FileSize
637 bytes

I changed it from bug report to feature request because it works when ajax is turned on for the view.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

harings_rob’s picture

Needs a reroll for 8.3.x?

aleksip’s picture

Here is a patch for 8.3.x

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

akalam’s picture

The patch applies and works against 8.4.x

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

idflood’s picture

Patch still applies to 8.5.4 and works very well, thanks!

borisson_’s picture

Status: Needs review » Needs work

We need tests to make sure that this works, and keeps working :)

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

swiftsystems’s picture

Will this be merged into the next core update?

yens’s picture

Patch still applies on 8.6.10 and works, thanks!

dawehner’s picture

The only problem I see with this patch is that once enabled, effectively block output need to be cached per page and query parameter.
I guess that's an okayish price to pay, if you want to have this functionality, but maybe somehow warning the user could be nice.

On top of that I did some historic investigation. There is this comment from @merlinofchaos: https://www.drupal.org/project/views/issues/1115782#comment-4582214 , but well, I'm not sure this still applies for today.

I agree with @borisson_, we really need a test for this functionality, which could for example test the cacheability metadata.

RabiaSajjad’s picture

RabiaSajjad’s picture

For my use case I have a view with faceted search using Search API and Facets. I need to expose the Fulltext search as block which was possible because of the patch 2692297-8.patch (thank you!) so that I can setup facets with it.

I have two displays in my view, one with the search list (block) and next with details (page with a url) using a contextual filter. After applying the patch, the search is redirected to the url of page from details display. This is because the exposed search form has action url of the details page. With some investigation, ViewsExposedForm.php plugin in the Views module has

$form['#action'] = $view->hasUrl() ? $view->getUrl()->toString() : Url::fromRoute('<current>')->toString(); on line 116.

And $view->hasUrl() returns true if any display of the view has a url, which in my case is not correct. The form should post back to the page itself. To fix this:
1. Workaround, rather than creating one view with two displays, create two separate views.
2. In ViewsExposedForm.php pass current display id to $view->hasUrl() since hasUrl accepts $display_id as argument hasUrl($args = NULL, $display_id = NULL)?

Please let me know if this makes sense, I am fairly new to Drupal.

markdc’s picture

#8 works for me on 8.6.17.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

idflood’s picture

Quick reroll of patch #8 for 8.9.* since it didn't apply anymore

handkerchief’s picture

My workaround for a specific views block:

/**
 * Implements hook_form_alter().
 */
function HOOK_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  switch ($form_id) {
    case 'views_exposed_form':
      if ($form['#id'] === 'views-exposed-form-example-block-1') {
        $nid_view = '7';
        $url = Url::fromRoute('entity.node.canonical', ['node' => $nid_view])->toString();

        // Because reload is not working correctly.
        $form['#action'] = $url;
        $form['#attached']['library'][] = 'example/example';
      }
      break;
  }
}
(function ($, Drupal, drupalSettings) {
  Drupal.behaviors.exampleTest = {
    attach: function (context, settings) {

      let $actions = $(".block-views-example-block-1 div[data-drupal-selector='edit-actions'] input.form-submit");
      if ($actions.length) {
        // Prevent Ajax from submit handler.
        $actions.unbind();
      }

      if (window.location.href.indexOf("field_example_filter") > -1) {
        let $block_view_block_1 = $('.block-views-example-block-1');
        if($block_view_block_1.length ){
          $("html, body").animate({ scrollTop: $block_view_block_1.offset().top }, "slow");
        }
      }
    }
  };
})(jQuery, Drupal, drupalSettings);
abhisekmazumdar’s picture

Status: Needs work » Needs review

This issue has a patch. Changing its states to needs review.

dpi’s picture

Version: 8.9.x-dev » 9.2.x-dev
Status: Needs review » Needs work

Patch still requires tests.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

maskedjellybean’s picture

The most recent patch does not apply in 9.3. The method these patches have been removing now looks like this:

  public function usesExposed() {
    if (!isset($this->has_exposed)) {
      foreach ($this->handlers as $type => $value) {
        foreach ($this->view->$type as $handler) {
          if ($handler->canExpose() && $handler->isExposed()) {
            // One is all we need; if we find it, return TRUE.
            $this->has_exposed = TRUE;
            return TRUE;
          }
        }
      }
      $pager = $this->getPlugin('pager');
      if (isset($pager) && $pager->usesExposed()) {
        $this->has_exposed = TRUE;
        return TRUE;
      }
      $this->has_exposed = FALSE;
    }

    return $this->has_exposed;
  }

Can anyone say whether this change happens to resolve this issue? I'll reroll the patch anyways.

maskedjellybean’s picture

Actually this issue is fixed for me in 9.3 without any patch! Assuming this fix was introduced in one of the related issues.

scott_euser’s picture

Status: Needs work » Closed (outdated)

Yep its now returning TRUE by default: https://git.drupalcode.org/project/drupal/-/blob/9.3.x/core/modules/view...
As a result of https://www.drupal.org/project/drupal/issues/2681947

Happy to mark this as closed!