|
@@ -0,0 +1,56 @@
|
|
|
+<?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;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|