Abdullah Diab’s Blog

Display Sub-terms In Drupal Term Page

After working with Drupal in the last two months I’ve really got used to searching for modules for every problem I face and installing them.

While I was working on iCommunity website, I was creating taxonomy terms to categorize the files using them. I created terms with sub terms. The problem was that when browsing a specified term the term page doesn’t display the sub terms of that term.

I searched the internet and found a solution that’s working.

After trying it I decided to try developing a module for doing that (just like the author of the solution suggested).

I’m a newbie in developing for Drupal but I knew how to dig my way through it 😀

(You’d be shocked if you knew that I don’t know PHP 😛 I just write code!)

So before I start this post please this is the first time I write modules, and I learned it yesterday 😛 so if you find any error or something wrong tell me 🙂

I named the module Taxonomy subterms. 😀

So the process is just re-implement the function ‘theme_taxonomy_term_page’ from taxonomy module, but to override it I needed to let Drupal call the function in my module when the user requests ‘/taxonomy/term/’ so to do that I needed to add the ‘hook_menu’ function.

function taxonomy_subterms_menu() {  
  $items['taxonomy/term/%'] = array(
    'title' => 'Taxonomy term',
    'page callback' => 'taxonomy_subterms_term_page',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  
  return $items;
}

But by doing this I discovered that the Taxonomy module won’t handle the page, so I couldn’t find a way to tell Taxonomy module to handle the page and my module to complete the handling 🙁 So I copied the ‘taxonomy_term_page’ function and renamed it to ‘taxonomy_subterms_term_page’

Then I re-implemented the ‘theme_taxonomy_term_page’ function so I can add the sub-terms section (just like the solution I mentioned did):

function theme_taxonomy_term_page($tids, $result) {
  drupal_add_css(drupal_get_path('module', 'taxonomy') . '/taxonomy.css');
  drupal_add_css(drupal_get_path('module', 'taxonomy_subterms') . '/taxonomy_subterms.css');
  $output = '';

  // Only display the description if we have a single term, to avoid clutter and confusion.
  if (count($tids) == 1) {
    $term = taxonomy_get_term($tids[0]);
    $children = taxonomy_get_children($tids[0]);
    $description = $term->description;
    if (!empty($description) || sizeof($children) > 0) {
      $output .= '<div class="taxonomy-details-wrapper">';
      $is_wrapped = true;
    } else {
      $is_wrapped = false;
    }
    // Check that a description is set.
    if (!empty($description)) {
      $output .= '<div class="taxonomy-term-description">';
      $output .= filter_xss_admin($description);
      $output .= '</div>';
    }
    if (sizeof($children) > 0) {
      $output .= '<div class="sub-terms-list"><h3>' . t('Sub-Terms:') . '</h3> <ul class="sub-terms inline">';
      foreach($children as $child) {
        $output .= '<li>' . l($child->name, taxonomy_term_path($child)) . '</li>';
      }
      $output .= '</ul></div>';
    }
    if ($is_wrapped) {
      $output .= '</div>';
    }
  }

  $output .= taxonomy_render_nodes($result);

  return $output;
}

Then I had my module working and installed it in the website (you can check it out here. The page is in Arabic but you can easily find the sub terms).

Download

You can download the module here (I didn’t maintain a project @ Drupal because I’m not yet ready to do that, this is just a small experiment).