bradrice
bradrice

Reputation: 1755

Drupal 7 - Programmatically switch menus on site

In Drupal 7 I want to have a site that has distinct sections with a left navigation menu that switches based upon the page you are on. For example I want to have a top level nav that takes you to a section, and then in that section the menu is unique for it. I'm assuming I have to create a theme function in my template.php file to handle this. Can someone give me an idea of how to start coding this?

Upvotes: 1

Views: 1169

Answers (2)

amateur barista
amateur barista

Reputation: 4520

Instead of using @SpaceBeers suggestion, I would resolve your problem using Context:

  • I would define one different menu for each unique page.
  • Using Context, you can then associate each section with a menu. You would define a separate context for each section.
  • Inside the Context, you would specify that section's relative path in the Context's rules. Then on the Context reaction you would select the Drupal block that is automatically created by your menu.

As a plus, because Contexts are exportables, you can package them up into a Feature, commit that Feature into your source control repository, and deploy that to your DEV, QA and PROD boxes without having to manually copy the menu/block positioning settings from server to server. In other words, the Context + Features solution will provide for a more robust and scalable solution in the long run.

Upvotes: 0

SpaceBeers
SpaceBeers

Reputation: 13947

I'm not 100% sure I follow you but if I do then you could do this with the Menu Block module - http://drupal.org/project/menu_block

If you're talking about levels:

If you've got a menu with multiple levels but only want to show say the 2nd level on pages where the parent item has a 2nd level then you go to structure -> blocks -> new menu block and the select the menu you want, what menu level to start the menu from and then add it where you want as a regular block.

An example is:

    <ul class="menu">
       <li>Menu item 1</li> 
       <li>Menu item 2</li> 
          <ul class="menu"> 
             <li>Menu item</li> 
             <li>Menu item</li> 
          </ul> 
       <li>Menu item 3</li> 
       <li>Menu item 4</li> 
       <li>Menu item 5</li> 
    </ul>

Your main menu will show just menu items at level one.

Your menu block will be set to just start from level two, so if you were on Menu item 2's page, your menu block would display the second level menu items.

If you're not talking about levels:

You can make separate menu blocks and then set them to only appear on certain pages by going to the block's visibility settings, then typing the node alias of the page you want it to appear on and select "Only the listed pages" from the radio buttons.

Upvotes: 4

Related Questions