Browse Source

raw-sources: support multi-bundle entities.

Frederic G. MARAND 7 years ago
parent
commit
546f6c1adc

+ 1 - 1
reinstall.drush.inc

@@ -26,7 +26,7 @@ function reinstall_drush_command() {
  *
  * @param string $entity_type
  *   The entity type.
- * @param array $bundles
+ * @param mixed $bundles
  *   An array of bundle names to export.
  */
 function drush_reinstall_export($entity_type = NULL, ...$bundles) {

+ 0 - 5
reinstall.services.yml

@@ -36,11 +36,6 @@ services:
     tags:
       - { name: 'event_subscriber' }
 
-  reinstall.migrate.map_save:
-    class: 'Drupal\reinstall\EventSubscriber\IdMapSave'
-    tags:
-      - { name: 'event_subscriber' }
-
   reinstall.migrate.post_row_save:
     class: 'Drupal\reinstall\EventSubscriber\IdPostRowSave'
     tags:

+ 0 - 56
src/EventSubscriber/IdMapSave.php

@@ -1,56 +0,0 @@
-<?php
-
-namespace Drupal\reinstall\EventSubscriber;
-
-use Drupal\Component\Utility\NestedArray;
-use Drupal\migrate\Event\MigrateEvents;
-use Drupal\migrate\Event\MigrateMapSaveEvent;
-use Drupal\migrate\Event\MigratePostRowSaveEvent;
-use Drupal\migrate\Row;
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
-
-/**
- * Class IdMapSave normalizes ids right before saving the map.
- *
- * This is needed because raw reinstall sources do not flatten source values,
- * and Sql::saveIdMap() assumes property fields are scalars of the described
- * type for the source, so we need to overwrite the source value for this last
- * step.
- *
- * Example: a node has an integer "nid" key, but is dumped like:
- * <nid>:
- *   nid:
- *     -
- *       value: 2
- *   ...
- */
-class IdMapSave implements EventSubscriberInterface {
-
-  public static function getSubscribedEvents() {
-    $events = [
-      MigrateEvents::MAP_SAVE => 'onMapSave',
-    ];
-
-    return $events;
-  }
-
-  public function onMapSave(MigrateMapSaveEvent $event) {
-    $rawIdValues = $event->getFields();
-    $filteredIdValues = array_filter($rawIdValues, function ($v) {
-      return true;
-    });
-    foreach ($filteredIdValues as $idName => $idValue) {
-      $current = $idValue;
-      while (is_array($current)) {
-        $current = current($current);
-      }
-      // Unavailable because source is frozen at this point.
-      // $row->setSourceProperty($idName, $current);
-      // So let's use brute force.
-      NestedArray::setValue($row->getSource(), explode(Row::PROPERTY_SEPARATOR, $idName), $current, TRUE);
-    }
-
-    return;
-  }
-
-}

+ 9 - 2
src/EventSubscriber/IdPostRowSave.php

@@ -25,6 +25,9 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  */
 class IdPostRowSave implements EventSubscriberInterface {
 
+  /**
+   * {@inheritdoc}
+   */
   public static function getSubscribedEvents() {
     $events = [
       MigrateEvents::POST_ROW_SAVE => 'postRowSave',
@@ -33,6 +36,12 @@ class IdPostRowSave implements EventSubscriberInterface {
     return $events;
   }
 
+  /**
+   * Callback for POST_ROW_SAVE.
+   *
+   * @param \Drupal\migrate\Event\MigratePostRowSaveEvent $event
+   *   The event instance.
+   */
   public function postRowSave(MigratePostRowSaveEvent $event) {
     $row = $event->getRow();
     $idValues = $row->getSourceIdValues();
@@ -46,8 +55,6 @@ class IdPostRowSave implements EventSubscriberInterface {
       // So let's use brute force.
       NestedArray::setValue($row->getSource(), explode(Row::PROPERTY_SEPARATOR, $idName), $current, TRUE);
     }
-
-    return;
   }
 
 }

+ 18 - 43
src/Plugin/migrate/process/FieldProcess.php

@@ -20,6 +20,9 @@ class FieldProcess extends ProcessPluginBase {
 
   protected $isMultiple;
 
+  /**
+   * {@inheritdoc}
+   */
   public function __construct(
     array $configuration,
     $plugin_id,
@@ -27,7 +30,8 @@ class FieldProcess extends ProcessPluginBase {
   ) {
     parent::__construct($configuration, $plugin_id, $plugin_definition);
     if (isset($this->configuration['field'])) {
-      $this->fieldValues = array_flip(FieldStorageConfig::load($this->configuration['field'])->getSetting('allowed_values'));
+      $this->fieldValues = array_flip(FieldStorageConfig::load($this->configuration['field'])
+        ->getSetting('allowed_values'));
     }
   }
 
@@ -36,11 +40,7 @@ class FieldProcess extends ProcessPluginBase {
    *
    * Optional: add prefix and suffix within the comment.
    *
-   * @param mixed $value
-   * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
-   * @param \Drupal\migrate\Row $row
-   * @param mixed $destination_property
-   * @return null|string
+   * {@inheritdoc}
    */
   public function transformComment(
     $value,
@@ -61,12 +61,7 @@ class FieldProcess extends ProcessPluginBase {
   /**
    * Remove everything except the target it to avoid extra ID issues.
    *
-   * @param mixed $value
-   * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
-   * @param \Drupal\migrate\Row $row
-   * @param mixed $destination_property
-   * @return null|string
-   *
+   * {@inheritdoc}
    */
   public function transformEntityReference(
     $value,
@@ -109,13 +104,9 @@ class FieldProcess extends ProcessPluginBase {
   }
 
   /**
-   * Remove empty items from a list, just like array_filter
+   * Remove empty items from a list, just like array_filter().
    *
-   * @param mixed $value
-   * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
-   * @param \Drupal\migrate\Row $row
-   * @param mixed $destination_property
-   * @return null|string
+   * {@inheritdoc}
    */
   public function transformFilter(
     $value,
@@ -130,11 +121,7 @@ class FieldProcess extends ProcessPluginBase {
   /**
    * Trim an array to its first element.
    *
-   * @param mixed $value
-   * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
-   * @param \Drupal\migrate\Row $row
-   * @param mixed $destination_property
-   * @return null|string
+   * {@inheritdoc}
    */
   public function transformFirst(
     $value,
@@ -153,11 +140,7 @@ class FieldProcess extends ProcessPluginBase {
   /**
    * Extract a sub-key from a D6-style image field. Useless on > D7 sources.
    *
-   * @param mixed $value
-   * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
-   * @param \Drupal\migrate\Row $row
-   * @param mixed $destination_property
-   * @return null|string
+   * {@inheritdoc}
    */
   public function transformImageData(
     $value,
@@ -175,8 +158,12 @@ class FieldProcess extends ProcessPluginBase {
    * Helper: build a search URL for a value.
    *
    * @param string $bundle
+   *   The node bundle.
    * @param string $query
+   *   The query to generate.
+   *
    * @return \Drupal\Core\Url
+   *   The generated search URL.
    *
    * @see \Drupal\reinstall\Plugin\migrate\process\FieldProcess::transformLink()
    */
@@ -194,11 +181,7 @@ class FieldProcess extends ProcessPluginBase {
   /**
    * Transform a link field. Beware limitations.
    *
-   * @param mixed $value
-   * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
-   * @param \Drupal\migrate\Row $row
-   * @param mixed $destination_property
-   * @return null|string
+   * {@inheritdoc}
    */
   public function transformLink(
     $value,
@@ -251,11 +234,7 @@ class FieldProcess extends ProcessPluginBase {
   /**
    * Like static_map, but based on the destination and not doing queries.
    *
-   * @param mixed $value
-   * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
-   * @param \Drupal\migrate\Row $row
-   * @param mixed $destination_property
-   * @return null|string
+   * {@inheritdoc}
    */
   public function transformLookupList(
     $value,
@@ -270,11 +249,7 @@ class FieldProcess extends ProcessPluginBase {
   /**
    * For multiple use only: push a configured value to the value array.
    *
-   * @param mixed $value
-   * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
-   * @param \Drupal\migrate\Row $row
-   * @param mixed $destination_property
-   * @return null|string
+   * {@inheritdoc}
    */
   public function transformPush(
     $value,

+ 9 - 0
src/Plugin/migrate/source/ReinstallSourceBase.php

@@ -174,6 +174,15 @@ class ReinstallSourceBase extends SourcePluginBase implements ContainerFactoryPl
     return $this->iterator;
   }
 
+  /**
+   * On raw sources, inject flat versions of the source ids.
+   *
+   * @param array $record
+   *   The array in which to inject the ids.
+   *
+   * @return array
+   *   The modified array.
+   */
   protected function injectIds(array $record) {
     $idKeys = array_keys($this->getIds());
     foreach ($idKeys as $idKey) {

+ 13 - 1
src/Plugin/migrate/source/SimpleSourceTrait.php

@@ -35,6 +35,13 @@ trait SimpleSourceTrait {
    */
   protected $sstEntityTypeManager;
 
+  /**
+   * Instance cache for the entity ID names, to avoid fetching them every time.
+   *
+   * @var array|null
+   */
+  protected $sstIds = NULL;
+
   /**
    * {@inheritdoc}
    */
@@ -133,6 +140,10 @@ trait SimpleSourceTrait {
    * {@inheritdoc}
    */
   public function getIds() {
+    if (is_array($this->sstIds)) {
+      return $this->sstIds;
+    }
+
     $typeName = $this->getEntityType();
     $typeDefinition = $this->getEntityTypeManager()->getDefinition($typeName);
 
@@ -158,7 +169,8 @@ trait SimpleSourceTrait {
       }
     };
 
-    return $ids;
+    $this->sstIds = $ids;
+    return $this->sstIds;
   }
 
 }