comparison core/modules/node/src/EventSubscriber/NodeTranslationMigrateSubscriber.php @ 0:c75dbcec494b

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