Browse Source

Added node source.

Frederic G. MARAND 7 years ago
parent
commit
202e2eef12

+ 49 - 0
src/Plugin/migrate/source/ReinstallNodeSource.php

@@ -0,0 +1,49 @@
+<?php
+
+namespace Drupal\reinstall\Plugin\migrate\source;
+
+use Drupal\Core\Entity\EntityFieldManagerInterface;
+use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
+use Drupal\migrate\Plugin\MigrationInterface;
+
+/**
+ * Source plugin for terms from a YAML file.
+ *
+ * @MigrateSource(
+ *   id = "reinstall_nodes"
+ * )
+ */
+class ReinstallNodeSource extends SimpleSource {
+  const ENTITY_TYPE = 'node';
+
+  /**
+   * Constructor.
+   */
+  public function __construct(
+    array $configuration,
+    string $pluginId,
+    array $pluginDefinition,
+    MigrationInterface $migration,
+    EntityTypeBundleInfoInterface $ebi,
+    EntityFieldManagerInterface $efm
+  ) {
+    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration, $ebi, $efm);
+
+    $rawRecords = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
+    $this->records = $rawRecords;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getIds() {
+    $ids = [
+      'nid' => [
+        'type' => 'integer',
+      ],
+    ];
+
+    return $ids;
+  }
+
+}

+ 8 - 41
src/Plugin/migrate/source/ReinstallTermSource.php

@@ -2,8 +2,9 @@
 
 namespace Drupal\reinstall\Plugin\migrate\source;
 
+use Drupal\Core\Entity\EntityFieldManagerInterface;
+use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
 use Drupal\migrate\Plugin\MigrationInterface;
-use Drupal\migrate\Row;
 
 /**
  * Source plugin for terms from a YAML file.
@@ -13,6 +14,7 @@ use Drupal\migrate\Row;
  * )
  */
 class ReinstallTermSource extends SimpleSource {
+  const ENTITY_TYPE = 'taxonomy_term';
 
   /**
    * Constructor.
@@ -21,51 +23,16 @@ class ReinstallTermSource extends SimpleSource {
     array $configuration,
     string $pluginId,
     array $pluginDefinition,
-    MigrationInterface $migration
+    MigrationInterface $migration,
+    EntityTypeBundleInfoInterface $ebi,
+    EntityFieldManagerInterface $efm
   ) {
-    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration);
+    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration, $ebi, $efm);
+
     $rawRecords = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
     $this->records = $rawRecords;
   }
 
-  /**
-   * Flatten the field hierarchy. Not correct for all cases.
-   *
-   * @param array $record
-   *   The raw source values.
-   *
-   * @return array
-   *   The flattened values.
-   *
-   * @see \Drupal\reinstall\Plugin\migrate\process\TermParent
-   */
-  protected function flattenRecord(array $record) {
-    $row = new Row($record);
-    $this->flattenRow($row);
-    return $row->getSource();
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public function fields() {
-    $ret = [
-      'changed'           => 'Modification timestamp',
-      'default_langcode'  => 'ISO 639 default language code',
-      'description'       => 'The entity description',
-      'langcode'          => 'ISO 639 language code',
-      'name'              => 'The term name (PK)',
-      'parent'            => 'Parent term ID',
-      'path'              => 'Optional path alias',
-      'tid'               => 'Taxonomy term ID',
-      'uuid'              => 'UUID',
-      'vid'               => 'Vocabulary ID',
-      'weight'            => 'Poids',
-    ];
-
-    return $ret;
-  }
-
   /**
    * {@inheritdoc}
    */

+ 11 - 47
src/Plugin/migrate/source/ReinstallUserSource.php

@@ -2,8 +2,9 @@
 
 namespace Drupal\reinstall\Plugin\migrate\source;
 
+use Drupal\Core\Entity\EntityFieldManagerInterface;
+use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
 use Drupal\migrate\Plugin\MigrationInterface;
-use Drupal\migrate\Row;
 
 /**
  * Source plugin for terms from a YAML file.
@@ -13,6 +14,7 @@ use Drupal\migrate\Row;
  * )
  */
 class ReinstallUserSource extends SimpleSource {
+  const ENTITY_TYPE = 'user';
 
   /**
    * Constructor.
@@ -21,29 +23,15 @@ class ReinstallUserSource extends SimpleSource {
     array $configuration,
     string $pluginId,
     array $pluginDefinition,
-    MigrationInterface $migration
+    MigrationInterface $migration,
+    EntityTypeBundleInfoInterface $ebi,
+    EntityFieldManagerInterface $efm
   ) {
-    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration);
-    $rawRecords = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
-    $records = array_filter($rawRecords, [$this, 'filter01']);
-    $this->records = $records;
-  }
+    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration, $ebi, $efm);
 
-  /**
-   * Flatten the field hierarchy. Not correct for all cases.
-   *
-   * @param array $record
-   *   The raw source values.
-   *
-   * @return array
-   *   The flattened values.
-   *
-   * @see \Drupal\reinstall\Plugin\migrate\process\TermParent
-   */
-  protected function flattenRecord(array $record) {
-    $row = new Row($record);
-    $this->flattenRow($row);
-    return $row->getSource();
+    $rawRecords = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
+    $rawRecords = array_filter($rawRecords, [$this, 'filter01']);
+    $this->records = $rawRecords;
   }
 
   /**
@@ -59,31 +47,6 @@ class ReinstallUserSource extends SimpleSource {
     return ($record['uid'] ?? 0) > 1;
   }
 
-  /**
-   * {@inheritdoc}
-   */
-  public function fields() {
-    $ret = [
-      'uid'               => 'User ID',
-      'uuid'              => 'UUID',
-      'langcode'          => 'ISO 639 language code',
-      'preferred_langcode' => 'ISO 639 preferred language code',
-      'preferred_admin_langcode' => 'ISO 639 preferred administrative language code',
-      'name'              => 'The user name (PK)',
-      'mail'              => 'The current user e-mail address',
-      'timezone'          => 'The user timezone',
-      'status'            => 'The user status',
-      'access'            => 'Latest access timestamp',
-      'login'             => 'Latest login timestamp',
-      'init'              => 'Initial user e-mail address',
-      'created'           => 'User creation timestamp',
-      'changed'           => 'Modification timestamp',
-      'default_langcode'  => 'ISO 639 default language code',
-    ];
-
-    return $ret;
-  }
-
   /**
    * {@inheritdoc}
    */
@@ -93,6 +56,7 @@ class ReinstallUserSource extends SimpleSource {
         'type' => 'integer',
       ],
     ];
+
     return $ids;
   }
 

+ 86 - 1
src/Plugin/migrate/source/SimpleSource.php

@@ -2,6 +2,8 @@
 
 namespace Drupal\reinstall\Plugin\migrate\source;
 
+use Drupal\Core\Entity\EntityFieldManagerInterface;
+use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
 use Drupal\migrate\MigrateException;
 use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
@@ -22,6 +24,30 @@ abstract class SimpleSource extends SourcePluginBase implements ContainerFactory
    * @var array
    */
   protected $records;
+  /**
+   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
+   */
+  protected $entityTypeBundleInfo;
+  /**
+   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
+   */
+  protected $entityFieldManager;
+
+  /**
+   * Constructor.
+   */
+  public function __construct(
+    array $configuration,
+    string $pluginId,
+    array $pluginDefinition,
+    MigrationInterface $migration,
+    EntityTypeBundleInfoInterface $ebi,
+    EntityFieldManagerInterface $efm
+  ) {
+    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration);
+    $this->entityTypeBundleInfo = $ebi;
+    $this->entityFieldManager = $efm;
+  }
 
   /**
    * {@inheritdoc}
@@ -35,7 +61,9 @@ abstract class SimpleSource extends SourcePluginBase implements ContainerFactory
   ) {
     $importPath = $container->getParameter('reinstall.path');
     $configuration['importPath'] = $importPath;
-    return new static($configuration, $pluginId, $pluginDefinition, $migration);
+    $ebi = $container->get('entity_type.bundle.info');
+    $efm = $container->get('entity_field.manager');
+    return new static($configuration, $pluginId, $pluginDefinition, $migration, $ebi, $efm);
   }
 
   /**
@@ -45,6 +73,63 @@ abstract class SimpleSource extends SourcePluginBase implements ContainerFactory
     return count($this->records);
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function fields() {
+    $bundles = $this->entityTypeBundleInfo->getBundleInfo(static::ENTITY_TYPE);
+    $rows = [];
+
+    foreach ($bundles as $bundleName => $bundle) {
+      $fieldDefinitions = $this->entityFieldManager->getFieldDefinitions(static::ENTITY_TYPE,
+        $bundleName);
+      foreach ($fieldDefinitions as $fieldName => $fieldDefinition) {
+        $rows[$fieldName][$bundleName] = $fieldDefinition->getLabel();
+      }
+    }
+
+    $fields = [];
+    $singleBundle = count($bundles) === 1;
+    foreach ($rows as $fieldName => $labels) {
+      if ($singleBundle) {
+        $fields[$fieldName] = reset($labels);
+        continue;
+      }
+
+      if (count(array_unique($labels)) === 1) {
+        $fields[$fieldName] = reset($labels);
+        continue;
+      }
+
+      $ret = [];
+      ksort($labels);
+      foreach ($labels as $ct => $label) {
+        $ret[] = $this->t('@ct: @label', ['@ct' => $ct, '@label' => $label]);
+      }
+      $fields[$fieldName] = implode(', ', $ret);
+    }
+
+    ksort($fields);
+    return $fields;
+  }
+
+  /**
+   * Flatten the field hierarchy. Not correct for all cases.
+   *
+   * @param array $record
+   *   The raw source values.
+   *
+   * @return array
+   *   The flattened values.
+   *
+   * @see \Drupal\reinstall\Plugin\migrate\process\TermParent
+   */
+  protected function flattenRecord(array $record) {
+    $row = new Row($record);
+    $this->flattenRow($row);
+    return $row->getSource();
+  }
+
   /**
    * Flatten a typical Drupal 8 field array to a 1-level array.
    */