简体   繁体   English

Drupal 7 - 以编程方式在现场切换菜单

[英]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. 在Drupal 7中,我希望有一个具有不同部分的站点,左侧导航菜单根据您所在的页面进行切换。 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. 我假设我必须在template.php文件中创建一个主题函数来处理这个问题。 Can someone give me an idea of how to start coding this? 有人能告诉我如何开始编码吗?

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 我不是100%肯定我会关注你但是如果我这样做你可以使用Menu Block模块执行此操作 - 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. 您的菜单块将设置为从第二级开始,因此如果您在菜单项2的页面上,您的菜单块将显示第二级菜单项。

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. 您可以创建单独的菜单块,然后将它们设置为仅显示在某些页面上,方法是转到块的可见性设置,然后键入要显示的页面的节点别名,并从单选按钮中选择“仅列出的页面” 。

Instead of using @SpaceBeers suggestion, I would resolve your problem using Context : 我没有使用@SpaceBeers建议,而是使用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. 在Context中,您可以在Context的规则中指定该部分的相对路径。 Then on the Context reaction you would select the Drupal block that is automatically created by your menu. 然后在Context反应中,您将选择由菜单自动创建的Drupal块。

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. 作为一个优点,因为上下文是可导出的,您可以将它们打包到一个功能部件中 ,将该功能部件提交到您的源控件存储库中,然后将其部署到您的DEV,QA和PROD框中,而无需手动复制菜单/块定位设置。服务器到服务器。 In other words, the Context + Features solution will provide for a more robust and scalable solution in the long run. 换句话说,Context + Features解决方案从长远来看将提供更强大和可扩展的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM