<?xml version="1.0" encoding="utf-8"?>
<!-- generator="Kirby" -->
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">

  <channel>
    <title>Mot-cl&#233;: migrate api &#183; Blog &#183; Liip</title>
    <link>https://www.liip.ch/fr/blog/tags/migrate+api</link>
    <generator>Kirby</generator>
    <lastBuildDate>Tue, 21 Aug 2018 00:00:00 +0200</lastBuildDate>
    <atom:link href="https://www.liip.ch" rel="self" type="application/rss+xml" />

        <description>Articles du blog Liip avec le mot-cl&#233; &#8220;migrate api&#8221;</description>
    
        <language>fr</language>
    
        <item>
      <title>Migrate File Entities to Media Entities in Drupal 8</title>
      <link>https://www.liip.ch/fr/blog/migrate-file-entities-to-media-entities-in-drupal-8</link>
      <guid>https://www.liip.ch/fr/blog/migrate-file-entities-to-media-entities-in-drupal-8</guid>
      <pubDate>Tue, 21 Aug 2018 00:00:00 +0200</pubDate>
      <description><![CDATA[<p>At Liip, we started several Drupal 8 projects a while ago, when the media module was not yet part of Drupal Core. Back then, we decided, to use normal file / image fields for media uploads in our content types.</p>
<p>A few months later, our clients prefered a media library using Drupal Media in Core and an Entity Browser to search and find their media assets.</p>
<p><strong>The question is:</strong> Is it possible to convert my old-fashioned image fields to new shiny media entities?<br />
<strong>The answer is:</strong> Yes, there is a way out of your misery!</p>
<p>We created a module called: &quot;Migrate File Entities to Media Entities&quot;.<br />
<a href="https://www.drupal.org/project/migrate_file_to_media">https://www.drupal.org/project/migrate_file_to_media</a></p>
<p>The module allows you, to migrate Drupal 8.0 file entities to Drupal 8.5 media entities, using the migrate module.</p>
<h2>The main features of the module?</h2>
<ul>
<li>It provides a drush command that automatically detects all file / image fields and creates the corresponding media reference field.</li>
<li>Before migrating the files to media entities, a binary hash of all images is calculated, and duplicate files are recognized. If the same file was uploaded multiple times on different nodes, only one media entity will be created.</li>
<li>Migration of translated file / image fields is supported. Having different images per language will create a translated media entity with the corresponding image.</li>
<li>Using migrate to file module allows drush processing, rollback and track changes.</li>
</ul>
<h2>How to migrate images to media entities</h2>
<h3>Prepare target media fields</h3>
<ul>
<li>Prepare the media fields based on the existing file fields using the following drush command:
<pre><code>drush migrate:file-media-fields &lt;entity_type&gt; &lt;bundle&gt; &lt;source_field_type&gt; &lt;target_media_bundle&gt;</code></pre>
<p><strong>Example</strong></p>
<pre><code>drush migrate:file-media-fields node article image image</code></pre>
<p>For all file fields the corresponding media entity reference field will be automatically created suffixed by {field_name}_media.</p></li>
</ul>
<h3>Prepare duplicate file / image detection</h3>
<p>In order to detect duplicate files / images, run the following drush command to calculate a binary hash for all files. The data will be saved to the table &quot;migrate_file_to_media_mapping&quot;. You need to run this drush command to be able to import media entities.</p>
<pre><code>drush migrate:duplicate-file-detection</code></pre>
<h3>Create a custom migration per content type based on the migrate_file_to_media_example module</h3>
<ul>
<li>Create a custom module</li>
<li>Create your own migration templates based on the examples in migrate_file_to_media_example.</li>
</ul>
<p>The module provided a custom migrate source plugin called &quot;media_entity_generator&quot;.</p>
<pre><code>id: article_images
label: Article Image to Media Migration
migration_group: media
source:
  plugin: media_entity_generator
  entity_type: node
  bundle: article
  langcode: 'en'

  # provide a list of all field names you want to migrate
  field_names:
  - field_image
  - field_image2

destination:
  plugin: entity:media</code></pre>
<p>You need to create a migration per entity bundle and provide a list of all field names, you want to migrate. The source plugin will query the database and find all files / images linked with these fields.</p>
<h3>Step-by-step instruction how to migrate your own files / images.</h3>
<h4>Step 1: Create media entities.</h4>
<p>File <code>migrate_file_to_media_example/config/install/migrate_plus.migration.article_images.yml</code></p>
<p>This is the starting point. This migration creates a unique media entity from all files / images referenced by fields in the configuration <code>field_names</code> of the source plugin.<br />
In the example, we have two image fields called: &quot;field_image&quot; and &quot;field_image2&quot;.</p>
<p><strong>Important:</strong><br />
The drush command to calculate the binary hash needs to be run before you can use the media_entity_generator source plugin.</p>
<h4>Using rokka.io on Step 1:</h4>
<p>File <code>migrate_file_to_media_example/config/install/migrate_plus.migration.article_images_rokka.yml</code></p>
<p>This is an example migration, how to move all images to the <a href="https://rokka.io/">rokka.io image content delivery network</a>. You need to install the<br />
<a href="https://www.drupal.org/project/rokka">drupal rokka module</a>.</p>
<h4>Step 2: Create media entity translations.</h4>
<p>File <code>migrate_file_to_media_example/config/install/migrate_plus.migration.article_images_de.yml</code></p>
<p>This migration adds a translation to existing media entities if a translated file / image field is found.</p>
<h4>Step 3: Link media entities with media reference field on target bundle.</h4>
<p>File <code>migrate_file_to_media_example/config/install/migrate_plus.migration.article_media.yml</code></p>
<p>This migration links the newly created media entities with entity reference field on the target bundle.</p>
<h4>Step 4: Check the migration status.</h4>
<pre><code>drush migrate:status</code></pre>
<h4>Step 5: Run the migration.</h4>
<pre><code>drush migrate:import &lt;migration_name&gt;</code></pre>]]></description>
                  <enclosure url="http://liip.rokka.io/www_card_2/0f60d8/istock-843016322.jpg" length="1155573" type="image/jpeg" />
          </item>
        <item>
      <title>Drupal 8 Migrate Multilingual Content using Migrate API</title>
      <link>https://www.liip.ch/fr/blog/drupal-8-multilingual-content</link>
      <guid>https://www.liip.ch/fr/blog/drupal-8-multilingual-content</guid>
      <pubDate>Thu, 05 Jan 2017 00:00:00 +0100</pubDate>
      <description><![CDATA[<p>As a follow-up to my <a href="https://blog.liip.ch/archive/2016/05/04/using-the-new-drupal-8-migration-api-module.html">previous blog post about the usage of Migrate API in Drupal 8</a>, I would like to give an example, how to import multilingual content and translations in Drupal 8.</p>
<h2>Prepare and enable translation for your content type</h2>
<p>Before you can start, you need to install the “Language” and “Content Translation” Module. Then head over to “admin/config/regional/content-language” and enable Entity Translation for the node type or the taxonomy you want to be able to translate.</p>
<p>As a starting point for setting up the migrate module, I recommend you my blog post mentioned above. To import data from a CSV file, you also need to install the <a href="https://www.drupal.org/project/migrate_source_csv">migrate_source_csv</a> module.</p>
<h2>Prerequisites for migrating multilingual entities</h2>
<p>Before you start, please check the requirements. You need at least Drupal 8.2 to import multilingual content. We need the destination option “translations”, which was added in a patch in Drupal 8.2. See the corresponding <a href="https://www.drupal.org/node/2225775">drupal.org issue</a> here.</p>
<h2>Example: Import multilingual taxonomy terms</h2>
<p>Let's do a simple example with taxonomy terms. First, create a vocabulary called “Event Types” (machine name: event_type).</p>
<p>Here is a simplified dataset:</p>
<table><thead><tr><th>Id</th>
<th>Name</th>
<th>Name_en</th>
</tr></thead><tbody><tr><td>1</td>
<td>Kurs</td>
<td>Course</td>
</tr><tr><td>2</td>
<td>Turnier</td>
<td>Tournament</td>
</tr></tbody></table>
<p>You may save this a csv file.</p>
<pre><code>Id;Name;Name_en
1;Kurs;Course
2;Turnier;Tournament</code></pre>
<h3>The recipe to import multilingual content</h3>
<p>As you can see in the example data,  it contains the base language (“German”) and also the translations (“English”) in the same file.</p>
<p><strong>But here comes a word of warning:</strong> </p>
<p>Don't try to import the term and its translation in one migration run. I am aware, that there are some workarounds with post import events, but these are hacks and you will run into troubles later.</p>
<p>The correct way of importing multilingual content, is to</p>
<ol>
<li>create a migration for the base language and import the terms / nodes. This will create the entities and its fields.</li>
<li>Then, with an additional dependent migration for each translated language, you can then add the translations for the fields you want.</li>
</ol>
<p>In short: You need a base migration and a migration for every language. Let's try this out.</p>
<h3>Taxonomy term base language config file</h3>
<p>In my example, the base language is “German”. Therefore, we first create a migration configuration file for the base language:</p>
<p>This is a basic example in migrating a taxonomy term in my base language ‘de'.</p>
<p>Put the file into <code>&lt;yourmodule&gt;/config/install/migrate.migration.event_type.yml</code> and import the configuration using the <a href="https://blog.liip.ch/archive/2016/05/04/using-the-new-drupal-8-migration-api-module.html">drush commands explained in my previous blog post about Migration API</a>.</p>
<pre><code class="language-yaml">id: event_type
label: Event Types
source:
  plugin: csv
  # Full path to the file. Is overriden in my plugin
  path: public://csv/data.csv
  # The number of rows at the beginning which are not data.
  header_row_count: 1
  # These are the field names from the source file representing the key
  # uniquely identifying each node - they will be stored in the migration
  # map table as columns sourceid1, sourceid2, and sourceid3.
  keys:
    - Id
ids:
  id:
    type: string
destination:
  plugin: entity:taxonomy_term
process:
  vid:
   plugin: default_value
   default_value: event_type
  name:
    source: Name
    language: 'de'
  langcode:
    plugin: default_value
    default_value: 'de'

#Absolutely necessary if you don't want an error
migration_dependencies: {}</code></pre>
<h3>Taxonomy term translation migration configuration file:</h3>
<p>This is the example file for the English translation of the name field of the term.</p>
<p>Put the file into <code>&lt;yourmodule&gt;/config/install/migrate.migration.event_type_en.yml</code> and import the configuration using the <a href="https://blog.liip.ch/archive/2016/05/04/using-the-new-drupal-8-migration-api-module.html">drush commands explained in my previous blog post about Migration API</a>.</p>
<pre><code class="language-yaml">id: event_type_en
label: Event Types english
source:
  plugin: csv
  # Full path to the file. Is overriden in my plugin
  path: public://csv/data.csv
  # The number of rows at the beginning which are not data.
  header_row_count: 1
  keys:
    - Id
ids:
  id:
    type: string
destination:
  plugin: entity:taxonomy_term
  translations: true
process:
  vid:
    plugin: default_value
    default_value: event_type
  tid:
    plugin: migration
    source: id
    migration: event_type
  name:
    source: Name_en
    language: 'en'
  langcode:
     plugin: default_value
     default_value: 'en'

#Absolutely necessary if you don't want an error
migration_dependencies:
  required:
    - event_type</code></pre>
<h2>Explanation and sum up of the learnings</h2>
<p>The key in the migrate configuration to import multilingual content are the following lines:</p>
<pre><code class="language-yaml">destination:
  plugin: entity:taxonomy_term
  translations: true</code></pre>
<p>These configuration lines instruct the migrate module, that a translation should be created.</p>
<pre><code class="language-yaml">tid:
  plugin: migration
  source: id
  migration: event_type</code></pre>
<p>This is the real secret. Using the <a href="https://www.drupal.org/docs/8/api/migrate-api/migrate-process/process-plugin-migration">process plugin migration</a>,  we maintain the relationship between the node and its translation.The wiring via the tid field make sure, that Migrate API will not create a new term with a new term id. Instead, the existing term will be loaded and the translation of the migrated field will be added. And thats exactly what we need!</p>
<p>Now go ahead and try to create a working example based on my explanation. Happy Drupal migrations!</p>]]></description>
          </item>
    
  </channel>
</rss>
