Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Changes introduced in #2826449: Ajax Facets blocks for views mean that facet blocks are rendered even when they are empty. This creates display errors including:
- If a block is configured to render a title but is empty, the title appears because Drupal's block processing considers the block non-empty.
- Even without a title, the block may occupy screen space due to common Drupal theming approaches such as vertical margin between blocks.
Proposed resolution
In hook_preprocess_block()
, detect empty blocks and assign them the 'hidden'
class.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comment | File | Size | Author |
---|---|---|---|
#31 | facets-empty-block-2984465-31.patch | 1.46 KB | Nick_vh |
#30 | facets-empty-block-2984465-30.patch | 1.74 KB | Nick_vh |
#26 | facets-empty-block-2984465-26.patch | 816 bytes | nedjo |
#7 | 2984465.patch | 2.25 KB | borisson_ |
Comments
Comment #2
Nick_vhComment #3
Nick_vhSilly mistake with comments
Comment #4
Nick_vhTested this locally with bigpipe and ajax enabled and it seems to work pretty fine. Looking for some reviews from frontend developers here in terms of jquery performance impact.
Comment #5
Nick_vhIt is scary that all tests pass though. We need to write a test to make sure that the behavior was "wrong" before, and is corrected with this patch.
Comment #6
borisson_I agree, this needs a test.
Comment #7
borisson_While this needs a test, I think we should get this in and make sure we add new testcoverage in the future. I'll get to it eventually.
Comment #8
darvanenDidn't apply very well with composer but I think that's cweagans/composer-patchers, not this patch.
JS works well as long as people use the default block classes. Not sure how you would get around that in a more generic fashion.
I think this approach is good, and as @borisson_ says tests can come later I call this RTBC on #7.
Comment #10
borisson_Crediting Christian for their work in #2867000: Add "Render facet anway" emptyBehavior to the same effect as this issue.
Comment #11
seanBRerolled for latest module version.
Comment #12
seanBIs there a better way to find facet blocks than this class? A lot of (custom) themes might not have this class. Just ran into this while testing this.
Added the class to my template for now, but this could be an issue that is hard to debug later if you don't know the module.
Comment #13
borisson_We should probably add a class that is prefixed with
js-
and use that to target the blocks? If we issue a change request - that shouldn't be a problem I think?Comment #14
5n00py CreditAttribution: 5n00py as a volunteer commentedhttps://developer.mozilla.org/en-US/docs/Web/Events/DOMSubtreeModified
It's deprecated.
This who calls also can be avoided. We can hide elements using css, html attributes, etc.
$('body').on('DOMSubtreeModified', '.block-facets', function () {
In drupal behaviors he have context. It should be used to avoid adding multiple event subscribers.
Comment #15
5n00py CreditAttribution: 5n00py as a volunteer commentedFacets ajax callbacks uses ReplaceCommand to update blocks.
So when block updated via ajax or bigpipe module Drupal anyway attach behavior's for new one.
Also we can add attribute/class to empty block and i will be updated after ajax. Hiding can be implemented via css.
If we solve this issue in right way, we don't need any JS.
UPD: look \Drupal\facets\Controller\FacetBlockAjaxController::ajaxFacetBlockView
Comment #16
Jon352 CreditAttribution: Jon352 commentedThis seems related to an issue I'm running into regarding ajax facets. If you configure a facet to use a widget (dropdown for example) the library and settings are not attached if the facet is empty when it is first built. You may run into this for example if there is some filter condition from a view / contextual filter or whatever your use case is.
My use case is I'm setting some facets active by default depending on the values of some fields of a product content type. Pretend we are looking at a product that's tagged a certain way. On the product page I want to see related products by default. So I set some active facets. Unfortunately some facets may be empty.
The missing piece is getting the widget instance and building the facet. If I comment out the test for empty results everything works as expected. I assume there is a good reason for not building the entire facet if its empty.
** edit
Here's a bit of a work around for those facing a similar issue. It might also just be useful for anyone that might want to build some custom facet results.
Comment #17
andypostCould you extract $(this) into variable?
why this class added without condition?
Comment #18
akalam CreditAttribution: akalam at Metadrop commentedThe Drupal behaviours are invoked on ajaxRequest, so no need to do this twice. Also, don't need to show the block, just to hide it.
Taking the andypost recomendation and saving $(this) to a variable.
Comment #19
akalam CreditAttribution: akalam at Metadrop commentedSorry, wrong patch.
Comment #20
akalam CreditAttribution: akalam at Metadrop commentedComment #21
geek-merlinIS:
Either this is outdated or i do not get it, but current code in \Drupal\facets\FacetManager\DefaultFacetManager::build render empty divs:
So either this is outdated or needs clarification.
Comment #22
geek-merlinCrosslinking a similar issue that i am facing, with a fixing patch.
I checked that this patch does not fix the other issue, so not a dup.
You might want to check if the other patch fixes any of the problems faced here (which i tbh do not fully understand, see above).
In any case, both issues touch the same code.
Comment #23
RoSk0Just faced this issue.
Facets "Empty facet behavior" configured to "Do not display facet" but issue #2826449: Ajax Facets blocks for views introduced this code, mentioned in #21, which makes facet block be rendered always.
Patch fixes issue, tested on 1.3.
Comment #24
lahoosascoots CreditAttribution: lahoosascoots commentedI'm not sure we can call this fixed and would be a workaround at best. The facets are hidden with jQuery and are seen on the screen at page load before they are hidden. This is not a good user experience. Is there any other way we could do this? Maybe via using a hidden class attribute and css?
Comment #25
lahoosascoots CreditAttribution: lahoosascoots commentedNot elegant but this works to kill the flickering for now if it bothers anyone else.
Comment #26
nedjoAttached is a patch that starts fresh, following #25. Rather than working with JavaScript, we process the block prior to rendering and conditionally hide it.
Comment #27
borisson_Not doing this with javascript seems like a better way to do this, so #26 looks like a good start, since this does not add additional testcoverage, we should probably at least get more +1's, but I'd prefer an extra testcase.
Comment #28
FeyP CreditAttribution: FeyP as a volunteer and at werk21 commentedI'm sorry, if I missed anything, but as far as I can see,
Drupal\facets\FacetManager\DefaultFacetManager::build()
adds thefacet-empty
class to all facets with an empty result set regardless of the configured empty behavior.We want to hide the block, if the empty behavior is configured to
none
, but if the behavior is configured totext
we want to show that text, right? I guess we shouldn't hide the block then. So the check whetherfacet-empty
exists in patch #26 is probably not enough.Comment #29
Nick_vhWhat Feyp said is actually correct. I tried to reproduce this and indeed, the facet hides itself even though the empty facet behavior is not respected anymore. Let's try to fix this.
Comment #30
Nick_vhComment #31
Nick_vhComment #32
estoyausenteTested and seems ok!
Comment #34
estoyausenteComment #35
borisson_Committed and pushed.
Comment #37
joshua.boltz CreditAttribution: joshua.boltz at Mediacurrent commentedThis was exactly what I needed for a use case i was seeing where i placed a facet block via layout builder and even if there were no facets to use and drill down the results for, the title was still being displayed. This patch solved that!
Comment #38
bmcclure CreditAttribution: bmcclure as a volunteer and at Top Floor commentedSince this fix has been on the dev branch for a year already, it would be great to get a new release version of facets so that those not using the dev branch can benefit from this.
Comment #39
berliner CreditAttribution: berliner commentedThis is part of https://www.drupal.org/project/facets/releases/8.x-1.5