thierry-b
12/20/2013 - 11:27 AM

This module gets the main menu tree data and adds the menu links to a specific taxonomy menu block. When the main menu is multilingual, only

This module gets the main menu tree data and adds the menu links to a specific taxonomy menu block. When the main menu is multilingual, only the links that corresponds to the current language will be added to the taxonomy menu block. By default the vocabulary id is set to 2, this is defined by a variable on top of the module file.

<?php
/**
 * @file custom_taxonomy_menu_block
 *
 * @description
 * Add the menu items of main-menu to the taxonomy menu block called main-menu
 **/

define('TMB_MAIN_MENU_VID', 2);

/**
 * Implements hook_taxonomy_menu_block_alter().
 *
 * Alters the taxonomy menu block data.
 **/
function custom_taxonomy_menu_block_taxonomy_menu_block_alter(&$tree, $config) {
  // Alter the output of only a single specific taxonomy menu block.
  if($config['vid'] == TMB_MAIN_MENU_VID && $config['delta'] == 1) {

    // Get the build info from the main menu.
    $main_menu_build = _menu_build_tree('main-menu');

    // Check access for the current user to each item in the tree.
    menu_tree_check_access($main_menu_build['tree'], $main_menu_build['node_links']);

    // If the active page is a node, get the object for setting the active trail.
    $obj = new stdClass();
    if(arg(0) == 'node') {
      $obj = menu_get_object();
    }

    // Get the language object
    global $language;

    // Transform the menu items into tmb menu items.
    foreach ($main_menu_build['tree'] as $menu_item) {
      // For multilingual menu's, check the menu item's language
      if(isset($menu_item['link']['language'])) {
        if($menu_item['link']['language'] == $language->language) {
          custom_taxonomy_menu_block_tmb_transform_menu_item($menu_item, $tree, $main_menu_build, 0, $config['depth'], $obj);
        }
      }
      else {
        custom_taxonomy_menu_block_tmb_transform_menu_item($menu_item, $tree, $main_menu_build, 0, $config['depth'], $obj);
      }
    }
  }
}

/**
 * Transform a regular menu item to a tmb menu item.
 **/
function custom_taxonomy_menu_block_tmb_transform_menu_item($menu_item, &$tree, $main_menu_build, $depth = 0, $max_depth = 8, $obj) {
  // Set the menu item's parent
  $parents = array(0);
  if (isset($main_menu_build['node_links'][$menu_item['link']['plid']])) {
    $parents[0] = $main_menu_build['node_links'][$menu_item['link']['plid']];
  }

  // Set the active trail
  $active_trail = 0;
  // If the active item is a node, check if this menu item matches it.
  if (isset($obj->nid)) {
    if ($menu_item['link']['link_path'] == 'node/' . $obj->nid) {
      // Setting the active trail to 2 gives the list item the class active.
      $active_trail = 2;

      // If the item has a parent, add the active-trail class
      if($parents[0] <> 0) {
        custom_taxonomy_menu_block_tbm_active_trail($main_menu_build['node_links'][$menu_item['link']['plid']], $tree);
      }
    }
  }

  // Create the new tmb menu item
  $tree['main_menu_mlid_'.$menu_item['link']['mlid']] = array(
    'name' => $menu_item['link']['link_title'],
    'path' => drupal_get_path_alias($menu_item['link']['link_path']),
    'depth' => $depth++,
    'parents' => $parents,
    'active_trail' => $active_trail,
  );

  // Create the deeper levels if the maximum depth is not reached.
  if(!empty($menu_item['below']) && $depth < $max_depth) {
    foreach ($menu_item['below'] as $child_menu_item) {
      custom_taxonomy_menu_block_tmb_transform_menu_item($child_menu_item, $tree, $main_menu_build, $depth, $max_depth, $obj);
    }
  }
}

/**
 * Set the parents active trail.
 **/
function custom_taxonomy_menu_block_tbm_active_trail($parent, &$tree) {
  // Setting active trail to 1 gives the list class the class active-trail.
  $tree[$parent]['active_trail'] = 1;

  // Prevent the top level from going deeper
  if($tree[$parent]['parents'][0] <> 0) {
    custom_taxonomy_menu_block_tbm_active_trail($tree[$parent]['parents'][0], $tree);
  }
}
name = Custom taxonomy menu block
description = Alter taxonomy menu blocks
core = 7.x

package = the AIM

version = 7.x-1.0