Hide a view link when no results


Imagine ... you have a view that displays the latest news. You created therefore also a menu link in the main menu.

What happens if there is no news (eg not published because it is outdated)? The menu link remains in the main menu. By clicking you get ... nothing. Painful for a good Drupal site.

You can use hooks, but I do this in a different way ...

I take in the same view a block. This block has in the setting 'no results' a text that contains the CSS code to hide the appropriate link.

Eg. I have a view that shows all nodes of the content type .. Article


And I make a menu link


Now I create a block in the same view. I limit the output with 1 field. This field must be invisible. If there is content I must not see the view in the block. I see the view on the page already.


Because you can not exclude the only field of view, I rewrite it by placing a space. If you do not enter any character, the field will still be displayed.


Now comes the magic ... We use 'behavior in the absence of results. Add a text field.


Place the code in the block to hide the specific menu link. Every menulink has an ID.

Place the block on each page. Is there is content in the view, the menu link will be visible. Otherwise, the CSS code will be generated and the link is invisible. Shore.. the site will slow down a fraction of a second because it checks the view for results.