Working with Drupal Taxonomy
Last Updated: 23 May 2025
Working with Drupal Taxonomy
Taxonomy in Drupal is a powerful feature that helps you classify and organize your content. It enables you to create categories, tags, or vocabularies to manage and structure your website effectively. This tutorial will guide you through the basics of Drupal taxonomy and how to use it to streamline content management on your site.
What is Drupal Taxonomy?
Taxonomy is a way to classify content using terms grouped into vocabularies. For example:
- Vocabulary: "Blog Categories"
- Terms: "Tech", "Lifestyle", "Travel"
- Vocabulary: "Tags"
- Terms: "Drupal", "PHP", "Tutorials"
By assigning these terms to your content, you can easily group and filter it.
Steps to Create and Use Taxonomy
1. Create a Vocabulary
- Go to Structure > Taxonomy.
- Click Add Vocabulary.
- Enter a name for your vocabulary (e.g., "Tags") and a description.
- Click Save.
2. Add Terms to the Vocabulary
- Under the newly created vocabulary, click Add Terms.
- Enter the term name (e.g., "Drupal") and description (optional).
- Save your term.
3. Add Taxonomy to Content Types
- Go to Structure > Content Types.
- Edit the content type (e.g., Article).
- Click Manage Fields.
- Add a new field:
- Label: "Tags"
- Field Type: Term Reference
- Widget: Autocomplete (Tags Style) or Checkboxes/Radio Buttons
- Save the field.
4. Assign Terms to Content
- When creating or editing content (e.g., an Article), you'll see the "Tags" field.
- Start typing a term, or select one from the list.
- Save the content to assign the terms.
Displaying Taxonomy Terms on Your Site
Using Views
- Go to Structure > Views.
- Click Add View and name it (e.g., "Tagged Content").
- Select the content type (e.g., Articles).
- Add a filter for the taxonomy field (e.g., Tags).
- Configure the display and save the view.
Using Blocks
You can display a list of taxonomy terms in a block:
- Go to Structure > Taxonomy.
- Click the dropdown for the vocabulary and select List Terms.
- Use the taxonomy term URL to create menu links or use blocks.
Benefits of Using Taxonomy
- Improved Organization: Helps users find related content easily.
- Dynamic Content Filtering: Enables advanced filtering using Views.
- SEO Optimization: Helps search engines understand your site's structure.
Example Use Case
Imagine you’re running a Drupal-based blog. You can:
- Create a vocabulary called Categories with terms like "Tech", "Lifestyle", and "Travel".
- Assign categories to articles, and then create a view that displays articles based on these categories.
With Drupal taxonomy, you can transform your website into a well-organized and user-friendly platform. Start experimenting today to unlock its full potential!
Let me know if you want any specific examples or more advanced use cases for taxonomy!
Adding taxonomy via code
// Example of defining a vocabulary programmatically in Drupal.
use Drupal\taxonomy\Entity\Vocabulary;
$vocabulary = Vocabulary::create([
'id' => 'example_tags',
'vid' => 'example_tags',
'description' => 'A vocabulary for example tags.',
'name' => 'Example Tags',
]);
$vocabulary->save();
When you execute the above code, it will automatically add a new taxonomy vocabulary under taxonomy:

Now let's add terms programmatically by running this code:
<?php
use Drupal\taxonomy\Entity\Term;
// Create a term using the Term entity class and storing in a variable
$term = Term::create([
'name' => 'Test term 1',
'vid' => 'example_tags', // Replace with your vocabulary machine name, it must be a valid vocabulary.
]);
$term->save();
// OR this can be done using the Term entity class directly.
Term::create([
'name' => 'Test term 2',
'vid' => 'example_tags', // Replace with your vocabulary machine name, it must be a valid vocabulary.
])->save();
The above code will create terms for our custom vocabulary that we created in previous step:
This a just an example and in a real word scenario, you would be using dynamic variables from different sources.
If you have any question, please feel free to write us and we will update the tutorial!