web-dev-qa-db-ja.com

イメージを移行するにはどうすればよいですか?

ブログ(Drupal 7)をDrupal 8.に移行する必要があります。8。フィールド移行に成功しましたが、画像フィールドを移行できません。プロセスプラグインで試行しましたが、何も起こりませんでした。 。ファイルの移行はすでに行われています。

これは設定ファイルです

id: custom_blogs
label: Custom Blogs Migration

source:
  plugin: custom_blogs

destination:
  plugin: entity:node

process:
  # Field mappings and transformations will go here. We will get to this in a minute.
  nid: nid
  type:
    plugin: default_value
    default_value: ct_blogs
  langcode:
    plugin: static_map
    bypass: true
    source: language
    map:
      und: en
  title: title
  uid: uid
  status: status
  'body/value': body_value
  'body/format':
    plugin: static_map
    bypass: true
    source: body_format
    map:
      1: plain_text
      2: basic_html
      3: full_html
  'body/summary': teaser
  'field_ct_blog_image_caption/value': field_blog_image_caption_value
  'field_ct_blog_image_caption/format':
    plugin: static_map
    bypass: true
    source: field_blog_image_caption_format
    map:
      1: plain_text
      2: basic_html
      3: full_html
      4: filtered_html
  'field_ct_blog_image_caption/summary': field_blog_image_caption_teaser
  field_ct_blogs_tags: tags
  field_ct_blogs_image: # Image field name in Drupal 8 site
    plugin: iterator
    source: field_image # Image field name in Drupal 7 site
    process:
      target_id:
        plugin: migration
        migration: custom_blogs_images
        source: fid
      alt: field_image_alt
      title: field_image_alt
      width: field_image_width
      height: field_image_height

これはプロセスプラグインです

<?php
/**
 * @file
 * Contains \Drupal\migrate_custom\Plugin\migrate\source\Blogs.
 */

namespace Drupal\custom_migration\Plugin\migrate\source;

use Drupal\migrate\Row;
use Drupal\migrate\Plugin\migrate\source\SqlBase;

/**
 * Extract users from Drupal 7 database.
 *
 * @MigrateSource(
 *   id = "custom_blogs"
 * )
 */
class Blogs extends SqlBase {

  /**
   * {@inheritdoc}
   */
  public function query() {
    $query = $this->select('node', 'n')
      ->condition('n.type', 'blog')
      ->fields('n', array(
        'nid',
        'vid',
        'type',
        'language',
        'title',
        'uid',
        'status',
        'created',
        'changed',
        'promote',
        'sticky',
      ));

    $query->orderBy('nid');
    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
    return [
      'nid' => $this->t('Node ID'),
      'vid' => $this->t('Version ID'),
      'type' => $this->t('Type'),
      'title' => $this->t('Title'),
      'format' => $this->t('Format'),
      'teaser' => $this->t('Teaser'),
      'uid' => $this->t('Authored by (uid)'),
      'created' => $this->t('Created timestamp'),
      'changed' => $this->t('Modified timestamp'),
      'status' => $this->t('Published'),
      'promote' => $this->t('Promoted to front page'),
      'sticky' => $this->t('Sticky at top of lists'),
      'language' => $this->t('Language (fr, en, ...)'),
      'body/value' => $this->t('Value of body'),
      'body/format' => $this->t('Format of body'),
      'body/summary' => $this->t('Summary of body'),
      'field_blog_image_caption/value' => $this->t('Value of Image Caption'),
      'field_blog_image_caption/format' => $this->t('Format of Image Caption'),
      'field_blog_image_caption/summary' => $this->t('Summary of Image Caption'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
    // Find parents for this row.
    $nid = $row->getSourceProperty('nid');

    $result = $this->getDatabase()->query('
      SELECT
        fld.body_value,
        fld.body_format,
        fld.body_summary
      FROM
        {field_data_body} fld
      WHERE
        fld.entity_id = :nid
    ', array(':nid' => $nid));
    foreach ($result as $record) {
      $row->setSourceProperty('body_value', $record->body_value );
      $row->setSourceProperty('body_format', $record->body_format );
      $row->setSourceProperty('teaser', $record->body_summary );
    }

    // Image Caption
    $result = $this->getDatabase()->query('
      SELECT
        fld.field_blog_image_caption_value,
        fld.field_blog_image_caption_format,
        fld.field_blog_image_caption_summary
      FROM
        {field_data_field_blog_image_caption} fld
      WHERE
        fld.entity_id = :nid
    ', array(':nid' => $nid));
    foreach ($result as $record) {
      $row->setSourceProperty('field_blog_image_caption_value', $record->field_blog_image_caption_value );
      $row->setSourceProperty('field_blog_image_caption_format', $record->field_blog_image_caption_format );
      $row->setSourceProperty('field_blog_image_caption_teaser', $record->field_blog_image_caption_summary );
    }

    // Images
    $result = $this->getDatabase()->query('
      SELECT
        -- fld.field_image_fid,
        fld.field_image_alt,
        fld.field_image_title,
        fld.field_image_width,
        fld.field_image_height
      FROM
        {field_data_field_image} fld
      WHERE
        fld.entity_id = :nid
    ', array(':nid' => $nid));
    // Create an associative array for each row in the result. The keys
    // here match the last part of the column name in the field table.
    $images = [];
    foreach ($result as $record) {
      $images[] = [
        // 'target_id' => $record->field_files_fid,
        'field_image_alt' => $record->field_image_alt,
        'field_image_title' => $record->field_image_title,
        'field_image_width' => $record->field_image_width,
        'field_image_height' => $record->field_image_height,
      ];
    }
    $row->setSourceProperty('field_image', $images);


    // Tags
    $result = $this->getDatabase()->query('
      SELECT
        GROUP_CONCAT(fld.field_tags_tid) as tids
      FROM
        {field_data_field_tags} fld
      WHERE
        fld.entity_id = :nid
    ', array(':nid' => $nid));
    foreach ($result as $record) {
      if (!is_null($record->tids)) {
        $row->setSourceProperty('tags', explode(',', $record->tids) );
      }
    }

    return parent::prepareRow($row);
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    $ids['nid']['type'] = 'integer';
    $ids['nid']['alias'] = 'n';
    return $ids;
  }
}

私が間違っていることについて何か考えはありますか?

4
Aman

プロセスプラグインとして「migration」の代わりに「migration_lookup」を使用してみてください。これは、画像フィールド 'field_image'に移行するために使用したものです。

  field_image:
    plugin: iterator
    source: field_image
    process:
      target_id:
        plugin: migration_lookup
        migration: id_of_file_migration
        source: fid
      alt: alt
      title: title
      height: height
      width: width
3
John

https://github.com/jigarius/drupal-migration-example/ に問題の修正に役立つサンプルプロジェクトがあります。

このサンプルモジュールは Drupal 8の移行:ファイル/イメージの移行(パート3) で説明されています。これは、知っておく必要がある重要な部分です。

最初に、各ファイルのファイルエンティティを作成する必要があります。これは、Drupalがファイルを独自のIDを持つファイルエンティティとして扱うためです。次に、Drupalがノードとファイルの関連付けをエンティティ参照として扱い、彼らのID。

2