[English] Drupal Tutorial: Node Auto Term, Taxonomy tips and Jquery menu API in use.

Aleksey Khodakovskiy

Aleksey Khodakovskiy

1 mars 2009 | Catégorie: Drupal, Etude de cas, Posts in English |

Taxonomy Term description is more then just text? Taxonomy Term page is more then node listings? Is it possible? Of course, using Node Auto Term and some Drupal tricks will make your idea come true.

I like to start from example where feature can be implemented, so let’s imagine you are building website for book store and you are interested in keeping hierarchy of genres, where books can be marked with this or that genre, but in the same time you want some of the genres contain more information associated with it, not only title and description, landing genre page be more or less representative - with kind of summaries, latest comments on books in this genre, etc.

Ok, why not? Drupal is always helping you ;)
At the beginning we need to install Node Auto Term [NAT] and enable Taxonomy core module. The NAT module is used to maintain node-term relationships, i.e. when a node is created, an equivalent taxonomy term is automatically created in any associated vocabularies. And as far as nodes can be extended with CCK fields - NAT is exactly what we need.

Let’s use new content types ‘Literary genre’ (genre) and ‘Store book’ (store_book) and new Taxonomy Vocabulary ‘Literary genres’, marked to tag this 2 content types. Next, go to NAT configuration - usually it can be found on this location: admin/settings/nat/settings.
Open field set for ‘Literary genre’ and select ‘Literary genres’ as a vocabulary which will contain node related term. Here you can configure 4 extra options - for our case I found important to enable ‘Associate node body with term description’ to be consistent in our task and keep same description for node and term. As far as Literary genres can be added manually in vocabulary as a terms (means that this genre doesn’t have any extra information and term page will contain only list of books belong to this genre) - ‘Delete associated term if a node is deleted’ was not enabled, so if you delete NAT node - term will be still available.

step1: Configure NAT

Lets add some genre nodes

step2: Create top-level genre node

Note: if you want to create a term not in the top of the hierarchy, but as a sub-genre you can do it easily: while creating new genre node - select term which you want to be parent for your new genre

step3: Add child-genre node

or manually with sortable option on Taxonomy terms list page

step 3b: Manually configure hierarhy of genres.

While filling store catalogue with books you will mark them with proper literary genre. Usually people use block to display taxonomy hierarchy (block can be displayed in different regions of your website), that’s why I researched a bit and found a nice module that provides us with opportunity to create extremely customizable blocks for browsing through single hierarchy taxonomies based on JQuery Menu API - Advanced Taxonomy Blocks. I would not write a lot about configuration of this module, because you can find a helpful tutorial here. So I have created new block, display in it hierarchy of ‘Literary genres’ vocabulary and count only books that were tagged with genre.

step 4: Create block that lists taxonomy vocabulary's terms.

Now when you click on one of the terms in the list - you go to the list of nodes which were marked with this genre.

step 5: Review genre term page (for now its default listing).

Last issue that should be implemented is to make Advanced Taxonomy Blocks module treat ‘Literary genre’ node page as a landing page for taxonomy term if it was specified and default ‘taxonomy/term/%tid%’ page - if not.

For each taxonomy term we will verify if ‘Literary genre’ node exists and if yes - provide node page link.
Here are few tips that can help us. First of all we should know that Drupal allows us to specify which module controls specific taxonomy vocabulary (we can define that vocabulary ‘Literary genres’ was created for the sake of ‘book_store’ module). That’s why we should execute SQL query that will set this ‘hidden’ option for us:

1
UPDATE `vocabulary` SET `module` = 'book_store' WHERE vid = _vid_;

note: _vid_ is the vocabulary id of ‘Literary genres’

The second thing to know is that if module controls taxonomy vocabulary it means that it can provide rewritten URLs for each taxonomy term in it. For this we are going to implement hook_term_path().

1
2
3
4
5
6
7
8
9
10
11
12
/**
 * Implementation of hook_term_path()
 */
function book_store_term_path($term) {
  if (module_exists('nat') && $nids = nat_get_nids(array($term->tid))) {
     // we have strong linking between node and term
    // so only 1 node can be associated with 1 auto term
    $nid = key($nids);
    return 'node/'. $nid;
  }
  return 'taxonomy/term/'. $term->tid;
}

step 6: View genre landing page.

Oh, yes :) Now with the help of simple theming we will provide what we expect from genre landing page.

node-genre.tpl.php

1
2
3
4
5
6
7
8
9
10
11
<div class="node">
  <?php if ($page == 0) { ?><h2 class="title"><a href="<?php print $node_url?>"><?php print $title?></a></h2><?php }; ?>
  <div class="content">
     <?php print theme('image', $node->field_image[0]['filepath'], $node->field_image[0]['filename'], $node->field_image[0]['filename'], array('style' => 'float:left;'))?>
    <?php print $node->content['body']['#value']?>
 
    <div align="center"><?php print t('SOME CUSTOM TEXT/BLOCKS/LINS/ETC.'); ?></div>
 
    <div align="right"><?php print l(t('Books of this genre'), 'taxonomy/term/'. key($node->nat));?></div>
  </div>
</div>

(some of php code can be moved to appropriate theme preprocess methods, here I just wanted to show how easy things can be)

step 7: View themed landing page.

I hope this small tutorial was helpful for you. If you have any questions please don’t hesitate to ask - i will answer them with great pleasure.

Modules that were used :
- CCK v.6.x-2.1 -  http://drupal.org/project/cck
- Node Auto Term [NAT] v.6.x-1.1-beta3 - http://drupal.org/project/nat
- JQuery Menu v.6.x-2.3 - http://drupal.org/project/jquerymenu
- Advanced Taxonomy Blocks v.6.x-1.8 - http://drupal.org/project/taxonomyblocks



9 commentaires sur “[English] Drupal Tutorial: Node Auto Term, Taxonomy tips and Jquery menu API in use.”

  1. Michelle Blum dit :

    Very nice tutorial. As it happened, NAT wasn’t the module I was needed (I need a way that will set the taxonomy automatically depending on content type). But great work and I’m sure I’ll use the module in the future.
    ~Michelle

  2. Thanks for your comment.
    I hope you found the module that help you to tag content depending on content type, if no - it’s called Taxonomy Defaults. You can find it here: http://drupal.org/project/taxonomy_defaults

  3. Eddie dit :

    Hi Aleksey

    Thanks for this tutorial helped me a lot.
    I am having problems though displaying Genre page that has no posts for the term. Is that possible?

    Thanks

    Eddie

  4. Eddie dit :

    Actually what I meant was is it possible to sidplay entry for the term in navigation block even if term has no posts.

    Cheers,

    Eddie

  5. Philip Blaauw dit :

    Hi alex,

    Nice tutorial. Tried it but can not get the term links in the menu link to the node page instead of the term page.

    Added the function my_custum_utils_term_path as described above to my my_custom_utils module.

    Set the vocabulary (category I use) to the my_custom_utils module in the vocabulary table.

    the function my_custum_utils_term_path is used. When I place
    drupal_set_message(’

    nid= ' . var_export($nid, true) . '

    ‘);
    just before the return ‘node/’ . $nid it shows the nid is should link to, but the link in the menu is not changed.

    Any suggestions,

    Greetings,

    Philip

  6. [...] Read the rest here: [English] Drupal Tutorial: Node Auto Term, Taxonomy tips and … [...]

  7. [...] See more here: [English] Drupal Tutorial: Node Auto Term, Taxonomy tips and … [...]

  8. Ruski dit :

    Love this module - thanks for the heads up!

Laisser une réponse