mcpField
D
drupal-taxonomy

drupal-taxonomy

github.com/edutrul开发7353 次安装21 周前更新
代码仓库

关于 drupal-taxonomy

面向 Drupal 开发者的分类术语技能,涵盖术语加载、查询、创建、层级操作、字段读取和 Twig 访问。

主要功能

  1. 01按 ID 或属性加载术语
  2. 02查询并排序术语
  3. 03创建含描述和父级的术语
  4. 04遍历父级、子级和术语树
  5. 05读取术语字段并在 Twig 中渲染引用

适用场景

  1. 01在应用逻辑中获取分类术语
  2. 02筛选并排序词汇表中的术语
  3. 03构建层级术语结构
  4. 04在 Twig 模板中显示引用术语

安装

复制下面的命令,在终端中运行。

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

安装后在 Claude Code 中输入/drupal-taxonomy确认已加载。

附带文件README.md

详细介绍

Drupal 分类法

加载术语

// 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);

术语实体查询

$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);

创建术语

$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();

术语层级

// 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;
}

术语字段值

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

在 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 %}

评论

支持 Markdown