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.
Comment | File | Size | Author |
---|---|---|---|
#23 | 2692297-23.patch | 612 bytes | idflood |
#8 | 2692297-8.patch | 627 bytes | aleksip |
#4 | 2692297-4.patch | 637 bytes | bircher |
#3 | exposed filter.png | 40.61 KB | Revathi.B |
Comments
Comment #2
Peter MajmeskuComment #3
Revathi.B CreditAttribution: Revathi.B commentedI have checked this issue in 8.2.x-dev.Its working fine.
Comment #4
bircherI changed it from bug report to feature request because it works when ajax is turned on for the view.
Comment #7
harings_rob CreditAttribution: harings_rob at Harings.be for Nuvole commentedNeeds a reroll for 8.3.x?
Comment #8
aleksipHere is a patch for 8.3.x
Comment #10
akalam CreditAttribution: akalam at Atenea tech commentedThe patch applies and works against 8.4.x
Comment #12
idflood CreditAttribution: idflood at Stimul.ch commentedPatch still applies to 8.5.4 and works very well, thanks!
Comment #13
borisson_We need tests to make sure that this works, and keeps working :)
Comment #15
swiftsystems CreditAttribution: swiftsystems commentedWill this be merged into the next core update?
Comment #16
yens CreditAttribution: yens at Dropsolid commentedPatch still applies on 8.6.10 and works, thanks!
Comment #17
dawehnerThe 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.
Comment #18
RabiaSajjad CreditAttribution: RabiaSajjad commentedIs this a duplicate issue Allow exposed-form-in-block for block displays?
Comment #19
RabiaSajjad CreditAttribution: RabiaSajjad commentedFor 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 argumenthasUrl($args = NULL, $display_id = NULL)
?Please let me know if this makes sense, I am fairly new to Drupal.
Comment #20
markdc#8 works for me on 8.6.17.
Comment #23
idflood CreditAttribution: idflood at Stimul.ch commentedQuick reroll of patch #8 for 8.9.* since it didn't apply anymore
Comment #24
handkerchiefMy workaround for a specific views block:
Comment #25
abhisekmazumdarThis issue has a patch. Changing its states to needs review.
Comment #26
dpiPatch still requires tests.
Comment #29
maskedjellybeanThe most recent patch does not apply in 9.3. The method these patches have been removing now looks like this:
Can anyone say whether this change happens to resolve this issue? I'll reroll the patch anyways.
Comment #30
maskedjellybeanActually this issue is fixed for me in 9.3 without any patch! Assuming this fix was introduced in one of the related issues.
Comment #31
scott_euser CreditAttribution: scott_euser as a volunteer and at Soapbox Communications Ltd commentedYep 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!