doudi2005 2015-01-15 23:00
浏览 23
已采纳

Drupal:将菜单子项的链接更改为锚点

My goal is to modify the output of a menu so that the submenu links are anchors of the parent item:

<ul class="menu">
  <li>
    <a href="page1">Page 1</a>
    <ul class="submenu">
      <li>
        <a href="page1#section">Section</a>
      </li>
    </ul>
  </li>
</ul>

Because the Drupal GUI doesn't allow anchors in menu items, I need to modify the theme_link() function to change all children:

function MYTHEME_menu_link__menu_block__main_menu(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';

  if ($element['#below']) {

    foreach($element['#below'] as $child) {
      $child['#href'] = str_replace(' ', '-', strtolower(render($child['#title'])));
    }

    $sub_menu = drupal_render($element['#below']);
  }

  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>
";

}

But there are two problems, which are a result of my lack of experience with Drupal functions:

  1. The changed href is not being passed to the rendered HTMl.
  2. I receive this error for line 8: Warning: Cannot use a scalar value as an array.

What do I need to correct this error, and how can I pass the new href to the menu-children's output link?

EDIT: I should add that I’m using Entity Reference to pull nodes into one super page, and there are three super pages on the site. So using a single-page solution will not work for me.

  • 写回答

2条回答 默认 最新

  • duaiwu8385 2015-01-16 21:38
    关注

    After much trial and error, I decided to use a non-PHP solution for now. I’m using JavaScript to change the submenu-children's hrefs on page load, which is not necessarily the best solution, but the most feasible right now. It seems like Drupal’s menu functions are better served for modifying the markup and attributes of the menu items, rather than the menu's link attributes. If I return to this issue and come up with a PHP solution, I’ll post it here.

    Even though this doesn’t answer my original questions, I’m marking it as the accepted answer because it provides the end result I trying to accomplish.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效