$npx -y skills add edutrul/drupal-ai --skill drupal-taxonomyDrupal Taxonomy — loading taxonomy terms, vocabularies, term hierarchy, taxonomy term reference fields, and Twig or PHP patterns for taxonomy entities.
| 1 | # Drupal Taxonomy |
| 2 | |
| 3 | ## Loading Terms |
| 4 | |
| 5 | ```php |
| 6 | // Load term by ID |
| 7 | $term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid); |
| 8 | |
| 9 | // Load multiple terms |
| 10 | $terms = $this->entityTypeManager->getStorage('taxonomy_term')->loadMultiple($tids); |
| 11 | |
| 12 | // Load terms by vocabulary |
| 13 | $terms = $this->entityTypeManager->getStorage('taxonomy_term')->loadByProperties([ |
| 14 | 'vid' => 'tags', |
| 15 | 'status' => 1, |
| 16 | ]); |
| 17 | |
| 18 | // Load term by name |
| 19 | $terms = $this->entityTypeManager->getStorage('taxonomy_term')->loadByProperties([ |
| 20 | 'vid' => 'tags', |
| 21 | 'name' => 'My Tag', |
| 22 | ]); |
| 23 | $term = reset($terms); |
| 24 | ``` |
| 25 | |
| 26 | ## Entity Query for Terms |
| 27 | |
| 28 | ```php |
| 29 | $query = $this->entityTypeManager->getStorage('taxonomy_term')->getQuery() |
| 30 | ->accessCheck(TRUE) |
| 31 | ->condition('vid', 'tags') |
| 32 | ->condition('status', 1) |
| 33 | ->sort('weight', 'ASC') |
| 34 | ->sort('name', 'ASC'); |
| 35 | |
| 36 | $tids = $query->execute(); |
| 37 | $terms = $this->entityTypeManager->getStorage('taxonomy_term')->loadMultiple($tids); |
| 38 | ``` |
| 39 | |
| 40 | ## Creating Terms |
| 41 | |
| 42 | ```php |
| 43 | $term = $this->entityTypeManager->getStorage('taxonomy_term')->create([ |
| 44 | 'vid' => 'tags', |
| 45 | 'name' => 'New Tag', |
| 46 | 'description' => [ |
| 47 | 'value' => 'Tag description', |
| 48 | 'format' => 'basic_html', |
| 49 | ], |
| 50 | 'parent' => [['target_id' => $parent_tid]], |
| 51 | 'weight' => 0, |
| 52 | ]); |
| 53 | $term->save(); |
| 54 | ``` |
| 55 | |
| 56 | ## Term Hierarchy |
| 57 | |
| 58 | ```php |
| 59 | // Get parent terms |
| 60 | $parents = $this->entityTypeManager->getStorage('taxonomy_term')->loadParents($tid); |
| 61 | |
| 62 | // Get children |
| 63 | $children = $this->entityTypeManager->getStorage('taxonomy_term')->loadChildren($tid); |
| 64 | |
| 65 | // Get all tree (flat) |
| 66 | $tree = $this->entityTypeManager->getStorage('taxonomy_term')->loadTree('tags'); |
| 67 | foreach ($tree as $item) { |
| 68 | $tid = $item->tid; |
| 69 | $name = $item->name; |
| 70 | $depth = $item->depth; |
| 71 | $parents = $item->parents; |
| 72 | } |
| 73 | ``` |
| 74 | |
| 75 | ## Term Field Values |
| 76 | |
| 77 | ```php |
| 78 | $name = $term->getName(); // getName() shortcut |
| 79 | $vid = $term->bundle(); // Vocabulary ID |
| 80 | $tid = $term->id(); |
| 81 | $description = $term->get('description')->value; |
| 82 | $weight = $term->getWeight(); |
| 83 | ``` |
| 84 | |
| 85 | ## In Twig |
| 86 | |
| 87 | ```twig |
| 88 | {# Render term field #} |
| 89 | {{ content.field_tags }} |
| 90 | |
| 91 | {# Access term entity from a reference field #} |
| 92 | {% for item in node.field_tags %} |
| 93 | {{ item.entity.name.value }} |
| 94 | {% endfor %} |
| 95 | ``` |