Browse Source

Extracted fields() to SimpleSourceTrait.

Frederic G. MARAND 7 years ago
parent
commit
65f7c643aa

+ 3 - 68
src/Plugin/migrate/source/SimpleSource.php

@@ -17,37 +17,14 @@ use Symfony\Component\Yaml\Yaml;
  * Class SimpleSource provides the basic mechanisms to load a YML entity dump.
  */
 abstract class SimpleSource extends SourcePluginBase implements ContainerFactoryPluginInterface {
+  use SimpleSourceTrait;
 
   /**
-   * The source records.
+   * The source records. MUST be initialized in concrete classes __construct().
    *
    * @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}
@@ -61,9 +38,7 @@ abstract class SimpleSource extends SourcePluginBase implements ContainerFactory
   ) {
     $importPath = $container->getParameter('reinstall.path');
     $configuration['importPath'] = $importPath;
-    $ebi = $container->get('entity_type.bundle.info');
-    $efm = $container->get('entity_field.manager');
-    return new static($configuration, $pluginId, $pluginDefinition, $migration, $ebi, $efm);
+    return new static($configuration, $pluginId, $pluginDefinition, $migration);
   }
 
   /**
@@ -73,46 +48,6 @@ 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.
    *

+ 82 - 0
src/Plugin/migrate/source/SimpleSourceTrait.php

@@ -0,0 +1,82 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: fgm
+ * Date: 11/04/2017
+ * Time: 08:35
+ */
+
+namespace Drupal\reinstall\Plugin\migrate\source;
+
+
+trait SimpleSourceTrait {
+
+  protected $sstEntityTypeBundleInfo;
+
+  protected $sstEntityFieldManager;
+
+  protected $sstEntityType;
+
+  protected function getEntityTypeBundleInfo() {
+    if (!isset($this->sstEntityTypeBundleInfo)) {
+      $this->sstEntityTypeBundleInfo = \Drupal::service('entity_type.bundle.info');
+    }
+
+    return $this->sstEntityTypeBundleInfo;
+  }
+
+  protected function getEntityFieldManager() {
+    if (!isset($this->sstEntityFieldManager)) {
+      $this->sstEntityFieldManager = \Drupal::service('entity_field.manager');
+    }
+
+    return $this->sstEntityFieldManager;
+  }
+
+  protected function getEntityType() {
+    assert(isset($this->sstEntityType));
+    return $this->sstEntityType;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function fields() {
+    $bundles = $this->getEntityTypeBundleInfo()
+      ->getBundleInfo($typeName = $this->getEntityType());
+    $rows = [];
+
+    foreach ($bundles as $bundleName => $bundle) {
+      $fieldDefinitions = $this->getEntityFieldManager()
+        ->getFieldDefinitions($typeName, $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;
+  }
+
+}