Drupal 6 - 在函数之间传递变量专门 - 将菜单名称传递给 phptemplate_menu_item

发布于 2024-12-19 11:00:07 字数 2170 浏览 1 评论 0原文

我对 Drupal 6 及其主题还很陌生。我的主题需要主菜单为我合理涉及的样式提供相当广泛的 html/css。为了实现这一点,我将这段代码“function phptemplate_menu_item”拼凑在一起(见底部),它位于我的 template.php 中,并根据菜单项是否有子项生成不同的 html,并使用链接的内容生成 css 类。

我的问题是这个代码也被应用到我的其他菜单。我想让 menu_item 生成不同的 html,具体取决于它是否在主菜单中。我本以为最简单的方法是在函数 phptemplate_menu_item 中使用 if 语句,例如:

function phptemplate_menu_item (...){
  if ($menu_name == 'primary-links')
  {DO ABC}
  else
  {DO XYZ}
}

但是我相信我需要知道如何将菜单名称传递给 phptemplate_menu_item 函数。任何有关这方面的帮助将非常感激,因为我已经用头撞墙试图解决这个问题有一段时间了。

谢谢!

function phptemplate_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {

$class = ($menu ? 'no_wrap' : ($has_children ? 'collapsed' : 'li_wrap'));
if (!empty($extra_class)) {
  $class .= ' '. $extra_class;
}
if ($in_active_trail) {
  $class .= ' active-trail';
}
if (!empty($link)) {
/* The following section gives the list items unique classes based on their link text - note how spaces and sepcial chars are removed */
// remove all HTML tags and make everything lowercase
$css_id = strtolower(strip_tags($link));
// remove colons and anything past colons
if (strpos($css_id, ':')) $css_id = substr ($css_id, 0, strpos($css_id, ':'));
// Preserve alphanumerics, everything else goes away
$pattern = '/[^a-z]+/ ';
$css_id = preg_replace($pattern, '', $css_id);
$class .= ' '. $css_id;
}
// the following code returns the menu item formatted in a different fashion depending on the class of the item. The first one is for items with a class of none - ie the space li at end of menu
if (strstr($class, 'none')) {
      return '<li class="'. $class . ' main"></span></span></li>';
}
if (strstr($class, 'li_wrap')) {
      return '<li class="'. $class .' main"><span class="wrapped">'. $link . $menu ."<span class='indicator'></span></li>\n";
}
if (strstr($class, 'no_wrap')) {
      return '<li class="'. $class . ' main">'. $link ."<span class='indicator'></span><span class='menu_box'><span class='menu_box_inner'>". $menu ."</span></span></li>\n";
}

}

I'm quite new to Drupal 6 and theming it. My theme required the main menu to pump out quite extensive html/css for my reasonably involved styling. To achieve this I pieced together this code 'function phptemplate_menu_item' (see bottom) which sits in my template.php and produces different html depending on the whether the menu item has children or not and uses the contents of the link to generate a css class.

My problem is that this code is also being applied to my other menus. I would like make the menu_item generate different html depending on whether it is in the primary-menu or not. I would have thought that the easiest way to do this is with an if statement in the function phptemplate_menu_item, for example:

function phptemplate_menu_item (...){
  if ($menu_name == 'primary-links')
  {DO ABC}
  else
  {DO XYZ}
}

However I believe I need to know how to pass the menu name to the phptemplate_menu_item function. Any help with this would be really appreciated as I have been banging my head against the wall trying to solve this for some time now.

Thanks!

function phptemplate_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {

$class = ($menu ? 'no_wrap' : ($has_children ? 'collapsed' : 'li_wrap'));
if (!empty($extra_class)) {
  $class .= ' '. $extra_class;
}
if ($in_active_trail) {
  $class .= ' active-trail';
}
if (!empty($link)) {
/* The following section gives the list items unique classes based on their link text - note how spaces and sepcial chars are removed */
// remove all HTML tags and make everything lowercase
$css_id = strtolower(strip_tags($link));
// remove colons and anything past colons
if (strpos($css_id, ':')) $css_id = substr ($css_id, 0, strpos($css_id, ':'));
// Preserve alphanumerics, everything else goes away
$pattern = '/[^a-z]+/ ';
$css_id = preg_replace($pattern, '', $css_id);
$class .= ' '. $css_id;
}
// the following code returns the menu item formatted in a different fashion depending on the class of the item. The first one is for items with a class of none - ie the space li at end of menu
if (strstr($class, 'none')) {
      return '<li class="'. $class . ' main"></span></span></li>';
}
if (strstr($class, 'li_wrap')) {
      return '<li class="'. $class .' main"><span class="wrapped">'. $link . $menu ."<span class='indicator'></span></li>\n";
}
if (strstr($class, 'no_wrap')) {
      return '<li class="'. $class . ' main">'. $link ."<span class='indicator'></span><span class='menu_box'><span class='menu_box_inner'>". $menu ."</span></span></li>\n";
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄司 2024-12-26 11:00:07

好吧,我认为解决方案在这里找到了最好的,所以我不会因此而获得荣誉。您应该从 theme_links 开始定义自定义函数 theme_primary_links,而不是覆盖 theme_menu_item。您可以选择您认为最适合您的自定义主题的任何名称(但请确保它尚未使用)。

底线:有时定义自定义主题比覆盖默认主题更容易。这样您就知道您可以根据您的需要严格应用自定义主题(例如仅应用于主菜单链接)。当您希望更改是全局的(例如应用于所有菜单链接)时,最好使用覆盖。

Well I think the solution found here it's the best so I'm not going to take credit for it. Instead of overwriting theme_menu_item you should define your custom function theme_primary_links starting from theme_links.You can choose whatever name you think it's best for your custom theme (but make sure that it's not already used).

Bottom line: sometimes it's easier to define your custom theme than overwriting the default ones. That way you know that you can strictly apply the custom theme according to your needs (e.g. applied only to the primary menu links). Overwriting is best used when you want your changes to be global (e.g. applied to all menu links).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文