I've used this module on a number of sites and it saves so much time, thank you for writing it!

I was wondering if anyone could explain how to add the title of the menu as a class for my menu block.

On my site the menu block requires special styling for each section, so I could use the menu title as a class to target each section. I am using Zen and it already provides a section class in the body, but some of my paths are not correct to the section body class won't work in this situation.

Thanks!

Comments

mlncn’s picture

Version: 6.x-2.2 » 6.x-2.x-dev
Status: Active » Needs work

The dev version of menu_block has a hook_menu_block_tree_alter which you can implement in a custom module to make these modifications and many more. We're using it and it is stable.-

The ugly part is recursing through every menu item but if you know the depth you care about you can skip a lot...

/**
 * Implements hook_menu_block_tree_alter().
 */
function agaricexample_menu_block_tree_alter(&$tree, $context) {
  foreach ($tree as $top_id => $item) {
    if (isset($item['below']) && is_array($item['below'])) {
      foreach ($item['below'] as $id => $item) {
        $tree[$top_id]['below'][$id]['link']['options']['attributes']['id'] = zen_id_safe($item['link']['title']);
      }
    }
  }
}

This code is a) untested, b) relies on the latest dev of menu_block, and c) requires the zen theme or stealing the zen_id_safe function out of it for yourself. So it's almost guaranteed not to work for you-- but that's the approach, and i do recommend the dev version-- congrats to JohnAlbin! Maybe we can work out best practices for use of hook_menu_block_tree_alter in this comment thread, or come to think of it maybe there is / should be a much easier alter hook for what you need to do... or maybe a theme hook works for this.

benjamin, agaric

codecouleurs’s picture

To add class in the element which has id "block-menu-block-yourblockid" I use the yourtheme_preprocess_block(&$vars) function in template.php file.

Menu block module already uses a different title for each block so I choose to use this title to create the classe. Like this the classe is different for each item menu and I can use special styling for each.

function yourtheme_preprocess_block(&$vars) {
  $vars['classes_array'][] = drupal_html_class('block-' . $vars['block']->subject);
}
JohnAlbin’s picture

Issue summary: View changes
Status: Needs work » Closed (won't fix)