Drupal Taxonomy Tutorial
How to manage taxonomy in Drupal
In Drupal taxonomy is used to organize and classify the content on your site. For example, when you create content items (nodes) you can add tags that are related to the subject, content, etc. of the node. When a user clicks on such a tag on the frontend of your site, all nodes that contain that tag will be listed. In Drupal these tags are referred to as taxonomy terms. One term can be included in as many nodes as you want. Taxonomy terms are grouped into vocabularies. You can include many terms in one vocabulary group but it's not recommended to create a vocabulary that's too big (you can split it into several vocabulary groups). For example, you can have a vocabulary called Cities that consists of terms such as New York, London, Paris, etc.
In this tutorial we'll show you how to create and edit vocabularies, how to add terms to a vocabulary and manage existing ones, and how to add term fields to content types, so that you can include tags in nodes.
Adding and Managing Vocabularies
To manage vocabularies, terms and anything connected with taxonomy, log in to the admin panel of your Drupal, click on the Structure tab in the top navigation bar and on the page that opens click on Taxonomy. On the next page that opens you'll see a table with all the existing vocabularies. Drupal comes preconfigured with one vocabulary named Tags. A field is added by default to the preconfigured article content type. This field is configured to work with this Tags vocabulary group. This means that when you create an article you can specify tags for that article and they will be automatically included as terms in the Tags vocabulary. We'll come to adding term fields to content types and relating them to a vocabulary group later in this tutorial.
On the Taxonomy page of the admin panel click on the edit vocabulary button for an existing vocabulary to edit its settings, or click on the Add vocabulary button that's above the top left corner of the table to add a new vocabulary group:
In both cases the settings are the same. You can add/change the vocabulary name and include a description. The description is optional and it's only shown in the admin panel to give some more information about the vocabulary:
After you're done with the settings click on the Save button.
Adding and Managing Terms
Once a vocabulary is created you can add terms to it. Of course, you can also add terms to existing vocabularies. On the Taxonomy page of the admin panel there are a couple of other buttons for each vocabulary: list terms and add terms. The list terms button will display a page with the exiting terms in that vocabulary; you can edit the settings of these terms, you can change their order, and you can also add new terms. If you want to add new terms directly, just click on the add terms button:
As an example we've created a vocabulary called Cities and now we want to add terms (e.g. New York, London, etc.) to it.
Clicking on the add terms button will display the page with the term settings that you have to configure. Type a name for the term and optionally a description in the corresponding fields:
The name will be displayed on the frontend as a tag, for example, or a menu item (wherever you include the term). It can consist of more than one word. By default the field for the description is also configured to be visible on the frontend. This means that if you type a description, it will be shown on the frontend on the page that lists the nodes that have the particular term/tag assigned to them.
Under the text area for the description there's a field labeled URL alias. In that field you can specify a friendly URL for the term. By default, the path to a taxonomy term has the form of taxonomy/term/2, meaning the URL address to the term would be something like http://yourdomain.com/taxonomy/term/2. If you type an URL alias, don't type the whole URL address but just the path that's added to the base URL address. For example, if your site is accessible at yourdomain.com, meaning that Drupal is installed directly in the public_html directory on your Drupal hosting account, the name of the term is New York and you want the URL address to be yourdomain.com/new-york, then just type new-york in the field for URL alias. Now when somebody uses the URL address yourdomain.com/new-york (and also the original URL address-e.g. yourdomain.com/taxonomy/term/2), all items on your site containing the term New York will be listed.
Click on the Relations button to change the parent of the term and its weight:
By default, the term is added directly under the root, meaning that it has no parent term. You can, however, make it the child of another term. The weight of the term determines its position with respect to the other terms on the List Terms page in the admin panel (this is the page on which the terms belonging to the particular vocabulary are listed). Terms with smaller weight are listed above terms with heavier weight. However, its easier to reorder all the terms directly on the List Terms page. We'll come to that a bit further down in this tutorial.
After you configure the settings don't forget to click on the Save button.
As we mentioned earlier, on the page that lists all the vocabularies if you click on the list terms button for a particular vocabulary it will show a table with the terms in that vocabulary:
In our example, we've added two terms to the Cities vocabulary.
When you hover the mouse pointer over a term you can see the URL address of that term. You can edit the settings of any of the terms by clicking on its edit button. The settings are the same as when adding a new term. You can also add new terms by clicking on the Add term button that's above the top left corner of the table. To reorder the terms in the table use the four-arrow button which is in front of the name of each term. Just hover the mouse pointer over the button, press and hold the left mouse button, and drag and drop the term in the desired spot. You can also reorder them by changing the row weights.
Whichever method you use, this will not only change the order of the terms in the table on the List Terms page, but also, for example, on the create/edit page for nodes from content types that have the Term reference field type added with the Select list or Checkboxes/radio buttons widget. This will consequently change the order of the tags that are displayed on the frontend. For more information on field types and widgets check out the tutorial on field types in Drupal.
By using the tabs that are at the top right corner of the page you can navigate between the List Terms page, the edit page on which you can change the name and description of the vocabulary, and the pages on which you can manage the taxonomy fields and how they are displayed:
By default, the fields that are added to any new vocabulary are for the name and the description of the terms in the vocabulary. You can add more fields, if you want to. You can also change the settings of the existing fields, and change which ones are to be visible or hidden on the frontend. For more information on how to do it check out the tutorial on managing fields in Drupal. It's written for content types in particular, but the principle of adding and managing taxonomy fields is exactly the same.
When you add a new field from the Manage fields tab, it will be added on the create/edit page for all terms from the particular vocabulary. And if you don't set the field to be hidden (done from the Manage display tab) the value of the field will also be displayed on the frontend. For instance, we want to add an image field to the terms from our example Cities vocabulary. So we go to the List Terms page of the vocabulary, we click on the Manage fields tab and we add an Image field type. We want it to be visible on the frontend so we go the Manage display tab and we make sure that it's not set to Hidden. Now we want to add an image to the New York term so we edit the term and we use the newly added field for uploading images. Once uploaded the image will be displayed on the frontend. It will be shown on the page listing all the nodes containing the New York tag (e.g. yourdomain.com/new-york and/or yourdomain.com/taxonomy/term/2).
Adding a Vocabulary to Content Types and Terms to Nodes
After you have created a vocabulary, in order to be able to include terms/tags in nodes that you publish on your site you have to add a Term reference field type to the content type(s). When you add the field you'll be prompted to choose a vocabulary. When you create/edit nodes from that content type you'll be able to select terms from that vocabulary. The terms are the tags that are displayed on the frontend and that provide a link to all nodes that contain the particular tag(s).
For example, we have created a content type and now we want to add tags to nodes from that content type. We want to use terms from our newly created example Cities vocabulary. So we click on the Structure tab in the top navigation bar of the admin panel. On the page that opens we click on Content types and on the next page we click on the manage fields button for the particular content type. On the page with the fields we type the name of the field in the text area for Add new field, and from the Select a field type drop-down menu we choose Term reference. With this field type you can select from three different widgets: Select list, Checkboxes/radio buttons, Autocomplete term widget.
With the widget Autocomplete term widget (tagging) when creating/editing nodes you can add tags that don't exist in the vocabulary group. The term will be automatically added in the vocabulary when the node is saved. For example, we have only New York and London as terms in the Cities vocabulary. When we create/edit nodes we can add Paris as a tag, despite the fact that it doesn't exist as a term in the vocabulary group. It will be added automatically. The Autocomplete term widget is also useful, if you have given a user role the permissions to create/edit content and you want users from that role to be able to add tags that don't exist as terms in the vocabulary group, but you don't want them to be able to edit the taxonomy settings. The preconfigured article content type that comes with Drupal has a Term reference field added with the Autocomplete term widget; it's configured to work with the preconfigured Tags vocabulary.
With the Select list and the Checkboxes/radio buttons widgets only existing terms can be selected when creating/editing nodes. With the Select list widget a drop-down menu will be available on the create/edit form for nodes. From that drop-down menu you can choose the terms that are in the particular vocabulary. The Checkboxes/radio buttons widget is similar, but in this case the terms can be selected by marking the respective radio buttons on the submission form.
After you select the widget click on the Save button and you'll be automatically taken to the page with the field settings for the Term reference field:
From the Vocabulary drop-down menu select the vocabulary that you want to use. Then click on the Save field settings button. This will automatically display the general settings for that field type (Term reference). From the Number of values drop-down menu you can select the number of tags that can be included in each node from that content type:
If you want to use more than one vocabulary group with a particular content type, just add more Term reference fields.
Once the field is added to the content type, when you create/edit nodes from that content type you can add tags from the vocabulary that you assigned to the field.
Of course, you can also include taxonomy terms in menus, for example. Just specify the path to the term when you add a link to the particular menu. For more information on menus check out the tutorial on managing menus in Drupal.