Wednesday, June 29, 2011

Check if Drupal 7 region is empty and print a body class (template.tpl.php)

Add to your template.tpl.php the following function:

function YourThemeName_preprocess_html(&$variables) {
if (empty($variables['page']['RegionName'])) {
$variables['classes_array'][] = 'YourClassName';
}
}

Drupa API
7 – 8 template_preprocess_html(&$variables)
http://api.drupal.org/api/drupal/includes--theme.inc/function/template_preprocess_html/7

Sunday, June 5, 2011

Add unique class (mlid) to all Drupal 7 menu items

Main and secondary menus has a great class .menu-123 (.menu-mlid) to do that. So here how you can port the same behavior to any/all menu items. Add the following function in your template.php file,

<?php
/**
* Add unique class (mlid) to all menu items.
*/
function YOURTHEMENAME_menu_link(array $variables) {
$element = $variables['element'];
$sub_menu = '';

$element['#attributes']['class'][] = 'menu-' . $element['#original_link']['mlid'];

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";
}
?>

7 – 8 theme_menu_link(array $variables)

See core function theme_menu_link for Drupal 7 - 8 at http://api.drupal.org/api/drupal/includes--menu.inc/function/theme_menu_link/7

Thanks betarobot for this comment