web-dev-qa-db-fra.com

Comment migrer des images?

Je dois migrer mes blogs (Drupal 7) vers Drupal 8. J'ai essayé et réussi la migration de champs mais je ne suis pas en mesure de migrer mon champ d'image. J'ai essayé par plugin de processus, rien ne s'est passé La migration des fichiers a déjà été effectuée.

Ceci est un fichier de configuration

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

Ceci est un plugin de processus

<?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;
  }
}

Une idée de ce que je fais mal?

4
Aman

Vous pouvez essayer d'utiliser "migration_lookup" au lieu de "migration" comme plug-in de processus. Voici ce que j'ai utilisé pour migrer dans un champ d'image "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

Il y a un exemple de projet sur https://github.com/jigarius/drupal-migration-example/ qui peut vous aider à résoudre votre problème.

Cet exemple de module est expliqué dans Migration Drupal 8: Migration de fichiers/images (Partie 3) . C'est une partie importante que vous devez savoir:

Nous devons d'abord créer des entités de fichier pour chaque fichier. Cela est dû au fait que Drupal traite les fichiers comme des entités de fichier qui ont leur propre ID. Ensuite Drupal traite les associations nœud-fichier comme des références d'entité, se référant aux entités de fichier avec leurs identifiants.

2