Understanding Taxonomy in Drupal

Drupal's Taxonomy module offers a host of tools you can use to classify, organize, and make sense of all the diverse content of your site using special tags called terms. An important advantage of using terms and taxonomies over other methods of classifying and organizing your content is that taxonomy is written in plain English and is easy to understand and apply.

1. Getting Started with Taxonomy

The basic elements of the Taxonomy module are terms, which are any kind of category, and vocabularies, which are a collection of terms.

In this tutorial, we're going to create a community-driven website for book lovers where members can log on and enter reviews, book-related news items, and information about new releases. We can tell right away that taxonomy will be very helpful to us, since we're going to want to organize posts according to whether they are reviews, news, or new releases, and then also according to genre -- for example, fiction, non-fiction, romance, science fiction, and children's literature.

To get started, navigate to administer >> content management and then click on the categories or taxonomy page (in Drupal 5, this page is named "categories" but it is "taxonomy" in Drupal 6).

2. Setting Up A Vocabulary

We're just going to need a single vocabulary for this simple project. You can think of the vocabulary as the root node for all of the other nodes you're going to add. In our case, we can just name all the kinds of content that will be on our site "articles". Note that if you wanted to, you could establish more than one taxonomy -- in most cases, however, a single one will do.

Click on add Vocabulary on the categories or taxonomy page. Go ahead and fill out the Add Vocabulary section. Call your vocabulary "articles" and enter "Articles submitted by users". For the help text, you can use "Please select a type of article you would like to submit" (or some other text if you prefer). Then go ahead and check the box beside "Story" so that each story node will be able to use the Articles vocabulary. Next, select Single hierarchy and check Required to complete the basic setup for a vocabulary. You can play with the options later if you like, but for now, we just want all articles to be classified in at least one category and we also want only one parent per child term.

3. Creating Terms

Next, if you go to your list tab, you will see your Articles vocabulary listed. Click on Add Terms, and you will have a form you can fill out to create a term. Let's go ahead and create a book review term. Enter "Book Review" under term name, "user submitted review of a book" under description, and then submit it. Your new term has been created.

Let's go ahead and create the other terms we are going to want. Add two new terms just like before, but call them "New Releases" and "News". Enter whatever description you like and click submit.

We noted before that we're going to want some subcategories for each of our main categories. In other words, for "Book Reviews", we might want "Fiction" and "Non-fiction" (we might want more, but let's keep it simple.) Maybe we will want "Paperbacks" and "Hardcovers" for the "New Releases" section. Let's start with Book Reviews. Add a term just like you did before, but this time, you need to select a Parent in the drop-down list (earlier, the parent was just "root"). Pick "Book Review". This means that your fiction and non-fiction sections "belong to" (or are "children of") the term "Book Review". Enter the rest of the information for the term just as before, and repeat for Non-Fiction. Now do the same thing for your "New Releases" section and add "Paperback" and "Hardcover" children for the parent node, "New Releases".

4. Putting it all together

Go to your test site and play around with the new taxonomy you've set up. Practice adding some articles in your different categories to see how it works.

If you have Pathauto installed, it's a great idea to set all your Story paths to use this pattern:

[vocab]/[catpath]/[nid]-[title]

This means that if a new article is created, say in the Fiction Book Review section, titled "Wicked", it would automatically get a url that looked a little bit like this:

http://yoursiteURL.com/articles/book_reviews/fiction/1-Wicked

You can see that this is another important advantage of using taxonomy -- each new content item will automatically get classified and assigned a meaningful, easy-to-identify path name.