Is there a way of setting visibility of menu items itself based on the URL?

With the Panels module, we can set visibility of the panel section based on the URL or other rule. How can I set that kind of rule for individual items of a menu block?

For example, the menu block has the following items: Item-1, Item-2, Item-3, Item-4. If the URL is node/, display all items; if the URL is node/string-1, display Item-1, Item-2, Item-3; if the URL is node/string-2, display Item-4, item-5, Item-6.

1 Answer 1


Step 1

Create 3 blocks:

  • Block 1 containing links pointing to all items like Item-1, Item-2, Item-3, Item-4, ... etc.
  • Block 2 containing links pointing to all items Item-1, Item-2 and Item-3.
  • Block 3 containing links pointing to all items Item-4, Item-5 and Item-6.

Step 2

Enable the Rules block visibility module. Here is a quote from its project page:

The Rules block visibility module allows Rules components to be used to control block visibility. This provides Drupal administrators and developers extreme flexibility in controlling when blocks should be displayed on their websites, in addition to the default visibility options provided by Drupal.

The general idea is that if you can do it with Rules, you can use it to control block visibility, so the possibilities are limitless:

  • Need to show a block only for users registered more than a month ago?

  • Perhaps you have a block that must be shown only between 8am-5pm on weekdays?

  • What about displaying or hiding a block based on current weather conditions?

All of this can be done by using Rules block visibility.

For an illustration of how to use this module, refer to my answer to "How to stop a Views block from displaying to admins, such as user/1?".

Step 3

As per the "if you can do it with Rules, you can use it to control block visibility" above, you've reduced your question to making Rules "check the path of the current page" (so that the block is only shown for the listed pages).

So create 3 Rules Components, each with a Rules Condition that performs a Text comparison with Parameter: Text: [site:current-page:path] and an appropriate Matching Text for the URLs like:

  • (NOT node/string-1 AND node/string-2), to show Block 1.
  • node/string-1, to show Block 2.
  • node/string-2, to show Block 3.
  • This is the perfect answer for this question. Actually, I accidentally represented the question wrong way, which I realized after reading the answer. Actually what I want is, I want just one block where I can provide all menu items at once and after that I am able to set the rules on that list of items so that items gets displayed based on that rules. This solution I need to solve the my bigger menu problem, Question link. Thanks for your time on this, this also will help.
    – CodeNext
    May 10, 2016 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.