Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add css class to "main menu" (drupal, integration with columnal grid system)

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?

like image 460
MARP Avatar asked Jan 23 '26 11:01

MARP


1 Answers

If you are looking to achieve the same goal with your own module or your theme's template.php file, you can use your own implementation of drupal's theme_menu_link()

Sample code:

function [THEME_OR_MODULE]_menu_link(array $variables) {
  $element = $variables['element'];
  $sub_menu = "";

  $element['#attributes']['class'][] = "col_1";

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}
like image 69
Muhammad Reda Avatar answered Jan 26 '26 03:01

Muhammad Reda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!