1

I am using Drupal 7 with the Bootstrap theme and a customised subtheme.

I would like to modify template.php to add a class for each <li> in the menu. As per instructions provided in: https://drupal.org/comment/5830446#comment-5830446

Instructions say:

"...use this snippet in template.php to put a unique id for <a> and <li> elements at the same time"

<?php
/**
* theme_menu_link()
*/
function MYSUBTHEME_menu_link(array $variables) {
//add class for li
   $variables['element']['#attributes']['class'][] = 'menu-' . $variables['element']['#original_link']['mlid'];
//add class for a
   $variables['element']['#localized_options']['attributes']['class'][] = 'menu-' . $variables['element']['#original_link']['mlid'];
//dvm($variables['element']);
  return theme_menu_link($variables);
}
?>

Which template.php file should I modify? and how? I have the following template.php available:

./bootstrapMYSUBTHEME/template.php
./bootstrapMYSUBTHEME/templates/template.php
./bootstrap/template.php
./bootstrap/bootstrap_subtheme/template.php
1
  • Bootstrap separated from Twitter some time ago. Just mentioning. By the way, isn't /bootstrap/bootstrap_subtheme/ an example dir only, to copy-paste to start writing your own subtheme? And where /bootstrapMYSUBTHEME/templates/template.php came from? I'm working on a site that uses Bootstrap + it's subtheme (not as an theme guy, but anyway) and we certainly don't have it.
    – Mołot
    Jan 13 '14 at 7:41
4

Whenever in a theming guide you see a reference to template.php without a context, it should (and in official documentation does) mean the template.php in the root directory of a theme / subtheme you are developing.

In your case it means ./bootstrapMYSUBTHEME/template.php

Never hack your base theme. Your changes will be lost and gone if someone will decide to upgrade it. And if you will upgrade it, you will have to re-write them into new version after each upgrade. Waste of time, subthemes are there exactly to avoid that burden.

1
  • Thanks. It worked. The whole content into /bootstrapMYSUBTHEME/template.php Jan 17 '14 at 19:13
1

Code for customized menus used with Drupal Bootstrap subtheme

<?php

/**
 * @file template.php
 */

 /**
 * Overrides theme_menu_link().
 */
function bootstrapf24_preprocess_menu_link(&$variables) {
    $variables['element']['#attributes']['class'][] = 'menu-' . $variables['element']['#original_link']['mlid'];
    $menu_title_class = preg_replace("/[^a-zA-Z0-9]/", "-", strtolower(strip_tags($variables['element']['#title'])));
    // Another option $menu_title_class = preg_replace("/[^a-zA-Z0-9]/", "-", strtolower(strip_tags($variables['element']['#original_link']['link_title'])));
    $variables['element']['#attributes']['class'][] = 'menu-' . $menu_title_class;

    if ($variables['element']['#original_link']['menu_name'] == 'user-menu') {
        $variables['element']['#localized_options']['html']= true;
        $variables['element']['#title'] = '<span>'.$variables['element']['#title'].'</span>';
    }   
}

without <php ending tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.