关于 drupal-fields
drupal-fields 是面向 Drupal 开发者的字段开发技能,涵盖使用 Drush 创建和管理字段、导出配置、生成自定义字段插件及读取字段值。
主要功能
- 01使用 Drush 创建字段
- 02列出和删除字段
- 03CLI 修改后导出配置
- 04生成字段类型、格式化器和控件插件
- 05读取单值、引用及多值字段
适用场景
- 01为内容类型添加字段
- 02查看或删除现有字段
- 03实现自定义字段插件
- 04在 PHP 中读取字段数据
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ edutrul/ drupal-ai --skill drupal-fields安装后在 Claude Code 中输入/drupal-fields确认已加载。
附带文件README.md
详细介绍
Drupal 字段
创建字段(Drush — 命令行优先)
始终使用 drush field:create,而不是手动创建字段配置文件。
# Interactive (recommended first time)
drush field:create
# Non-interactive
drush field:create node article \
--field-name=field_subtitle \
--field-label="Subtitle" \
--field-type=string \
--field-widget=string_textfield \
--is-required=0 \
--cardinality=1
# Reference field
drush field:create node article \
--field-name=field_tags \
--field-label="Tags" \
--field-type=entity_reference \
--field-widget=entity_reference_autocomplete \
--cardinality=-1 \
--target-type=taxonomy_term
# Image field
drush field:create node article \
--field-name=field_image \
--field-label="Image" \
--field-type=image \
--field-widget=image_image \
--is-required=0 \
--cardinality=1
管理字段
# List all fields on a content type
drush field:info node article
# Delete a field
drush field:delete node.article.field_subtitle
# Discover available types/widgets/formatters
drush field:types
drush field:widgets
drush field:formatters
创建字段后导出
# Always export config after CLI changes
drush config:export -y
自定义字段类型(插件)
drush generate plugin:field:type
drush generate plugin:field:formatter
drush generate plugin:field:widget
在代码中访问字段值
// Single value
$value = $entity->get('field_name')->value;
// Entity reference
$referenced = $entity->get('field_ref')->entity;
// Multi-value
foreach ($entity->get('field_multi') as $item) {
$value = $item->value;
}
评论
支持 Markdown