background
- I have a view of upcoming events I created for my client. (example.com/events)
- There is a link in my main menu to the view (events)
- I would like that link to disappear when the view is empty (i.e. typing in example.com/events to lead to a 404, and the menu item to disappear)
- Intuitively, I looked at "No Results Behavior" in the advanced section of my view, expecting there to be a "view access".
Feature request
- Under advanced > No Results Behavior, there could be a "view access" field.
- Selecting it could provide a checkbox to "hide entire view (hide block or page 404)"
- When the view exists and there are no results, typing in the field would lead to a "404 page".
I know it's possible to do this using views theme functions, but this is not related to the theme, and should work whichever theme I use.
If there is already a way of doing this and I haven't found it, my apologies.
Comments
Comment #1
ccw commentedI don't know about the response body. But getting a 404 Status code can be done (at least in 6.x) with PHP code "empty text" like:
You're free to make the content look like your "404 page". Not exactly what you ask for, but close.
See #652610: Time-based output caching is writing cache when it is set to "never cache" for cache issues I had when I used the standard header function instead of the drupal api, wrapper.
Comment #2
Anonymous (not verified) commentedNeed a similar feature, "hide page completely if there are 0 results". Also hide the menu link setup via Views. I have nodes with a Views tab for "Related content", which should hide when 0 results.
Solution from #1 indeed creates a 404, but does not remove the views tab.
Comment #3
merlinofchaos commentedThis is not possible with Views, and will not be possible.
The reason is this: Hiding the menu link means running the view whenever the menu link might be seen. That could mean running the view basically every page load. This is a major performance drain.
If you absolutely positively need this feature, you can implement this yourself using hook_menu_alter and changing the 'access callback' to something that will run the view, test to see if it has results, and act appropriately. However, Views will simply not offer this feature natively.
Comment #4
alberto56 commented@merlinofchaos thanks for the explanation
Comment #5
kurapov commentedFor future reference, to get the proper 404 page when the view returns zero results, I put the following in "Text if empty" field (Drupal 6, Views 3.0):
The snippet courtesy sylvain lavielle
Comment #5.0
kurapov commentedadded no results behavior