我正在迁移包含段落的内容。所以我有两个迁移文件。
迁移内容时。百事大吉。我可以看到段落都附在了节点上。
当我编辑已迁移的节点并向节点添加一个新的段落时,当我再次执行迁移时,该新段落将从该节点中移除。有什么办法能留住他们吗?
The paragraph_import.yml
langcode: en
status: true
id: paragraph_import
label: My paragraph import
migration_tags:
- paragraph
source:
plugin: csv
delimiter: ','
enclosure: '"'
escape: '`'
header_offset: 0
ids: ['personal_number']
destination:
plugin: 'entity_reference_revisions:paragraph'
default_bundle: my_paragraph
process:
mail:
- plugin: skip_on_empty
method: process
source: 'e-mail werk'
cellphone:
- plugin: skip_on_empty
method: process
source: 'gsm werk'
phone:
- plugin: skip_on_empty
method: process
source: telefoon
langcode:
- plugin: default_value
default_value: 'nl'
dependencies:
enforced:
module:
- migrate_source_csv节点迁移。
langcode: en
status: true
id: contact_import
label: Contacts
migration_tags:
- node
source:
plugin: csv
delimiter: ','
enclosure: '"'
escape: '`'
header_offset: 0
ids: ['personal_number']
destination:
plugin: 'entity:node'
default_bundle: contact
process:
type:
plugin: default_value
default_value: contact
title: title
paragraph:
- plugin: migration_lookup
migration: paragraph_import
source: personal_number
my_paragraph_field:
- plugin: sub_process
source:
- '@paragraph'
process:
target_id: '0'
target_revision_id: '1'
langcode:
- plugin: default_value
default_value: 'nl'
dependencies:
enforced:
module:
- migrate_source_csv我的迁移执行代码。
$migration = $this->migrationPluginManager->createInstance($migration_id);
$migration->getIdMap()->prepareUpdate();
try {
$executable = new MigrateExecutable($migration, new MigrateMessage());
$executable->import();
}
catch (MigrateException $exception) {
$this->logger->error($exception->getMessage());
}它与这个问题不同,因为这个问题涉及段落中的字段。这个问题是关于段落实体本身的。
发布于 2021-04-02 05:43:03
我已经为这个目的创建了一个自定义的源代码插件。
这是我的源代码插件。
class MYSOURCECSV extends CSV {
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
$database = \Drupal::database();
$table = 'migrate_map_migration_id';
// The migration removes the manually created entities from the system,
// that are not present in the migration source. But we want them to be
// ignored during the migration.
if ($database->schema()->tableExists($table)) {
$organisations = [];
try {
$results = $database->select($table, 'mmt')
->fields('mmt', ['destid1', 'destid2'])
->condition('sourceid1', $row->getSourceProperty('personal_number'))
->execute()
->fetchAll();
if (!empty($results)) {
foreach ($results as $result) {
$paragraph_id = $result->destid1;
$paragraph = \Drupal::entityTypeManager()
->getStorage('paragraph')
->load($paragraph_id);
if ($paragraph instanceof ParagraphInterface) {
$node = $paragraph->getParentEntity();
// If paragraph is already attached to a node. Get the ids from
// the node.
if ($node instanceof NodeInterface) {
$organisations = $node->get('my_paragraph_field')->getValue();
if ($key = array_search($paragraph_id, array_column($paragraphs, 'target_id'))) {
$paragraphs[$key]['target_revision_id'] = $result->destid2;
}
}
else {
$paragraphs[] = [
'target_id' => $result->destid1,
'target_revision_id' => $result->destid2,
];
}
}
}
}
$row->setSourceProperty('my_paragraph_field', $paragraphs);
}
catch (Exception $exception) {
\Drupal::logger('my_module')->error($exception->getMessage());
}
}
parent::prepareRow($row);
}
}然后将迁移文件中的段落字段映射更改为:
my_paragraph_field:
- plugin: sub_process
source: organisations
process:
target_id: target_id
target_revision_id: target_revision_idhttps://drupal.stackexchange.com/questions/301135
复制相似问题