mcpField
D
drupal-entity-api

drupal-entity-api

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

关于 drupal-entity-api

面向 Drupal 开发者的 Entity API 参考技能,涵盖实体存储操作、查询、字段访问和权限检查,并提供 PHP 示例。

主要功能

  1. 01按 ID 或属性加载实体
  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,
) {}

评论

支持 Markdown