For a customer project we had the requirements to set the active menu item (if selected) to first of all menu items dynamically.
This was quite tricky, because it was a little hard to find the right hook / theme hook to use. Finally we decided to use theme_preprocess_page within Adaptive Theme.
Here is our code snippet which may help you.
IMPORTANT NOTICE: If you don't use adaptive theme, you might not need to rebuild the $vars['primary_navigation'] but use another template menu variable instead!
function MYTHEME_preprocess_page(&$vars) {
//#webksde: Set active menu item first!
if(!empty($vars['main_menu'])){
foreach($vars['main_menu'] as $menu_item_id => $menu_item_props){
// Set to first, if menu item is active (key contains "active-trail" string)
if(strpos($menu_item_id, 'active-trail')) {
$prepend = array($menu_item_id => $menu_item_props);
$vars['main_menu'] = array_merge($prepend, $vars['main_menu']);
break;
}
}
$vars['primary_navigation'] = theme('links', array(
'links' => $vars['main_menu'],
'attributes' => array(
'class' => array('menu', 'primary-menu', 'clearfix'),
),
'heading' => array(
'text' => t('Main menu'),
'level' => 'h2',
'class' => array('element-invisible'),
)
));
}
}
?>
Have fun using this snippet and leave a comment if it was helpful :)