back

Knowledge Centre

Wie man Absätze von Drupal 7 zu Drupal 8 migriert

| 22.12.2017

Wie man Absätze von Drupal 7 zu Drupal 8 migriert

Vor einigen Tagen stießen wir auf eine spannende Herausforderung: die Migration von Absätzen von einer Drupal 7 Plattform zu Drupal 8. Während wir nach der richtigen Lösung suchten, durchkämmten wir das Web, fanden aber nichts, was perfekt zu unseren Bedürfnissen passte. Als Ergebnis haben unsere Backend-Entwickler eine maßgeschneiderte Lösung erstellt. Ein besonderer Dank geht an mtech-llc.com und Ada Hernández, deren Arbeit an der Feldsammlungsmigration diese Lösung inspiriert hat.

Während Adas ursprünglicher Artikel die Migration von Feldsammlungen beschreibt, haben wir den Ansatz für die Migration von Absätzen angepasst. Ihre Erkenntnisse erwiesen sich als unschätzbar für unseren Prozess.

Bevor Sie fortfahren, stellen Sie sicher, dass Sie haben:

1. Eine funktionierende Drupal 8 Installation

2. Die folgenden Module installiert:

  • migrate_drupal
  • migrate_plus
  • migrate_tools
  • migrate_drupal_ui

Alternativ können Sie diese als Abhängigkeiten zu Ihrem benutzerdefinierten Modul hinzufügen.

Nach der Installation der erforderlichen Module, erstellen Sie einen Absatz mit dem Bündel "Kontakt", der diese Felder enthält:

  • Text plain (Maschinenname: field_name)
  • Email (Maschinenname: field_email)
  • Nummer (Integer) (Maschinenname: field_phone)

Dann erstellen Sie einen Inhaltstyp namens Organisation (Maschinenname: organisation) mit:

  • Standard Body-Feld
  • Feld Absatztyp (Maschinenname: field_contact)

Dieses Verfahren ist komplex und erfordert sorgfältige Aufmerksamkeit für Details. Wenn während eines Schrittes Fehler auftreten, müssen Sie möglicherweise von vorne beginnen. Bitte lesen Sie jeden Schritt gründlich durch, bevor Sie fortfahren.

1. Datenbankverbindung in settings.php angeben

Fügen Sie die zweite Verbindung zu Drupal 7 in Ihrer settings.php Datei hinzu. In diesem Beispiel lautet der Name der Datenbank "drupal_7_56":

$databases['migrate']['default'] = array(
   'database' => 'drupal_7_56',
   'username' => 'root',
   'password' => '',
   'prefix' => '',
   'host' => '127.0.0.1',
   'port' => '33067',
   'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
   'driver' => 'mysql',
);

2. Ein benutzerdefiniertes Modul erstellen

Erstellen Sie ein benutzerdefiniertes Modul mit dem Maschinennamen "custom".

3. YML Vorlagen erstellen

Platzieren Sie die folgenden YML Vorlagen im Ordner config/install:

Zuerst erstellen Sie die Absatz-Migrationsvorlage (Dateiname: migrate_plus.migration.d7_paragraph_contact.yml):

langcode: en
status: true
dependencies: {}
id: d7_paragraph_contact
class: null
field_plugin_method: null
cck_plugin_method: null
migration_tags:
 - 'Drupal 7'
migration_group: migrate_drupal_7
label: Contacts
source:
 plugin: d7_paragraph_item
 key: migrate
 field_name: field_contact
process:
 field_name:
   plugin: iterator
   source: field_name
   process:
     value: value
   revision_id: revision_id
 field_email:
   plugin: iterator
   source: field_email
   process:
     value: email
   revision_id: revision_id
 field_phone:
   plugin: iterator
   source: field_phone
   process:
     value: value
   revision_id: revision_id
destination:
 plugin: 'entity_reference_revisions:paragraph'
 default_bundle: contact
migration_dependencies:
 required: {}
 optional: {}

Dann erstellen Sie die Node-Migrationsvorlage (Dateiname: migrate_plus.migration.d7_node_organization.yml):

langcode: en
status: true
dependencies: {}
id: d7_node_organization
class: null
field_plugin_method: null
cck_plugin_method: null
migration_tags:
 - 'Drupal 7'
 - Content
migration_group: migrate_drupal_7
label: 'Nodes (Organization)'
source:
 plugin: d7_node
 node_type: organization
process:
 nid: nid
 vid: vid
 langcode:
   plugin: default_value
   source: language
   default_value: und
 title: title
 uid: node_uid
 status: status
 created: created
 changed: changed
 promote: promote
 sticky: sticky
 revision_uid: revision_uid
 revision_log: log
 revision_timestamp: timestamp
 body:
   plugin: iterator
   source: body
   process:
     value: value
     format:
       - plugin: static_map
         bypass: true
         source: format
         map:
           - null
       - plugin: skip_on_empty
         method: process
       - plugin: migration
         migration:
           - d6_filter_format
           - d7_filter_format
         source: format
 field_contact:
   - plugin: skip_on_empty
     method: process
     source: field_contact
   - plugin: migration_lookup
     migration: d7_paragraph_contact
     no_stub: true
   - plugin: iterator
     process:
       target_id: '0'
       target_revision_id: '1'
destination:
 plugin: 'entity:node'
 default_bundle: organization
migration_dependencies:
 required: {}
 optional: {}

4. Erstellen Sie die Source Plugin Klasse

Erstellen Sie eine Datei namens ContactParagraph.php im Verzeichnis /src/Plugin/migrate/source:

<?php
namespace Drupal\mparagraf\Plugin\migrate\source;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
/**
* D7_paragraph_item source.
*
* @MigrateSource(
*   id = "d7_paragraph_item"
* )
*/
class ContactParagraph extends FieldableEntity {
 /**
  * {@inheritdoc}
  */
 public function query() {
   // Select node in its last revision.
   $query = $this->select('paragraphs_item', 'fci')
     ->fields('fci', [
       'item_id',
       'field_name',
       'revision_id',
     ]);
   if (isset($this->configuration['field_name'])) {
     $query->innerJoin('field_data_' . $this->configuration['field_name'], 'fd', 'fd.' . $this->configuration['field_name'] . '_value = fci.item_id');
     $query->fields('fd', [
       'entity_type',
       'bundle',
       'entity_id',
       $this->configuration['field_name'] . '_revision_id',
     ]);
     $query->condition('fci.field_name', $this->configuration['field_name']);
   }
   return $query;
 }
 /**
  * {@inheritdoc}
  */
 public function prepareRow(Row $row) {
   // If field specified, get field revision ID so there aren't issues mapping.
   if (isset($this->configuration['field_name'])) {
     $row->setSourceProperty('revision_id', $row->getSourceProperty($this->configuration['field_name'] . '_revision_id'));
   }
   // Get field API field values.
   foreach (array_keys($this->getFields('paragraphs_item', 'contact')) as $field) {
     $item_id = $row->getSourceProperty('item_id');
     $revision_id = $row->getSourceProperty('revision_id');
     $row->setSourceProperty($field, $this->getFieldValues('paragraphs_item', $field, $item_id, $revision_id));
   }
   return parent::prepareRow($row);
 }
 /**
  * {@inheritdoc}
  */
 public function fields() {
   $fields = [
     'item_id' => $this->t('Item ID'),
     'revision_id' => $this->t('Revision ID'),
     'field_name' => $this->t('Name of field'),
   ];
   return $fields;
 }
 /**
  * {@inheritdoc}
  */
 public function getIds() {
   $ids['item_id']['type'] = 'integer';
   $ids['item_id']['alias'] = 'fci';
   return $ids;
 }
}

5. Implementieren Sie den Migrations-Hook

Fügen Sie den folgenden Code zur .module Datei Ihres Moduls hinzu:

<?php
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\MigrateSourceInterface;
use Drupal\migrate\Row;
/**
* Implements hook_migrate_MIGRATION_ID_prepare_row().
*/
function custom_migrate_d7_node_organization_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
 $values = $row->getSourceProperty('field_contact');
 $value_new = [];
 if ($values) {
   foreach ($values as $value) {
     $value_new[] = ['item_id' => $value['value']];
   }
   $row->setSourceProperty('field_contact', $value_new);
 }
}

6. Letzte Schritte

Sobald Sie alle oben genannten Schritte erfolgreich abgeschlossen haben, können Sie Ihre Migration durchführen und zusehen, wie Ihre Absätze von Drupal 7 zu Drupal 8 übertragen werden. Nehmen Sie sich einen Moment Zeit, um die Ergebnisse zu überprüfen, bevor Sie mit weiteren absatzbezogenen Aufgaben fortfahren.

Denken Sie daran, gründlich in einer Entwicklungsumgebung zu testen, bevor Sie diese Migration auf einer Produktionsseite versuchen.

migrieren
  • Wissen
    Wissenszentrum
    Im Gehirn einer KI
  • Wissen
    Wissenszentrum
    Feinabstimmung von LLaMA zur Nachbildung von Eminescus literarischem Stil
  • Wissen
    Wissenszentrum
    Ein neues Zeitalter beginnt: Drupal CMS 1.0 startet