<li class="middle odd sf-item-7 sf-depth-1 
mymenutitle
 sf-total-children-1 sf-parent-children-0 sf-single-children-1 menuparent" id="menu-331-1">
<a data-thmr="thmr_99" class="sf-depth-1  menuparent" href="mytestsite/node/6">
mymenutitle
</a>
</li>

I want to add menu title as css class name to li as shown in above code. It is easier to remember css class rather than ids.

Thanks
Akki

CommentFileSizeAuthor
#1 superfish-1961212-1.patch1.25 KBdiscipolo

Comments

discipolo’s picture

Version: 7.x-1.8 » 7.x-1.x-dev
Status: Active » Needs review
StatusFileSize
new1.25 KB

initial stab at this, would probably need a config option as well as preserve alphanumerics and remove colons

mehrpadin’s picture

Hey everybody,

Apologies for missing this; I don't think it's a good idea, because titles are usually too long for this, and often not so static like the IDs are, let alone the i18n and so on; but you can easily add them, for example: http://kahthong.com/2011/12/themer-friendly-menu-link-css-classes-drupal...

discipolo’s picture

i tried that, following https://drupal.org/node/345624 but it was tricky, http://drupal.stackexchange.com/questions/16153/alter-menu-structure-whe.... Also menu titles dont tend to be that long in my experience, translation point taken though.

The Superfish module bypasses the usual Drupal theming hooks for menus and implements its own. You will need to override the theme_superfish_build() function in order to change the HTML around those menu items.

mehrpadin’s picture

No longer, we now have these two: theme_superfish_menu_item & theme_superfish_menu_item_link that you can easily override; also those wishing better performance can easily disable these via block configuration (under More Options) and so on, yes haven't updated the documentation yet!

discipolo’s picture

does that mean that it should also be possible to add the class via a preprocess function since it looks like theme_superfish_menu_item line 15 adds the classes to the li element using drupal_attributes($element['attributes'])? What would be the right way to extend the classes there?
none of those two seem to be using theme_menu_link...

mehrpadin’s picture

Yes, just mix my example above with the theme hook and you'll get:

function bartik_superfish_menu_item($variables) { // bartik just as an example
  $element = $variables['element'];
  $properties = $variables['properties'];
  $sub_menu = '';

  // Add classes to menu items for styling.
  $menu_class = strtolower(drupal_clean_css_identifier($element['item']['link']['title']));
  if ($menu_class) {
    $element['attributes']['class'] .= ' menu-' . $menu_class;
  }

  if ($element['below']) {
    $sub_menu .= ($properties['megamenu']['megamenu_content']) ? '<ol>' : '<ul>';
    $sub_menu .= isset($properties['wrapper']['wul'][0]) ? $properties['wrapper']['wul'][0] : '';
    $sub_menu .= $element['below'];
    $sub_menu .= isset($properties['wrapper']['wul'][1]) ? $properties['wrapper']['wul'][1] : '';
    $sub_menu .= ($properties['megamenu']['megamenu_content']) ? '</ol>' : '</ul>';
  }

  $output = '<li' . drupal_attributes($element['attributes']) . '>';
  $output .= ($properties['megamenu']['megamenu_column']) ? '<div class="sf-megamenu-column">' : '';
  $output .= isset($properties['wrapper']['whl'][0]) ? $properties['wrapper']['whl'][0] : '';
  if ($properties['use_link_theme']) {
    $link_variables = array(
      'menu_item' => $element['item'],
      'link_options' => $element['localized_options']
    );
    $output .= theme('superfish_menu_item_link', $link_variables);
  }
  else {
    $output .= l($element['item']['link']['title'], $element['item']['link']['href'], $element['localized_options']);
  }
  $output .= isset($properties['wrapper']['whl'][1]) ? $properties['wrapper']['whl'][1] : '';
  $output .= ($properties['megamenu']['megamenu_wrapper']) ? '<ul class="sf-megamenu"><li class="sf-megamenu-wrapper ' . $element['attributes']['class'] . '">' : '';
  $output .= $sub_menu;
  $output .= ($properties['megamenu']['megamenu_wrapper']) ? '</li></ul>' : '';
  $output .= ($properties['megamenu']['megamenu_column']) ? '</div>' : '';
  $output .= '</li>';

  return $output;
}
discipolo’s picture

awesome, i had to change it to use link_title instead of just title, probably due to my theme ...

$menu_class = strtolower(drupal_clean_css_identifier($element['item']['link']['link_title']));

 if ($menu_class) {
    $element['attributes']['class'] .= ' menu-' . $menu_class;
  }
mehrpadin’s picture

Status: Needs review » Closed (won't fix)

:)