1

I'm setting up a new Drupal site and created a content type, taxonomy, and two menus. The taxonomy just has 2 items, which correspond to the menus. What I'd like to do is render the corresponding menu in a sidebar when the user is viewing a node from the content type, ideally highlighting the current item in the menu.

My current thinking is to write a custom module and use the hook_page_build. Then in that hook, see if arg(0) is 'node' and arg(1) is numeric and then node_load arg(1) and then if that node's type matches my content type, then field_get_items to get the node's taxonomy and pull the corresponding menu and then add it to $page['sidebar_first'].

Is this the right approach or is there a better way?

Thanks in advance!

1 Answer 1

1

I'd say the best way would be to use Menu Block. This would allow you to use blocks built in visibility rules.

All you would need to do is create a new menu block, select which menu to display and select which content type it should be displayed on. The menus get all the functionality of standard menus.

You could also just use the standard block that is created with every menu, check the disabled blocks section for those. Menu Block however has a few more features.

2
  • Thanks, Patrick! This looks really close to what I'm looking for. However, I have two menus and I only want to show the menu that corresponds to the taxonomy term that the node belongs to. I could create two different content types, but that seems a waste. Or maybe there's a hook for hiding the block when viewed on the node that has the different taxonomy term.
    – Purdy
    Commented Jun 4, 2015 at 17:27
  • Ok, I got it figured out. There's an option in Menu Block to select the menu selected by the page, which is really cool. Thanks again, Patrick!
    – Purdy
    Commented Jun 4, 2015 at 17:59

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.