Hide a view link when no results

Rating

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

Screen002047.png

And I make a menu link

Screen002053.png

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.


Screen002048.png

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.

Screen002052.png

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


Screen002049.png

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

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.