Imagine your website as a large library with lots of books on shelves. Of course, you, just like your staff and your visitors, will appreciate it if all the items are sorted in the most consistent way.
In this idyllic picture, all books are situated on the right shelves and conveniently labeled, they are easy to find and manage, and different categories of users have access to certain archives. You might call this the “perfect order” and that’s what can be created on your website.
This structure is possible thanks to a tool for organizing content on Drupal websites known as taxonomy. The tool is built into the Drupal core, being one of its most applicable and popular features. Contributed modules are also available if you need to enhance the standard functionality.
This blog post will cover:
- What Drupal taxonomy is and how it works
- Offer an easy step-by-step guide on how to set it up
- Describe some contributed modules that provide additional enhancements
Introduction to Drupal Taxonomy
Taxonomy in Drupal is the name for the practice of classifying or categorizing content, as well as the Drupal core module that does it. You can associate taxonomy terms with specific content, which is useful for organizing and displaying it based on its classification.
Terms and vocabularies are the key concepts in Drupal taxonomy:
- A taxonomy vocabulary is a set (group, list) of taxonomy terms. A standard example could be a set of categories for a website’s blog.
- A taxonomy term is an individual item as part of a vocabulary. For example, “Science,” “Lifestyles,” “Travel,” and other specific categories for the blog.
Based on structure, a vocabulary can be:
- flat (just a set of terms)
- hierarchical, or nested (a set of terms that further expand into child terms, which would represent subcategories)
In addition, vocabularies can be configured for content to use a single term only, or multiple terms.
Where is Drupal taxonomy used?
Drupal taxonomy can be used in numerous scenarios that involve classifying content, for example:
- blogs classified by categories
- tags added to content
- countries, cities, genders, and similar choices in a user registration form
- recipes classified by ingredients, cuisine, dish type, difficulty level, etc.
- e-commerce products classified by category, brand, collection, etc.
- states in workflows (in progress, draft, published, etc.)
- content access levels (public, private, admins only, etc.)
- menu, navigation, and URL schemas
- choice of display options
- and many more
How to create taxonomy in Drupal: a step-by-step guide
1) Creating a vocabulary
Let’s see all this in action and create a set of categories for a fictional university website’s news section. We will start by going to Structure > Taxonomy and clicking “Add vocabulary.”
We can name our vocabulary something like “News categories” and optionally add a description for it or just leave the description field blank.
After clicking “Save,” we will be taken to the page where we can add terms to our vocabulary.
We will enter the name of the first news category, leaving the optional description field blank. There is a very handy option at the bottom of the page to save and go to the list instead of just saving the term. It’s perfect for us because we are going to keep adding more news category items.
If we want to add a child term, we can specify the parent term in the “Relations” tab in the process of the term creation.
However, the hierarchy of terms can always be changed in the list of terms on the vocabulary page that we can access and manage at any time by clicking “Edit vocabulary” and then “List.” It’s possible to rearrange the terms using the easy drag-and-drop functionality. If we use the drag handles  to slide a term under and to the right of another term, it will become its child term.
2) Adding taxonomy to a content type
Let’s go to Structure > Content types and find our News content type (this can be any other content type you want to use a vocabulary for). On the Manage fields tab, we are going to click “Add field.”
We will select the “Taxonomy term” field type from the dropdown menu, create a label to name it (e.g. “Categories”), and click “Save and continue.”
Next, we are asked to choose the type of item to reference where the Taxonomy term is the one we want. The allowed number of values can be set to unlimited in our case. Finally, we will click “Save field settings” at the bottom of the page.
On the next page of the vocabulary settings, we will make sure the reference method is Default and then select the specific vocabulary that we want to attach to News. In our case, this is the newly created “News categories” vocabulary. It’s also possible to provide the default value that will be assigned to newly created blogs automatically. We will leave this blank.
After saving the result, we are almost done. However, let’s make the final touches and configure how the categories will be presented on the content editing form. We will switch to the Manage form display tab of the News content type. Next to the Categories field, we can choose the form in which they will be shown to the editors: select list (drop-down), checkboxes/radio buttons, autocomplete, or tags style autocomplete.
The choice of the display depends on your team’s preferences and the complexity of the vocabulary on your Drupal website. The checkboxes/radio buttons option could be handy for editors to view the full list of categories, and that’s what we will choose. Clicking “Save” is required just like on every other step of the configuration.
Finally, whenever we add a new piece of news by going to Content > Add content > News, we can see a set of categories to choose from as we post a new blog post. Great, the setup is successfully completed!
Top contributed modules for taxonomy in Drupal
The community has created plenty of excellent contributed modules to add more possibilities for working with Drupal taxonomy. Let’s run through some of the most interesting ones right now.
The Taxonomy Manager module offers advanced taxonomy management options on Drupal websites. A vocabulary is displayed as a dynamic tree with expandable and collapsible parent items. The module enables you to add terms in bulk to all levels of the hierarchy, find terms using a search feature, export them in a CSV file, and much more.
- Taxonomy Multi-delete Terms
If you do not need all the functionality of the above-described module, you could opt for a lightweight tool that focuses on a specific action. For example, the Taxonomy Multi-delete Terms module will help you bulk delete terms. All you need is to select the terms and click the delete button.
If you are using hierarchical vocabularies on your website, the Hierarchical Select module could be very useful to you. It provides a form widget for selecting items in a hierarchy in enhanced ways. The module can be configured to save the entire term lineage (the term with all its ancestors) or to save the deepest term only. It’s also possible to define from which level in a vocabulary users can select items (from the deepest level only or from any level). Additionally, you can assign labels to levels, limit the number of items that can be selected, and do some other useful settings.
With the Taxonomy access fix module, you can fine-tune user access to manage specific vocabularies. The module adds the permissions to reorder terms and view terms on a per-vocabulary basis. These permissions can be edited on the People > Permission page of the Drupal admin dashboard. In addition, the module removes the vocabularies from the vocabulary overview page for users who do not have permission to manage them.
- Taxonomy Access Control Lite
With the Taxonomy Access Control Lite module, you will be able to control the permissions to view, edit, and delete content based on taxonomy. For example, you can create a vocabulary with “Private,” “Public,” “Staff only,” or other terms according to your workflows and attach them to content nodes for access control. The permissions can be configured per role, as well as for individual users.
Another module for user access based on taxonomy is the Permissions by Term. It comes in handy when you need to restrict access to content nodes and taxonomy terms based on term-to-user relations. The term-related permissions can be tied to entire user roles or individual accounts. The Permissions by Entity submodule extends this functionality to other entities as well, for example, Media entities.
- Views Taxonomy Term Name Depth
The Views Taxonomy Term Name Depth module enhances the capabilities of Drupal Views — a powerful tool to display content based on settings. More specifically, the module enables more flexible settings in Drupal Views contextual filters. It adds a contextual filter “Has taxonomy term NAME with Depth.” This makes sure the content is displayed on the website if it has the selected terms or their child terms.
We have mentioned that Drupal taxonomy enables you to assign tags to content. The Similar By Terms module offers the functionality to display a block of similar content based on taxonomy tags assigned to content items. The block includes links to individual content nodes and is displayed via Drupal Views.
- Taxonomy Term Reference Tree Widget
The Taxonomy Term Reference Tree Widget module offers an advanced display for the term reference field. It is meant to replace the core widget which is a list of checkboxes or radio buttons. Instead, the module provides an expandable tree widget with additional features such as filtering and sorting the options via Drupal Views, starting with the taxonomy tree minimized or maximized, and so on.
It is easy to create menu items from the taxonomy page of your Drupal website if you use the Taxonomy Menu UI module. It shows “Available Menus” for you to choose from on the vocabulary editing page. Next, this Drupal module enables you to specify a menu item for each term on its add/edit form.
You can make sure your terms are never duplicated if you rely on the Taxonomy unique module. It will prohibit a user from saving a term if one with the same name already exists. This is configurable on a per-vocabulary basis. You can also customize the error message that will be shown to users in the case of finding duplicates.
To sum up
Taxonomy in Drupal comes in handy in a great variety of use cases on your website. It enables developers to create scenarios of any complexity, while the basic workflows of choosing/adding/removing terms are pretty straightforward for non-tech editors. If you need help organizing content on your site, you’re welcome to chat with our Drupal experts!