Menu theming

kingandy - July 23, 2007 - 10:27
Project:Taxonomy context
Version:5.x-1.x-dev
Component:User interface
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

I've had to use taxonomy_context in such a way that each category has its own style, and to do so I've had to alter the module itself. I think it would be useful if either
a) the menu items had unique classes (by $tid would be easiest), or
b) the menu element generation code was themable.

The first one is easiest and most straightforward, and would only require the following changes:

<?php
$output
.= '<li class="expanded">'. $link ."\n";
?>
to
<?php
$output
.= '<li class="expanded taxonomy-context-'.$term->tid.'">'. $link ."\n";
?>
at line 438,
and
<?php
$output
.= '<li class="collapsed">' . $link . "</li>\n";
?>
to
<?php
$output
.= '<li class="collapsed taxonomy-context-'.$term->tid.'">' . $link . "</li>\n";
?>
at line 446.

The second solution is perhaps more drupally, and would involve lifting some or all of the menu_tree code out into a themable function. As some brief off-the-cuff starter code, maybe the following would work:

<?php
/**
* Return a menu tree for a vocabulary.
*/
function taxonomy_context_menu_tree($vid, $tid = NULL) {
  static
$context;
  if (!isset(
$context)) {
   
$context = taxonomy_context_get_context();
  }
 
$parents = array();
 
$parents_terms = array_reverse(taxonomy_get_parents_all($context->tid));
  foreach (
$parents_terms as $parent) {
   
$parents[] = $parent->tid;
  }

 
$terms = $tid ? taxonomy_get_children($tid, $vid) : taxonomy_get_tree($vid, 0, -1, 1);
 
$output = "<ul class=\"menu\">\n";
  if (
$terms) {
    foreach (
$terms as $term) {
     
$output .= theme('taxonomy_context_menu_item',$vid,$term,$parents);
    }
  }
 
$output .= "</ul>\n";
  return
$output;
}

function
theme_taxonomy_context_menu_item($vid,$term,$parents) {
 
$params = array('title' => $term->description ? strip_tags(node_teaser($term->description)) : t('View this section'));
 
$link = l($term->name, taxonomy_term_path($term), $params); 
  if (
in_array($term->tid, $parents)) {
   
$output .= '<li class="expanded">'. $link ."\n";
   
$output .= taxonomy_context_menu_tree($vid, $term->tid, FALSE) ."\n";
    if (
variable_get('taxonomy_context_node_block', TAXONOMY_CONTEXT_NODE_BLOCK_NONE)) {
     
$output .= '<ul class="menu">'. taxonomy_context_show_nodes($term->tid) ."</ul>\n";
    }
   
$output .= "</li>\n";
  }
  else {
   
$output .= '<li class="collapsed">' . $link . "</li>\n";
  }
  return
$output;
}
?>
... which obviously doesn't include the TID-specific classes added above, but allows themers to add such classes if they wish.

I'd maybe expand that to include a theme_menu_tree style function - basically taking <ul class=\"menu\"> out of the function and wrapping it around the output with a themable function - but I'm afraid I'm strapped for time right now.

Apologies for not doing a patch file, I haven't got the facility to do so at the moment... hope it is useful nonetheless.

#1

Summit - March 17, 2008 - 20:01

Hi,
Very much interested in theming a specific menu-item in a vocabulary block with this great module. Somebody experience with the above, and is there a possibility this will be put into the module?
Thanks a lot for considering this.
Greetings,
Martijn

 
 

Drupal is a registered trademark of Dries Buytaert.