We're redesigning our site and getting rid of the sidebar. The main reason for the sidebar to exist was to hold Organic Menus (og_menu), which we have hundreds of groups, each with 2 or 3 menus. I want to move OG menus to the main menu.

What I would like to do is when a user visits a particular OG

  • load the menus for that OG (trivial)
  • programmatically make the menu's name the top level links linked to # (Trivial - we use bootstrap)
  • add the menu's links below those top level links (this is where it gets tricky)
  • and append them to the main menu as the last to links (no clue)

So the top level would look like this only when the user hit the group

Home Communities Forum Group-Menu-1 Group-Menu-2

Home, communities and forum are always there the group menus are appended when they're in that group. I don't need code I just need direction on which parts of the API will facilitate this.

1 Answer 1


I solved this by just appending my links to the menu tree with hook_preprocess_menu_tree


function my_module_preprocess_menu_tree(&$vars) {

  if($vars['theme_hook_original'] != 'menu_tree__primary') {

  $node = menu_get_object();

  //Check to see if this is a group node or group content and return the group_node
  $group_node = my_module_check_for_group_node($node);

  if($group_node) {

    global $user;

    $vars['tree'] = str_replace('last leaf', 'leaf', $vars['tree']);

    $result = db_query('SELECT menu_name FROM og_menu WHERE gid = :nid ORDER By weight', array(':nid' => $group_node->nid));

    $menus = $result->fetchAll();

    foreach($menus as $menu) {
      $tree = menu_tree($menu->menu_name);

      if(!empty($tree)) {
        // render the organic group menu as HTML
        $tree = drupal_render($tree);

        $tree = str_replace('menu nav', 'dropdown-menu', $tree);

        $menu_array = menu_load($menu->menu_name);

        //format <li> and top-level <a> for bootstrap
        $li = '<li class="expanded dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">'.$menu_array['title'].'<span class="caret"></span></a>'.$tree.'</li>';

        $vars['tree'] .= $li;

    $vars['tree'] .= $li; 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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