123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace Drupal\reinstall\EventSubscriber;
- use Drupal\Component\Utility\NestedArray;
- use Drupal\migrate\Event\MigrateEvents;
- use Drupal\migrate\Event\MigratePostRowSaveEvent;
- use Drupal\migrate\Row;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- /**
- * Class IdPostRowSave normalizes ids after the destination saved the row.
- *
- * 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 IdPostRowSave implements EventSubscriberInterface {
- /**
- * {@inheritdoc}
- */
- public static function getSubscribedEvents() {
- $events = [
- MigrateEvents::POST_ROW_SAVE => 'postRowSave',
- ];
- 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();
- foreach ($idValues 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);
- }
- }
- }
|