mcpField
D
drupal-entity-api

drupal-entity-api

github.com/edutrul開発7351 回インストール21 週間前に更新
リポジトリ

drupal-entity-api について

Drupal のエンティティを扱う開発者向けの Entity API リファレンススキルです。ストレージ操作、クエリ、フィールドアクセス、アクセスチェックを PHP の例とともに紹介します。

主な機能

  1. 01ID やプロパティによるエンティティのロード
  2. 02条件やソートを使ったエンティティクエリ
  3. 03エンティティの作成・更新・削除
  4. 04フィールド値とフォーマットの取得
  5. 05エンティティのアクセス権チェック

活用シーン

  1. 01コンテンツ操作対象の Drupal エンティティ検索
  2. 02絞り込みやページ分割を伴うクエリの作成
  3. 03保存済みエンティティの変更や削除

インストール

コマンドをコピーしてターミナルで実行します。

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

インストール後、Claude Code で/drupal-entity-apiを入力して読み込まれたか確認します。

同梱ファイルREADME.md

README(原文)English

プロジェクトの README より

この README の日本語訳はまだありません。以下は英語の原文です。日本語の紹介はページ上部をご覧ください。

Drupal Entity API

Loading Entities

// Load a single node
$node = $this->entityTypeManager->getStorage('node')->load($nid);

// Load multiple nodes
$nodes = $this->entityTypeManager->getStorage('node')->loadMultiple([1, 2, 3]);

// Load by properties
$nodes = $this->entityTypeManager->getStorage('node')->loadByProperties([
  'type' => 'article',
  'status' => 1,
  'uid' => $uid,
]);

// Load user
$user = $this->entityTypeManager->getStorage('user')->load($uid);

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

// Load media
$media = $this->entityTypeManager->getStorage('media')->load($mid);

Entity Query

$query = $this->entityTypeManager->getStorage('node')->getQuery()
  ->accessCheck(TRUE)
  ->condition('type', 'article')
  ->condition('status', 1)
  ->condition('field_tags', $tid)
  ->sort('created', 'DESC')
  ->range(0, 10);

$nids = $query->execute();
$nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nids);

Creating Entities

// Create a node
$node = $this->entityTypeManager->getStorage('node')->create([
  'type' => 'article',
  'title' => 'My Article',
  'status' => 1,
  'uid' => $this->currentUser->id(),
  'field_body' => [
    'value' => 'Content here',
    'format' => 'basic_html',
  ],
]);
$node->save();

// Create a taxonomy term
$term = $this->entityTypeManager->getStorage('taxonomy_term')->create([
  'vid' => 'tags',
  'name' => 'New Tag',
]);
$term->save();

Updating Entities

$node = $this->entityTypeManager->getStorage('node')->load($nid);
if ($node instanceof NodeInterface) {
  $node->set('title', 'Updated Title');
  $node->set('field_body', ['value' => 'New content', 'format' => 'basic_html']);
  $node->save();
}

Deleting Entities

$node = $this->entityTypeManager->getStorage('node')->load($nid);
if ($node) {
  $node->delete();
}

// Delete multiple
$storage = $this->entityTypeManager->getStorage('node');
$nodes = $storage->loadMultiple($nids);
$storage->delete($nodes);

Accessing Field Values

// Simple field
$title = $node->get('title')->value;
$status = $node->get('status')->value;

// Text with format
$body = $node->get('body')->value;
$format = $node->get('body')->format;

// Reference field
$tid = $node->get('field_tags')->target_id;
$term = $node->get('field_tags')->entity;

// Multi-value field
foreach ($node->get('field_images') as $item) {
  $fid = $item->target_id;
  $file = $item->entity;
}

// Check if field is empty
if (!$node->get('field_image')->isEmpty()) { ... }

Access Checking

// Always add access checks
$query->accessCheck(TRUE);

// Check entity access
if ($node->access('view', $account)) { ... }
if ($node->access('update', $account)) { ... }

Inject EntityTypeManager

public function __construct(
  private readonly EntityTypeManagerInterface $entityTypeManager,
) {}

コメント