Taxonomy

by Gisle Hannemyr

This chapter introduces the Drupal 7 taxonomy framework, with some examples of how to use taxonomies.

Table of contents

Introduction

The Taxonomy module in Drupal is part of the core, and enables categorization of content in both taxonomies (controlled vocabularies) and folksonomies (free tagging).

Taxonomies in Drupal are organised into vocabularies and terms, with the following properties:

To use the tools described in this chapter, you will need to be logged in on your Drupal 7 website as an administrator.

When you navigate to Structure » Taxonomy, you are shows a list of the vocabularies that currently exists. The screen shot below shows how this list looks on a freshly created Drupal 7 website.

Taxonomy.

Modules may manage their own taxonomies. For example, the Forum module uses the Taxonomy module to manage a vocabulary named Forums where the containers and forums are added automatically as terms when they are created.

Free tagging

Drupal comes with a predefined vocabulary named Tags. Initially, this vocabulary contains no terms. This vocabulary is by default used as a field to the predefined Article content type. To see how the Article content type is constructed, navigate to Structure » Content types and click on manage fields to the right of the content type named Article. If you're running a default configuration, you'll see something like the screen shot below.

Article fields.

The field with the label Tags is of field type Term reference, and set up to use the Autocomplete term widget (tagging). This configuration (in particular the autocomplete term widget) allows users to type anything they like into the Tags field that appears in the Create article form. Whatever the user types is automatically added to the Tags vocabularey.

To examine or change the specific settings for the Tags field when it is used in the Article content type, navigate to Structure » Content types » manage fields for Article, and click on edit for the field labelled Tags. This takes you to a form with these settings. This form allows you to:

Notice that the vocabulary the Tags field is associated with (Tags) is greyed out. This is because the field is already populated with data. As soon as this happens you can no longer change this association.

When you're done, click Save settings.

Adding tagging to content types

The main use of taxonomies is to let users tag content they create with taxonomy terms. To turn on tagging for a content type, navigate to Structure » Content types, then click on Manage fields for the content type you want users to be able to tag, then add a field to the content type to hold the taxonomy term reference. If a field for this vocabulary already exists, use Add existing field. If this is the first field you create for this vocabulary, use Add new field.

To demonstrate how to do this we're going to add a free tagging tags field to a content type called blog entry. The Blog module that contains the blog entry content type is part of the core, but it is disabled by default. So if you haven't enabled it yet, you first need to enable the Blog module.

To examine or change the specific settings for the Tags field when it is used in the Blog entry content type, navigate to Structure » Content types » manage fields for Blog entry. Now, in the section Add existing field, select Term reference: field_tags (Tags), which is the same Tags field that is already in use for the Article content type. Also select the Autocomplete term widget (tagging) to allow free form tagging.

Blog fields.

The screen shot above shows how to fill in the form. When you're done, press for Save, and a field for free tagging is added to the Blog entry content type.

When you set up a tagging field for a content type, the choice of widget determines what sort of tagging is allowed. A Select list or Check boxes/radio buttons restricts the user to using predefined terms (i.e. the user is rerstricted to a controlled vocabulary). If you set up the field to use the widget known as Autocomplete term widget (tagging), there will be a text field that will autocomplete for existing terms, but users can also type in new terms. When they do, these new terms will automatically be added to the vocabulary.

Adding a vocabulary

To add a vocabulary, navigate to Structure » Taxonomy » Add a vocabulary. We are going to create a vocabulary for cameras.

Add vocabulary.

A vocabulary must be given a human-readable name. Drupal converts automatically converts it into a machine name. Because the machine name is sometimes used when theming, you should keep it fairly short (e.g. eight characters or less). The description is not mandatory, but since it by default is displayed at the top of generated lists with tagged content, it may be a good idea to add it.

After you've typed in the Name and the Description, click Save to save the vocabulary. It will not yet have any terms in it.

After clicking Save, you're shown a panel with a list of content vocabularies. If you want to add terms to your freshly created vocabulary, click the link add terms.

Defining terms

We are creating a vocubulary for tagging blog entries with camera models. It is going to be hierachical, with camera manufacturer at the top level, and specific camera models at the second level. In the form below, we're creating a taxonomy term for the M8, a camera manufactured by Leica. We've already created a term for the manufacturer, so we expand the Relations panel to indicate that M8 is defined as a child term, with Leica as parent term.

When a taxonomy term is created, it is assigned a numeric unique primary key (tid) that can be used to access the taxonomy term object (the name, description, and a list of tagged entities). For instance if the Leica M8 is assigned tid 31, the following path: taxonomy/term/31 will always load and display the M8 object.

tipTo see what tid is assocated with a specific term, click on the term. In the browser's address field, you'll see the path to this term object. The path ends with a number (e.g. the path taxonomy/term/31 ends with the number 31). The number (31 in this case) is the tid for this specific term.

Terms.

The URL alias can be used to alias the path to something that makes sense to humans. By setting the URL alias to cameras/leica/m8 as shown in the screen shot above, the path cameras/leica/m8 will also load the Leica M8 object. The latter path is usually easier for humans to relate to then taxonomy/term/31. (The URL alias setting in the form for defining a taxonomy term is just a shortcut to Configuration » URL aliases » Add alias.)

After we've defined more terms, we may have a vocabulary for tagging blog posts with camera models like the one shown below. It shows a two level hierarchy, where specific camera models are listed as child nodes with their manufacturers as parents.

Terms.

As always in Drupal, we may rearrange both the order of such lists by manipulating the terms with the mouse. Remember to click Save afterwards to preserve the reordering.

Defining a new tagging field

In addition to letting you share fields across content types, Drupal also let you define new tagging fields. To do this look at the Add new field section of the form for adding fields to a content type, and set the Field column to Term reference. In the screen shot below, we add a new tagging field to be tag blog entries with cameras.

Blog fields.

When you press Save, you are taken to a form named Field settings where you shall associate the new field with a specific vocabulary. This setting can not be changed later (after the first entity has been tagged).

Field settings.

The form above associates the Camera tagging field whenever it is used with the vocabulary Cameras.

Click Save field settings to save this association and to go back to the Manage fields panel.

Using taxonomies

The simplest use of the Taxonomy module is to to tag an entity with a single term from a single vocabulary.

tipAs already noted Drupal gives access to a taxonomy term object by means of a specific path. For example, the path taxonomy/term/31 will give access to the taxonomy term object whose tid is 31. One place where you can put such paths is in menus. For example if you navigate to Structure » Menus » add link, and insert a taxonomy path in the Path field, you will create a menu item that can be clicked on access the taxonomy term object whose tid is at the end of the path. For example, if the term “M8” has been assigned the tid 31, you can create a menu item to access the “M8” taxonomy term object by entering taxonomy/term/31 in the Path field.

By using multiple vocabularies it is even possible to classify an individual entity in multiple ways. For example a site where people can blog about photo equipment may have a Cameras vocabulary with terms such as “Leica M8” and “Nikon D800” associated with it; a Price range vocabulary with terms such as “Less than $100”, “From $100 to $1000” and “More than $1000”; and a Camera class vocabulary with terms such as “P&S”, “DSLR” and “MILC”. Thus, we can use tags to indicate that a particular blog entry is about a Nikon D800, which costs more then $1000, and that it belongs to the DSLR class – and it could be located by any of these three terms.

noteIn Drupal 6, there was support for a special path syntax allowing for use of more than one tid in the path, and for supplying a depth argument for selecting multiple levels in hierarchical vocabularies. This special path syntax is deprecated in Drupal 7. You can still construct this type of complex queries, but you have to do it by means of the Views module, or through the taxonomy API.

The taxonomy pager

The default number of terms per page that are shown when you visit a particular vocabulary is 100, i.e.:

$page_increment  = variable_get('taxonomy_terms_per_page_admin', 100);

There is no GUI to set the database variable taxonomy_terms_per_page_admin, but it may be set using drush.

When you visit the path to examine the list of nodes produced when you visit the page for a particular taxonomy term (e.g. taxonomy/term/N), you get 10 nodes or the number of nodes controlled by database variable default_nodes_main. The code is this line in taxonomy.pages.inc:

// Arguments to taxonomy_select_nodes:
//   tid, use pager (TRUE or FALSE), no of terms or FALSE if to limit.
$nids =
  taxonomy_select_nodes($term->tid, TRUE, variable_get('default_nodes_main', 10));

The quick and dirty fix is to hack core (just don't do it). The following disables the pager and set the number of terms to unlimited:

$nids = taxonomy_select_nodes($term->tid, FALSE, FALSE);

Since hacking core is deprecated, the correct solution is given in this answer at Drupal SE.

Final word

[TBA]


Last update: 2018-07-21 [gh].