Taxonomy consists one of the most important Drupal elements, as it gives the ability to the admins to label their content. In this way the content is categorized and becomes organised and easily accessible throughout the website.
The taxonomies are terms which are grouped into vocabularies. Every Taxonomy term belongs to a vocabulary which characterizes semantically its origin. For instance, in the “Entities Classification” article of “How to Plan” section, we provided an example of a booking website where each hotel can be tagged with its category . If we wanted to implement this website using Drupal CMS, we would create a “Hotel Category” vocabulary and within this vocabulary we would create 3 taxonomy terms: Hotel, Hostel and Aparthotel.
Another important characteristic of taxonomy vocabularies is the support of fields. Taxonomy Vocabularies are able to encapsulate a set of defined by the admin fields and this way you can store field values which can consequently be used by the content which is tagged with this taxonomy term. The concept of fields encapsulation in taxonomy terms can be proved really useful especially in complex content structures.
How to create taxonomy vocabulary
The taxonomy workspace of your website is located in Structure -> Taxonomy
The process required in order to create a taxonomy vocabulary and terms is the following:
- From the taxonomy workspace click on “Add Vocabulary”
- Fill the fields of your vocabulary. The fields are straightforward and clear.
- After Saving the vocabulary form, you are redirected to the terms page of the created vocabulary.
How to create taxonomy terms for a vocabulary
For the following steps, we assume that you have already created a vocabulary as described in the previous section.
Having created a vocabulary you can then create terms for it by clicking on “List Terms” button of the vocabulary you want to edit and then “Add Term”. The following form will guide you to create the new term for the vocabulary by filling:
- Name: The name of the term
- Description: Text to make the term more understandable.
As an admin, whenever you have to add a description, try to make it as descriptive as possible because it’s really likely that you won’t be the only one who will administer the website (maybe not now but in the future). As a result, try to make the future admins (or the future you) understand the meaning of the term.
- Relations: You can create relations between terms of the same vocabulary. In this way, you can have parent and child terms to keep the terms more organised.
- URL alias: You can give a URL alias to mask the default URL of the term.
How to tag content with taxonomy term(s)
As already mentioned in previous sections, taxonomy terms are used to tag the website’s content. Until now we showed how to create a content type, how to create content of a content type and how to create a taxonomy vocabulary. In order to tag content with a specific term or terms, the only thing you need to do is to map the content type with the vocabulary. This is simply done by creating a reference field in the content type to this vocabulary:
- Visit the fields page of the content type you want to tag.
- Click on “Add Field”.
- Select “Taxonomy Term” and give it a descriptive label.
- Choose whether you want to reference one or more taxonomy terms.
- Provide a nice and descriptive help text, choose whether or not you want the field to be required and finally choose from which vocabularies to be able to choose.
As you might have noticed, the “Taxonomy Term” field type is placed under the “Reference” field category. This category contains field that reference another entity in the website. The entity can be anything: a user, an image, a taxonomy term, even another content. Since Drupal 8 is based on Object Oriented techniques, almost everything is an object. In our case when we created a new taxonomy term, an object was created. In the same way when we use a reference, a pointer will be created from the content to the taxonomy term object.