Blog IT

We know Taxonomy is a powerful core module and helps us categorize the content. A vocabulary in Drupal is essentially a container for terms that are used by the system. These terms are what Drupal uses to connect certain areas together within the website.

In Drupal, we can see the taxonomy module at Administer>Structure>Taxonomy. From here we can add the vocabulary and terms. We can also create taxonomy vocabularies and terms programmatically. Before you add a term, you need to make sure that the vocabulary is already there. Here is a way to create Taxonomy Vocabulary when you install the module for the first time.

<?php
$terms = ['Term One', 'Term Two', 'Term Three', 'Term Four'];

foreach ($terms as $term_name) {
    $term = \Drupal\taxonomy\Entity\Term::create([
        'vid' => 'my_taxonomy', // example: tags
        'name' => $term_name,
    ]);
    $term->save();
}
?>