关于 drupal-migrations
面向 Drupal 开发者的迁移参考技能,涵盖迁移 YAML、处理插件、自定义插件及 Drush 命令,适用于构建和排查迁移流程。
主要功能
- 01迁移 YAML 结构示例
- 02常用处理插件速查
- 03实体引用与多值处理
- 04自定义源和处理插件示例
- 05迁移 Drush 命令参考
适用场景
- 01编写 Drupal 迁移配置
- 02映射或生成迁移实体
- 03通过 Drush 批量导入或回滚
- 04查看迁移状态与逐行错误
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ edutrul/ drupal-ai --skill drupal-migrations安装后在 Claude Code 中输入/drupal-migrations确认已加载。
附带文件README.md
详细介绍
Drupal 迁移
迁移 YAML 结构
id: my_migration
label: 'My Migration'
migration_group: my_group # group related migrations together
source:
plugin: source_plugin_name
process:
destination_field: source_field
destination:
plugin: 'entity:node'
default_bundle: article
migration_dependencies:
required:
- other_migration # must match actual migration IDs
Process 插件速查
| 插件 | 用途 |
|---|---|
default_value | 硬编码一个值 |
static_map | 将旧值映射 → 到新值 |
concat | 使用分隔符连接字段 |
migration_lookup | 引用之前迁移的实体(按旧 ID) |
entity_lookup | 按字段值引用现有实体 |
entity_generate | 如果实体不存在,则创建实体 |
skip_on_empty | 如果值为空,则跳过字段/行 |
skip_on_value | 如果字段等于某个值,则跳过行 |
sub_process | 遍历多值源数组 |
实体引用
process:
# Reference a previously migrated entity
uid:
plugin: migration_lookup
migration: users
source: author_id
# Reference an existing entity by field value
field_category:
plugin: entity_lookup
source: category_name
entity_type: taxonomy_term
bundle: categories
bundle_key: vid
value_key: name
# Create term if it doesn't exist
field_tags:
plugin: entity_generate
source: tag_name
entity_type: taxonomy_term
bundle: tags
value_key: name
多个值与条件
process:
# Iterate a multi-value source array
field_tags:
plugin: sub_process
source: tags
process:
target_id:
plugin: entity_generate
source: name
entity_type: taxonomy_term
bundle: tags
value_key: name
# Skip field processing if source is empty
field_image:
plugin: skip_on_empty
method: process
source: image_url
# Skip entire row based on a value
pseudo_skip:
plugin: skip_on_value
source: status
method: row
value: 'draft'
自定义源插件
namespace Drupal\my_module\Plugin\migrate\source;
use Drupal\migrate\Attribute\MigrateSource;
use Drupal\migrate\Plugin\migrate\source\SqlBase;
use Drupal\migrate\Row;
#[MigrateSource(id: 'legacy_products', source_module: 'my_module')]
final class LegacyProducts extends SqlBase {
public function query() {
return $this->select('legacy_products', 'p')
->fields('p', ['id', 'name', 'price'])
->condition('p.status', 'active');
}
public function fields() {
return [
'id' => $this->t('Product ID'),
'name' => $this->t('Name'),
'price' => $this->t('Price'),
];
}
public function getIds() {
return ['id' => ['type' => 'integer', 'alias' => 'p']];
}
public function prepareRow(Row $row) {
$row->setSourceProperty('price_with_tax', $row->getSourceProperty('price') * 1.21);
return parent::prepareRow($row);
}
}
自定义 Process 插件
namespace Drupal\my_module\Plugin\migrate\process;
use Drupal\migrate\Attribute\MigrateProcess;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
#[MigrateProcess(id: 'cents_to_decimal')]
final class CentsToDecimal extends ProcessPluginBase {
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (empty($value) || !is_numeric($value)) {
return NULL;
}
return number_format((float) $value / 100, 2, '.', '');
}
}
常用 Drush 命令
drush migrate:status # list all migrations + status
drush migrate:import migration_id # run
drush migrate:import migration_id --update # re-run and update existing records
drush migrate:import migration_id --limit=50 # batch to avoid memory issues
drush migrate:rollback migration_id # undo
drush migrate:reset-status migration_id # fix "migration is busy" state
drush migrate:messages migration_id # show per-row errors
评论
支持 Markdown