thierry-b
10/10/2013 - 8:41 AM

Add an image to the TMB tree (cached) and it's output.

Add an image to the TMB tree (cached) and it's output.

<?php

define('TMB_MAIN_MENU_VID', 1);

/**
 * Implement hook_taxonomy_menu_block_tree_alter()
 *
 * This hook allows you to alter the tree before caching.
 * It is per vocabulary, not per block.
 **/
function yourmodule_taxonomy_menu_block_tree_alter(&$tree, $vid) {
  // Alter the vocabulary Product Type tree before caching
  if($vid == TMB_MAIN_MENU_VID) {

    // Get the parent terms
    $parents = array();
    foreach ($tree as $tid => $term) {
      if($term['depth'] == 0) {
        $parents[] = $tid;
      }
    }

    // Load the Terms at once
    $terms = taxonomy_term_load_multiple($parents);

    // Add the product type image to the tree
    if(!empty($terms)) {
      foreach ($terms as $tid => $term) {
        if(isset($term->image)) {
          $image = field_get_items('taxonomy_term', $term, 'image');
          $tree[$tid]['image']['#theme'] = 'image_formatter';
          $tree[$tid]['image']['#item'] = $image[0];
        }
      }
    }
  }
}
<?php


/**
 * Implements taxonomy_menu_block__
 */
function template_taxonomy_menu_block__X($variables) { // Change the X with your vid
  $tree = $variables['items'];
  $config = $variables['config'];
  $num_items = count($tree);
  $i = 0;
  $output = '<ul>';
  foreach ($tree as $tid => $term) {
    $i++;
    // Add classes.
    $attributes = array();
    if ($i == 1)
      $attributes['class'][] = 'first';
    if ($i == $num_items)
      $attributes['class'][] = 'last';
    if ($i%3 === 0)
      $attributes['class'][] = 'third';
    if ($i%4 === 0)
      $attributes['class'][] = 'fourth';
    $attributes['class'][] = ($i%2) ? 'odd' : 'even';

    // Set alias option to true so we don't have to query for the alias every
    // time, as this is cached anyway.
    $output .= '<li' . drupal_attributes($attributes) . '>';
    if(isset($term['image']) && !empty($term['image'])) {
      if(strlen($term['image']['#item']['title']) === 0) {
        $term['image']['#item']['title'] = $term['name'];
      }
      $logo = render($term['image']);
      $output .= l($logo . $term['name'], $term['path'], $options = array('alias' => TRUE, 'html' => TRUE));
    }
    else {
      $output .= l($term['name'], $term['path'], $options = array('alias' => TRUE));
    }
    if (!empty($term['children'])) {
      $output .= theme('taxonomy_menu_block__' . $config['delta'], (array('items' => $term['children'], 'config' => $config)));
    }
    $output .= '</li>';
  }
  $output .= '</ul>';
  return $output;
}

/**
 * Implement hook_preprocess_taxonomy_menu_block()
 * 
 * @status
 *  This should be done in a separate block, not in a preprocess function.
 * 
 **/
function template_preprocess_taxonomy_menu_block(&$variables){
 
  //only for block with delta == 1
  if ( 1 == $variables['config']['delta']) {
 
    $links = menu_tree('main-menu');
    $main_links = array();
 
    foreach ($links as $link) {
      if(isset($link['#original_link']) && !empty($link['#original_link'])) {
        $original_link = $link['#original_link'];
        $main_links[$original_link['mlid']] = array('name'=>$original_link['link_title'], 'path'=> $original_link['link_path'], 'depth'=>0, 'parents'=>array(), 'active_trail'=>'0', 'children'=>array());
      }
    }
    
    //switch both arrays in the merge if you want the term links to be appended or prepended
    $variables['items'] = array_merge($main_links,$variables['items']);
  }
}


/**
 * Implements taxonomy_menu_block__
 */
/*function template_taxonomy_menu_block__1($variables) {
  $tree = $variables['items'];
  $config = $variables['config'];
  $num_items = count($tree);
  $i = 0;
  $output = '<ul>';
  foreach ($tree as $tid => $term) {
    $i++;
    // Add classes.
    $attributes = array(); 
    $attributes['class'][] = $i % 2 ? 'odd' : 'even'; 
    if($i%1 == 0) {
      $attributes['class'][] = 'item';
    }
    // Set alias option to true so we don't have to query for the alias every
    // time, as this is cached anyway.
    $output .= '<li' . drupal_attributes($attributes) . '>';
    $output .= '<a href="/node/'.$term['link'].'">';
    $output .= '<div class="icon">'.theme('image_style', array('style_name' => '160x160', 'path' => $term['icon'])).'</div>';
    $output .= '<h4>'.$term['name'].'</h4>';
    $output .= '</a></li>';
  }
  $output .= '</ul>';
  return $output;
}*/