<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Module on Abdullah Diab&#39;s Blog</title>
    <link>https://mpcabd.xyz/tags/module/</link>
    <description>Recent content in Module on Abdullah Diab&#39;s Blog</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Tue, 30 Mar 2010 22:06:42 +0000</lastBuildDate>
    <atom:link href="https://mpcabd.xyz/tags/module/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Display Sub-terms In Drupal Term Page</title>
      <link>https://mpcabd.xyz/display-sub-terms-in-drupal-term-page/</link>
      <pubDate>Tue, 30 Mar 2010 22:06:42 +0000</pubDate>
      <guid>https://mpcabd.xyz/display-sub-terms-in-drupal-term-page/</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;While I was working on &lt;a href=&#34;http://www.communityi.net&#34;&gt;iCommunity website&lt;/a&gt;, 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.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>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.</p>
<p>While I was working on <a href="http://www.communityi.net">iCommunity website</a>, 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.</p>
<p>I searched the internet and found <a href="http://www.rbgrn.net/content/194-drupal-taxonomy-how-to-add-list-clickable-sub-categories">a solution</a> that’s working.</p>
<p>After trying it I decided to try developing a module for doing that (just like the author of the solution suggested).</p>
<p>I’m a newbie in developing for Drupal but I knew how to dig my way through it 😀</p>
<p>(You’d be shocked if you knew that I don’t know PHP 😛 I just write code!)</p>
<p>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 🙂</p>
<p>I named the module Taxonomy subterms. 😀</p>
<p>So the process is just re-implement the function <em>‘theme_taxonomy_term_page’</em> from taxonomy module, but to override it I needed to let Drupal call the function in my module when the user requests <em>‘/taxonomy/term/’</em> so to do that I needed to add the <em>‘hook_menu’</em> function.</p>
<pre class="lang:php decode:1">function taxonomy_subterms_menu() {  
  $items['taxonomy/term/%'] = array(
    'title' =&gt; 'Taxonomy term',
    'page callback' =&gt; 'taxonomy_subterms_term_page',
    'page arguments' =&gt; array(2),
    'access arguments' =&gt; array('access content'),
    'type' =&gt; MENU_CALLBACK,
  );
  
  return $items;
}
</pre>
<p>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’</p>
<p>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):</p>
<pre class="lang:php decode:1">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-&gt;description;
    if (!empty($description) || sizeof($children) &gt; 0) {
      $output .= '&lt;div class="taxonomy-details-wrapper"&gt;';
      $is_wrapped = true;
    } else {
      $is_wrapped = false;
    }
    // Check that a description is set.
    if (!empty($description)) {
      $output .= '&lt;div class="taxonomy-term-description"&gt;';
      $output .= filter_xss_admin($description);
      $output .= '&lt;/div&gt;';
    }
    if (sizeof($children) &gt; 0) {
      $output .= '&lt;div class="sub-terms-list"&gt;&lt;h3&gt;' . t('Sub-Terms:') . '&lt;/h3&gt; &lt;ul class="sub-terms inline"&gt;';
      foreach($children as $child) {
        $output .= '&lt;li&gt;' . l($child-&gt;name, taxonomy_term_path($child)) . '&lt;/li&gt;';
      }
      $output .= '&lt;/ul&gt;&lt;/div&gt;';
    }
    if ($is_wrapped) {
      $output .= '&lt;/div&gt;';
    }
  }

  $output .= taxonomy_render_nodes($result);

  return $output;
}
</pre>
<p>Then I had my module working and installed it in the website (you can check it out <a href="http://www.communityi.net/taxonomy/term/3">here</a>. The page is in Arabic but you can easily find the sub terms).</p>
<h2 id="download">Download</h2>
<p>You can download the module <a href="http://www.box.net/shared/hzahnghh6x">here</a> (I didn’t maintain a project @ Drupal because I’m not yet ready to do that, this is just a small experiment).</p>]]></content:encoded>
    </item>
  </channel>
</rss>
