I have a question for you.
I'm trying to make a website with drupal 7, it's almost done except for some little problems, I need that the first level of the main menu had some custom css classes in order to integrate it to columnal, this is the way I print the main menu:
<?php
print theme(
'links__system_main_menu',
array(
'links' => $main_menu,
'attributes' => array(
'id' => 'main-menu',
'class' => array('menu')
)
)
);
?>
and this is what I get:
<ul id="main-menu" class="menu">
<li class="menu-NNN first active">
<a href="url" title="" class="active">Home</a>
</li>
<li class="menu-NNN">
<a href="url">click me</a>
</li>
<li class="menu-NNN">
<a href="url">click me</a>
</li>
<li class="menu-NNN">
<a href="url">click me</a>
</li>
<li class="menu-NNN last">
<a href="url">click me</a>
</li>
</ul>
I need that the first level of the main menu has the class "col_1":
<ul id="main-menu" class="menu">
<li class="menu-NNN first .col_1 active">
<a href="url" title="" class="active">Home</a>
</li>
<li class="menu-NNN .col_1">
<a href="url">click me</a>
</li>
<li class="menu-NNN .col_1">
<a href="url">click me</a>
</li>
<li class="menu-NNN .col_1">
<a href="url">click me</a>
</li>
<li class="menu-NNN .col_1 last">
<a href="url">click me</a>
</li>
</ul>
the layout would look something like this:
/****************************************************************************************
* * .row * *
* ********************************************************************************* *
* * #logo.col_5 * * .pre_1| .col_5 |.suf_1 * *
* * * * | | * *
* * * * | | * *
* * * * |__________________________________| * *
* * *mar* |.col_1|.col_1|.col_1|.col_1|.col_1| * *
* * *gin*padding| menu1| menu2| menu3| menu4| menu5|padding* *
*mar*********************************************************************************mar*
*gin* *gin*
****************************************************************************************/
//I'm expecting to have just 5 links in the main menu
maybe I can set the width and the margin manually with css but I think this isn't the right approach, so, "using CSS to set properties to every sub element" won't work, I also try using "MYTHEME_menu_link" but that adds the properties to all the menus but the main menu, so that won't work too.
so the question is:
is there another way to add css classes to the first level of the main menu in drupal?
After some investigation. I found a way to achieve my goal without using a workaround or any external module based on this article (HOWTO create dropdown menus for Bartik in Drupal 7), I used 'variable_get', 'menu_tree' and 'drupal_render' to override the variable 'main_menu' with a string containing the menu with the format I needed, to override the variable 'main_menu' I used 'THEME_process_page';
function MYTHEME_process_page(&$variables) {
global $language;
if($variables['main_menu']){
$tree = menu_tree(variable_get('menu_main_links_source', 'main-menu'));
foreach($tree as $key => $val){
if(isset($tree[$key]['#href'])){ //just to make sure this is a link
if($language->language!=$tree[$key]["#localized_options"]["langcode"])
unset($tree[$key]);
else
$tree[$key]['#attributes']['class'][] = 'col_1';
}
}
$variables['main_menu'] = drupal_render($tree);
}
}
Before this modification the 'main_menu' was an array, but now it is a string containing the menu, so, now, I have to replace the function 'theme' in the tamplate.
<?php
print theme(
'links__system_main_menu',
array(
'links' => $main_menu,
'attributes' => array(
'id' => 'main-menu',
'class' => array('menu')
)
)
);
?>
With something like this;
<?php print $main_menu; ?>