mcpField
D
drupal-taxonomy

drupal-taxonomy

github.com/edutrulDevelopment7353 installsUpdated 21w ago
Repository

About drupal-taxonomy

A Drupal taxonomy skill covering term loading, querying, creation, hierarchy, fields, and Twig access for Drupal developers.

Key features

  1. 01Load terms by ID or properties
  2. 02Query and sort terms
  3. 03Create terms with descriptions and parents
  4. 04Traverse parent, child, and tree structures
  5. 05Read term fields and render references in Twig

Use cases

  1. 01Retrieve taxonomy terms for application logic
  2. 02Filter and order terms within a vocabulary
  3. 03Build hierarchical term structures
  4. 04Display referenced terms in Twig templates

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/edutrul/drupal-ai --skill drupal-taxonomy

After installing, type/drupal-taxonomyin Claude Code to confirm it loaded.

Bundled filesREADME.md

Details

From the project README

Drupal Taxonomy

Loading Terms

// Load term by ID
$term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid);

// Load multiple terms
$terms = $this->entityTypeManager->getStorage('taxonomy_term')->loadMultiple($tids);

// Load terms by vocabulary
$terms = $this->entityTypeManager->getStorage('taxonomy_term')->loadByProperties([
  'vid' => 'tags',
  'status' => 1,
]);

// Load term by name
$terms = $this->entityTypeManager->getStorage('taxonomy_term')->loadByProperties([
  'vid' => 'tags',
  'name' => 'My Tag',
]);
$term = reset($terms);

Entity Query for Terms

$query = $this->entityTypeManager->getStorage('taxonomy_term')->getQuery()
  ->accessCheck(TRUE)
  ->condition('vid', 'tags')
  ->condition('status', 1)
  ->sort('weight', 'ASC')
  ->sort('name', 'ASC');

$tids = $query->execute();
$terms = $this->entityTypeManager->getStorage('taxonomy_term')->loadMultiple($tids);

Creating Terms

$term = $this->entityTypeManager->getStorage('taxonomy_term')->create([
  'vid' => 'tags',
  'name' => 'New Tag',
  'description' => [
    'value' => 'Tag description',
    'format' => 'basic_html',
  ],
  'parent' => [['target_id' => $parent_tid]],
  'weight' => 0,
]);
$term->save();

Term Hierarchy

// Get parent terms
$parents = $this->entityTypeManager->getStorage('taxonomy_term')->loadParents($tid);

// Get children
$children = $this->entityTypeManager->getStorage('taxonomy_term')->loadChildren($tid);

// Get all tree (flat)
$tree = $this->entityTypeManager->getStorage('taxonomy_term')->loadTree('tags');
foreach ($tree as $item) {
  $tid = $item->tid;
  $name = $item->name;
  $depth = $item->depth;
  $parents = $item->parents;
}

Term Field Values

$name = $term->getName();           // getName() shortcut
$vid = $term->bundle();             // Vocabulary ID
$tid = $term->id();
$description = $term->get('description')->value;
$weight = $term->getWeight();

In Twig

{# Render term field #}
{{ content.field_tags }}

{# Access term entity from a reference field #}
{% for item in node.field_tags %}
  {{ item.entity.name.value }}
{% endfor %}

Comments

Markdown supported