annotate core/modules/node/src/EventSubscriber/NodeTranslationMigrateSubscriber.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\node\EventSubscriber;
Chris@0 4
Chris@0 5 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
Chris@0 6 use Drupal\Core\State\StateInterface;
Chris@0 7 use Drupal\migrate\Event\EventBase;
Chris@0 8 use Drupal\migrate\Event\MigrateEvents;
Chris@0 9 use Drupal\migrate\Event\MigrateImportEvent;
Chris@0 10 use Drupal\migrate\Event\MigratePostRowSaveEvent;
Chris@0 11 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Creates a key value collection for migrated node translation redirections.
Chris@0 15 *
Chris@0 16 * If we are migrating node translations from Drupal 6 or 7, these nodes will be
Chris@0 17 * combined with their source node. Since there still might be references to the
Chris@0 18 * URLs of these now consolidated nodes, this service saves the mapping between
Chris@0 19 * the old nids to the new ones to be able to redirect them to the right node in
Chris@0 20 * the right language.
Chris@0 21 *
Chris@0 22 * The mapping is stored in the "node_translation_redirect" key/value collection
Chris@0 23 * and the redirection is made by the NodeTranslationExceptionSubscriber class.
Chris@0 24 *
Chris@0 25 * @see \Drupal\node\NodeServiceProvider
Chris@0 26 * @see \Drupal\node\EventSubscriber\NodeTranslationExceptionSubscriber
Chris@0 27 */
Chris@0 28 class NodeTranslationMigrateSubscriber implements EventSubscriberInterface {
Chris@0 29
Chris@0 30 /**
Chris@0 31 * The key value factory.
Chris@0 32 *
Chris@0 33 * @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface
Chris@0 34 */
Chris@0 35 protected $keyValue;
Chris@0 36
Chris@0 37 /**
Chris@0 38 * The state service.
Chris@0 39 *
Chris@0 40 * @var \Drupal\Core\State\StateInterface
Chris@0 41 */
Chris@0 42 protected $state;
Chris@0 43
Chris@0 44 /**
Chris@0 45 * Constructs the NodeTranslationMigrateSubscriber.
Chris@0 46 *
Chris@0 47 * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value
Chris@0 48 * The key value factory.
Chris@0 49 * @param \Drupal\Core\State\StateInterface $state
Chris@0 50 * The state service.
Chris@0 51 */
Chris@0 52 public function __construct(KeyValueFactoryInterface $key_value, StateInterface $state) {
Chris@0 53 $this->keyValue = $key_value;
Chris@0 54 $this->state = $state;
Chris@0 55 }
Chris@0 56
Chris@0 57 /**
Chris@0 58 * Helper method to check if we are migrating translated nodes.
Chris@0 59 *
Chris@0 60 * @param \Drupal\migrate\Event\EventBase $event
Chris@0 61 * The migrate event.
Chris@0 62 *
Chris@0 63 * @return bool
Chris@0 64 * True if we are migrating translated nodes, false otherwise.
Chris@0 65 */
Chris@0 66 protected function isNodeTranslationsMigration(EventBase $event) {
Chris@0 67 $migration = $event->getMigration();
Chris@0 68 $source_configuration = $migration->getSourceConfiguration();
Chris@0 69 $destination_configuration = $migration->getDestinationConfiguration();
Chris@0 70 return !empty($source_configuration['translations']) && $destination_configuration['plugin'] === 'entity:node';
Chris@0 71 }
Chris@0 72
Chris@0 73 /**
Chris@0 74 * Maps the old nid to the new one in the key value collection.
Chris@0 75 *
Chris@0 76 * @param \Drupal\migrate\Event\MigratePostRowSaveEvent $event
Chris@0 77 * The migrate post row save event.
Chris@0 78 */
Chris@0 79 public function onPostRowSave(MigratePostRowSaveEvent $event) {
Chris@0 80 if ($this->isNodeTranslationsMigration($event)) {
Chris@0 81 $row = $event->getRow();
Chris@0 82 $source = $row->getSource();
Chris@0 83 $destination = $row->getDestination();
Chris@0 84 $collection = $this->keyValue->get('node_translation_redirect');
Chris@0 85 $collection->set($source['nid'], [$destination['nid'], $destination['langcode']]);
Chris@0 86 }
Chris@0 87 }
Chris@0 88
Chris@0 89 /**
Chris@0 90 * Set the node_translation_redirect state to enable the redirections.
Chris@0 91 *
Chris@0 92 * @param \Drupal\migrate\Event\MigrateImportEvent $event
Chris@0 93 * The migrate import event.
Chris@0 94 */
Chris@0 95 public function onPostImport(MigrateImportEvent $event) {
Chris@0 96 if ($this->isNodeTranslationsMigration($event)) {
Chris@0 97 $this->state->set('node_translation_redirect', TRUE);
Chris@0 98 }
Chris@0 99 }
Chris@0 100
Chris@0 101 /**
Chris@0 102 * {@inheritdoc}
Chris@0 103 */
Chris@0 104 public static function getSubscribedEvents() {
Chris@0 105 $events = [];
Chris@0 106
Chris@0 107 $events[MigrateEvents::POST_ROW_SAVE] = ['onPostRowSave'];
Chris@0 108 $events[MigrateEvents::POST_IMPORT] = ['onPostImport'];
Chris@0 109
Chris@0 110 return $events;
Chris@0 111 }
Chris@0 112
Chris@0 113 }