drupal-taxonomy について
Drupal 開発者向けに、タクソノミー用語の読み込み、検索、作成、階層操作、フィールド参照、Twig での利用を扱うスキルです。
主な機能
- 01ID や属性による用語の読み込み
- 02用語の検索と並べ替え
- 03説明や親を指定した用語の作成
- 04親・子・ツリー構造の取得
- 05用語フィールドの参照とTwigでの表示
活用シーン
- 01アプリケーション処理で用語を取得
- 02ボキャブラリー内の用語を絞り込み、整列
- 03階層型の用語構造を構築
- 04Twigテンプレートで参照先の用語を表示
インストール
コマンドをコピーしてターミナルで実行します。
$
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 対応