Problem/Motivation
On a block display with Hide block if the view output is empty set to Yes, a Global: View area in No results behavior keeps the block visible when the main and embedded views are both empty.
Block::execute() calls outputIsEmpty() before the areas are rendered, so isEmpty() in Drupal\views\Plugin\views\area\View responds with isEmpty = FALSE before render() sets $isEmpty. It is still NULL, and the method falls back to AreaPluginBase::isEmpty().
Only the Global: View plugin overrides isEmpty(). Otherwise, AreaPluginBase::isEmpty() assumes there is something to render when the plugin is set to show for no results: always in No results behavior, where init() forces the option on, and in a header or footer when Display even if view has no result is checked. So a Global: View area in a header or footer with that option checked is affected too. Other area plugins that render nothing, such as Content: Empty Node Frontpage behavior (see the steps to reproduce), are left to a follow-up.
Hiding the block only when the No results output is empty is valid configuration (comment #21), but the label text could be improved (comments #6 and #7).
Steps to reproduce
Global: View area
- Configure a view with a block display that returns no results.
- Set Hide block if the view output is empty to Yes.
- Add a Global: View area to No results behavior, embedding a view display that also returns no results.
- Place the block in the layout.
The block is still displayed, though both views are empty.
Other area plugins (follow-up)
- Configure a view with a block display that returns no results.
- Set Hide block if the view output is empty to Yes.
- Add Content: Empty Node Frontpage behavior to No results behavior.
- Place the block in the layout, and view the page as an anonymous user.
The block is displayed to anonymous users, but its "Add content" link is not, because access hides it, so the block is empty. This plugin does not override isEmpty(), so it is not fixed here.
Proposed resolution
When View::isEmpty() is asked before render() has run, render the embedded view then, which sets $isEmpty, and keep the output. render() returns the kept output, so the embedded view is executed only once. An embedded view that is never built, because no view is selected, the user cannot access it, or it would recurse, is reported as empty.
Remaining tasks
Rewrite MR, Move the test code into the existing AreaViewTest.
| Comment | File | Size | Author |
|---|---|---|---|
| views-view-area-hide-block.patch | 701 bytes | andy_w |
Issue fork drupal-2811795
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
dawehnerThank you for your patch. Did you tried writing a test for it?
Comment #3
andy_w commentedI didn't as I ran out of time, but I will when I get a chance.
Comment #4
dawehnerCool, @andywhale! Its certainly worth to post already the fix. Maybe someone else will pick it up.
Comment #6
lomo commented@andywhale: This is an interesting issue. I was planning to help with a test... but as far as I can tell, the two settings are simply in logical conflict. One is an override to provide some kind of content if there is nothing to display in the view and the other is to hide an empty view. That said, even with your patch applied, the block still shows up* until I remove the "No Results Behaviour". Then if "Hide block if the view output is empty:Yes" is enabled, the Views block is not displayed.
IMHO, this is less a bug than a quirk of Views. Or perhaps the bug is that we still allow the "Hide if empty" to be enabled if we have "No results behavior". The "No results behavior" means that the block is NOT empty, though, so "Hide if empty" no longer really applies, perhaps.
Anyway, I agree that the UX could be improved here, but I think it's a matter of having two settings that can be set in somewhat opposing manners and which setting takes precedence should be more clear to users. Or it should not be possible to have both settings enabled in this opposing manner. I think the "bug" is more to do with VIew UI and what the user experience should be here could bear further discussion. I would personally suggest disabling the "Hide block if the view output is empty:Yes" option if "No results behavior" provides some other kind of output. Users should then see that "Hide block" option will be available if they remove the "No results behavior". Note that there are a lot of options for the "No results behavior", so it's possible that there is a use case where these two options might NOT be in conflict (I am not sure, and I can't think of one... but just saying... ;-) )
* The block I used to test this was the standard "Recent content" block, which already has default text displayed if the view is empty. Enabling the "Hide block if the view output is empty" setting, with this patch applied, still meant the block was displayed. (Yes, the View was saved and the caches cleared). Then if I removed the "No results behavior", the block title was still displayed if "Hide if empty" was not enabled, but was correctly hidden if "hide if empty" was enabled. The attached patch did not appear to make any difference for any scenario I tried.
Comment #7
lendude@LoMo thanks for all the testing!
I agree that this is not a bug so much as unclear UX, so setting to task.
Comment #18
smustgrave commentedThank you for creating this issue to improve Drupal.
We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.
Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.
Thanks!
Comment #20
smustgrave commentedWould be good to confirm if this is still an issue. May be a bug.
Comment #21
mortona2k commentedI think it's a bug as reported, and UX confusion is separate.
The config is valid: hide block if output is empty, display view in place of empty results.
If the outer view is empty, render the inner view. If the inner view is empty, hide the block.
The problem is that the outer view is processing empty markup and considering it not empty, regardless of whether the inner view has results.
Clarifying the settings could be helpful, but this specific config is not working as intended.
The Recent Content block tested in #6 is not the target of this patch, but indicates there are other area plugins to check.
*I used claude to analyze the issue and build demo views to verify.
Comment #23
mortona2k commentedMR adds a test and fixes 2 issues in the patch:
Early return was not setting $isEmpty when no view selected, user has no access, or recursive inner view.
Inner view rendered twice, by isEmpty() and #pre_render.
*Claude used to extend the patch.
Comment #24
smustgrave commentedWill need an IS update
We should also look at expanding a new test vs adding a new one. Also seems like AI touched too much.
Also MR message is reading like pure AI, we don't want AI messages in the MR or it pushing code if that was the case.
Comment #25
smustgrave commentedThis is also one I'd like to get a human to review and verify please.
Comment #26
mortona2k commentedComment #27
mortona2k commentedI revised the summary and the patch, but have not yet touched the test yet.
The patch itself is much clearer IMO. What needs validating is whether building the embedded view inside isEmpty() is an acceptable fix.
An alternative approach is to fix this in the block display, build the areas in Block::execute() before checking outputIsEmpty().
Comment #28
mortona2k commentedThis exact issue is not really impacting me. I was trying to build something convoluted with views to render a field and ended writing a custom field formatter for it instead.
I still think there are some quirks here around checking for empty plugin markup without pre rendering. Some of them may be things you should not do with views.
One example, you could have a chain of no results global: view areas, where one view includes another, which includes another. Is there a programmatic way for the plugin to check if empty without rendering? I also suspect there are issues with access checks and caching as well. For example if one of the views has a button in the header for admins only.
My motivation was to help tighten up views so there are fewer issues for new site builders. Someone could potentially encounter this issue again, but the right solution may to build something custom instead of configuring a view.
#6 may have been the right call, disabling the hide block if empty option when no results behavior is set. That establishes a known limitation and prevents setting broken config.