Commit 1b990c19 authored by develCuy's avatar develCuy

by citlacom: set the active trail class.

parent c14e2021
......@@ -370,7 +370,8 @@ function menu_token_form_menu_edit_item_alter(&$form, &$form_state) {
);
if (isset($default) && $handler = menu_token_get_handler($default)) {
if ($append = $handler->form_options($options['menu_token_data'][$type]['options'])) {
$type_options = isset($options['menu_token_data'][$type]['options']) ? $options['menu_token_data'][$type]['options'] : array();
if ($append = $handler->form_options($type_options)) {
$form['menu_token_options'][$type]['menu_token_method_options_wrapper']['menu_token_method_options'] = array(
'#type' => 'fieldset',
'#title' => t('Method options'),
......@@ -495,6 +496,121 @@ function menu_token_method_callback($form, $form_state) {
return drupal_array_get_nested_value($form, $parents);
}
/**
* Preprocessor for menu_link used to fix the issue of in_active_trail for
* parent menus of active child elements. The parents never get active because
* the function menu_link_get_preferred($active_path, $menu_name) look in
* menu_links table via the link_path that for menu items using tokens will be
* <front> and never will find the active elements.
*/
function menu_token_preprocess_menu_link(&$variables) {
$l = &$variables['element']['#original_link'];
// If there are children, but they were not loaded, load them.
if ($l['has_children'] && !$variables['element']['#below']) {
$variables['element']['#below'] = menu_token_menu_subtree($l['menu_name'], $l['mlid']);
}
// iterate over childs and look if there is an active element to set
// correcty the in_active_trail of the parent menu
if (!$l['in_active_trail'] && isset($variables['element']['#below'])) {
foreach ($variables['element']['#below'] as &$child_element) {
if (isset($child_element['#original_link'])) {
$path = $child_element['#original_link']['link_path'];
// Append active class.
if (($path == $_GET['q'] || ($path == '<front>' && drupal_is_front_page()))) {
// add styles to parent
$variables['element']['#attributes']['class'][] = 'active-trail';
$l['in_active_trail'] = TRUE;
// add styles to child
$child_element['#attributes']['class'][] = 'active-trail';
$child_element['#original_link']['in_active_trail'] = TRUE;
}
}
}
}
}
/**
* Traverses the menu tree and returns the sub-tree of the item
* indicated by the parameter. (cloned from dhtml_menu module)
*
* @param $menu_name
* The internal name of the menu.
* @param $mlid
* The menu link ID.
*
* @return
* The tree below the menu item, as a renderable array, or an empty array.
*/
function menu_token_menu_subtree($menu_name, $mlid) {
static $index = array();
static $indexed = array();
// This looks expensive, but menu_tree_all_data uses static caching.
$tree = menu_tree_all_data($menu_name);
// Index the menu tree to find ancestor paths for each item.
if (!isset($indexed[$menu_name])) {
$index += _menu_token_index($tree);
$indexed[$menu_name] = TRUE;
}
// If the menu tree does not contain this item, stop.
if (!isset($index[$mlid])) {
return array();
}
// Traverse the tree using the ancestor path.
foreach ($index[$mlid]['parents'] as $id) {
$key = $index[$id]['key'];
if (isset($tree[$key])) {
$tree = $tree[$key]['below'];
}
else {
return array();
}
}
// Go one level further to go below the current item.
$key = $index[$mlid]['key'];
return isset($tree[$key]) ? menu_tree_output($tree[$key]['below']) : array();
}
/**
* Indexes the menu tree by mlid. This is needed to identify the items
* without relying on titles or stacks. This function is recursive.
* (cloned from dhtml_menu module)
*
* @param $tree
* A tree of menu items such as the return value of menu_tree_all_data().
* @param $ancestors
* Optional, used only by internal recursion.
* @param $parent
* Optional, used only by internal recursion.
*
* @return
* An array associating mlid values with the internal keys of the menu tree,
* and all the mlids of the item's ancestors.
*/
function _menu_token_index($tree, $ancestors = array(), $parent = NULL) {
$index = array();
if ($parent) {
$ancestors[] = $parent;
}
foreach ($tree as $key => $item) {
$index[$item['link']['mlid']] = array(
'key' => $key,
'parents' => $ancestors,
);
if (!empty($item['below'])) {
$index += _menu_token_index($item['below'], $ancestors, $item['link']['mlid']);
}
}
return $index;
}
/**
* The menu token handler interface should be implemented by all menu token
* plugins.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment