0

I want to display a block only on a particular menu-link.

For example I have a main menu link name: about us and the link: /about

If I restrict the block based on pages and give link as /about , In future the client may change the menu link as /about-us.

So instead of restricting it based on pages, is there any other module which restrict block visibility based on menu's or How can I custom code it?

  • How code should know what menu item was used to access given page? What about bookmark? How would you retrieve information about menu link used, if it was used month ago, and then page was bookmarked by your user? – Mołot Jun 29 '15 at 8:04
  • Can't we get add a section named menu like pages, roles, users in visibility. add display all menu links of a particular menu as check boxes. Are you saying that this is not feasible? I can't understand what are you talking about bookmarks. – user46121 Jun 29 '15 at 8:10
  • 1
    I meant that HTTP request does not have any info about menu item, only about path user wants. And keeping menu history in session is not reliable, because user can request page without using menu at all, for example of he bookmarks it. Storing in database is not feasible for anonymous users, so what's left? – Mołot Jun 29 '15 at 8:18
1

I created a contrib module Blocks per menu that you could use for this purpose.

Blocks per menu extends the block visibility interface in Drupal 7 and
allows the user to control block visibility based on the menu links.

Block visibilty settings

The module page also states:

  • when the module is enabled, a new vertical tab is added to the block configure admin settings as shown in the snapshot.

  • If only a parent menu is selected and no child menu item is selected, then the restriction is applied to all the child menu links.

1
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $active_trail = menu_get_active_trail();
  end($active_trail);
  $nid = 1; // change to nid of parent node you want to check
  if ($active_trail[1] && $active_trail[1]['link_path'] == 'node/'.$nid) {
    return true;
  } else {
    return false;
  }
}  

You can use the above code to achieve it in Drupal 7.

0

Use the Context module for what you're wanting. Add a new context (Structure > Context > Add), select a Menu condition with the menu item on which you want the block to appear, then select a Block Reaction with the selected block you want displayed and the region to display it in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy