题
我想添加菜单下拉到使用文本滑门CSS四舍五入一个Drupal主题。
当前版本使用跨度到一个标签,其正常工作的主链接注射。但不支持下拉菜单。
工作的代码:
<?php print theme('links', $primary_links, array('class' => 'links primary-links')) ?>
在与template.php文件中添加模板:
<?php
// function for injecting spans inside anchors which we need for the theme's rounded corner background images
function strands_guybrush_links($links, $attributes = array('class' => 'links')) {
$output = '';
if (count($links) > 0) {
$output = '<ul'. drupal_attributes($attributes) .'>';
$num_links = count($links);
$i = 1;
foreach ($links as $key => $link) {
$class = $key;
// Add first, last and active classes to the list of links to help out themers.
if ($i == 1) {
$class .= ' first';
}
if ($i == $num_links) {
$class .= ' last';
}
if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))) {
$class .= ' active';
}
$output .= '<li'. drupal_attributes(array('class' => $class)) .'>';
if (isset($link['href'])) {
$link['title'] = '<span class="link">' . check_plain($link['title']) . '</span>';
$link['html'] = TRUE;
// Pass in $link as $options, they share the same keys.
$output .= l($link['title'], $link['href'], $link);
}
else if (!empty($link['title'])) {
// Some links are actually not links, but we wrap these in <span> for adding title and class attributes
if (empty($link['html'])) {
$link['title'] = check_plain($link['title']);
}
$span_attributes = '';
if (isset($link['attributes'])) {
$span_attributes = drupal_attributes($link['attributes']);
}
$output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
}
$i++;
$output .= "</li>\n";
}
$output .= '</ul>';
}
return $output;
}
?>
所以,我已经添加了尼斯菜单模块效果很好,并允许下拉菜单功能我的导航其现在从模板使用解决:
<?php print theme_nice_menu_primary_links() ?>
在的问题是,一个标签需要有内部跨度,以允许所选择的状态标记。我曾尝试每一个角度,我能找到编辑Drupal的功能menu_item_link这是用来通过漂亮的菜单来建立链接。
E.g。我看着Drupal的论坛上两天,没有喜悦。
模块在建立链接的线是:
function theme_nice_menu_build($menu) {
$output = '';
// Find the active trail and pull out the menus ids.
menu_set_active_menu_name('primary-links');
$trail = menu_get_active_trail('primary-links');
foreach ($trail as $item) {
$trail_ids[] = $item['mlid'];
}
foreach ($menu as $menu_item) {
$mlid = $menu_item['link']['mlid'];
// Check to see if it is a visible menu item.
if ($menu_item['link']['hidden'] == 0) {
// Build class name based on menu path
// e.g. to give each menu item individual style.
// Strip funny symbols.
$clean_path = str_replace(array('http://', '<', '>', '&', '=', '?', ':'), '', $menu_item['link']['href']);
// Convert slashes to dashes.
$clean_path = str_replace('/', '-', $clean_path);
$class = 'menu-path-'. $clean_path;
$class .= in_array($mlid, $trail_ids) ? ' active' : '';
// If it has children build a nice little tree under it.
if ((!empty($menu_item['link']['has_children'])) && (!empty($menu_item['below']))) {
// Keep passing children into the function 'til we get them all.
$children = theme('nice_menu_build', $menu_item['below']);
// Set the class to parent only of children are displayed.
$class .= $children ? ' menuparent ' : '';
// Add an expanded class for items in the menu trail.
$output .= '<li id="menu-'. $mlid .'" class="'. $class .'">'. theme('menu_item_link', $menu_item['link']);
// Build the child UL only if children are displayed for the user.
if ($children) {
$output .= '<ul>';
$output .= $children;
$output .= "</ul>\n";
}
$output .= "</li>\n";
}
else {
$output .= '<li id="menu-'. $mlid .'" class="'. $class .'">'. theme('menu_item_link', $menu_item['link']) .'</li>'."\n";
}
}
}
return $output;
}
正如可以看到$输出使用menu_item_link解析数组链接和对类的活性添加到所选择的导航链接。
现在的问题是如何添加一个跨度的一个标签内部或我怎么裹一个标签与具有主动类的样式滑动门链接的跨度?
解决方案
如果您想用一个跨度来包装一个标签,可以覆盖theme_nice_menu_build
和您的跨度添加到输出。如果你想给一个标签内你需要覆盖menu_item_link
。
您可以通过创建覆盖一个主题功能可按一个函数调用your_theme_name_function_name
和Drupal将使用该功能来呈现的标记,而不是默认的一个。这样,你可以改变的标记,你想要的任何方式。这个功能应该是在你的主题的template.php文件中。
开始的一个好方法是复制要覆盖,只是改变你的喜好的功能。
由于Drupal的4.7发生了很多事情,我不希望你使用。这是很容易插入span标签:
function your_theme_name_menu_item_link($link) {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
$link['localized_options']['html'] = TRUE;
return l('<span>' . $link['title'] . '</span>', $link['href'], $link['localized_options']);
}
我测试这一点,它工作得很好。
不隶属于 StackOverflow