首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在迁移过程中保留未映射的段落?

如何在迁移过程中保留未映射的段落?
EN

Drupal用户
提问于 2021-03-25 07:17:17
回答 1查看 52关注 0票数 0

我正在迁移包含段落的内容。所以我有两个迁移文件。

迁移内容时。百事大吉。我可以看到段落都附在了节点上。

当我编辑已迁移的节点并向节点添加一个新的段落时,当我再次执行迁移时,该新段落将从该节点中移除。有什么办法能留住他们吗?

The paragraph_import.yml

代码语言:javascript
复制
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

节点迁移。

代码语言:javascript
复制
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

我的迁移执行代码。

代码语言:javascript
复制
$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());
 }

它与这个问题不同,因为这个问题涉及段落中的字段。这个问题是关于段落实体本身的。

EN

回答 1

Drupal用户

回答已采纳

发布于 2021-04-02 05:43:03

我已经为这个目的创建了一个自定义的源代码插件。

这是我的源代码插件。

代码语言:javascript
复制
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);
  }

}

然后将迁移文件中的段落字段映射更改为:

代码语言:javascript
复制
my_paragraph_field:
    - plugin: sub_process
      source: organisations
      process:
        target_id: target_id
        target_revision_id: target_revision_id
票数 0
EN
页面原文内容由Drupal提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://drupal.stackexchange.com/questions/301135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档