I have a counter of menu items, that basically add incremental value as a class to a menu system:
<?php
if ($element['#original_link']['depth'] == 1) {
static $counter = 0;
$counter++;
$class = 'count-' . $counter;
}
$output = 'some output code build';
return '<li class="' . $class . '">' .$output .'</li>';
?>
Note, the code is inside each menu item (outside a loop or array). The code will simply output the lists of unordered list, without UL:
<li class="count-1">One</li>
<li class="count-2">Two</li>, ...etc.
This works fine until I change the source of menus.
1). One is using the system menu of my CMS,
2). the latter is using a block output of that system menu,
and both output similar construct of menus, except for the fact that the latter continues the counter from the #1 rather than a reset from a 1 (although #1 is not activated once #2 takes over the place). I haven't figured out why, but it seems that #2 is a continuance of #1. While I expect each should start an increment from 1.
I can not use a reset here. Is there anything obvious I missed that PHP can handle here, apart from the the way of my CMS do to the counters?
Any pointer would be very much appreciated. Thanks
UPDATE, actual codes in use:
function mytheme_menu_link(array $variables) {
//dpm($variables);
$element = $variables['element'];
$sub_menu = '';
if ($element['#below']) {
$sub_menu = drupal_render($element['#below']);
}
if ($element['#original_link']['menu_name'] == variable_get('menu_main_links_source', 'main-menu')) {
if ($element['#original_link']['depth'] == 1) {
static $counter = 0;
$counter++;
$element['#attributes']['class'][] = 'count-' . $counter;
}
}
$output = l($element['#title'], $element['#href'], $element['#localized_options']);
return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>
";
}
/**
* Implements theme_menu_tree()
No helpful variables, except for a flattened render of tree
*/
function mytheme_menu_tree__main_menu($variables) {
return '<ul class="menu">' . $variables['tree'] . '</ul>';
}