diff --git a/core/modules/node/src/EventSubscriber/NodeTranslationExceptionSubscriber.php b/core/modules/node/src/EventSubscriber/NodeTranslationExceptionSubscriber.php index d00aed8fe6..72ec830858 100644 --- a/core/modules/node/src/EventSubscriber/NodeTranslationExceptionSubscriber.php +++ b/core/modules/node/src/EventSubscriber/NodeTranslationExceptionSubscriber.php @@ -39,7 +39,7 @@ class NodeTranslationExceptionSubscriber implements EventSubscriberInterface { protected $urlGenerator; /** - * Construct an ExceptionSubscriber. + * Construct an NodeTranslationExceptionSubscriber. * * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $keyValue * The key value factory. @@ -62,11 +62,15 @@ public function __construct(KeyValueFactoryInterface $keyValue, LanguageManagerI */ public function onException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); + if (!($exception instanceof NotFoundHttpException)) { + return; + } + $previousException = $exception->getPrevious(); - if ($exception instanceof NotFoundHttpException && $previousException instanceof ParamNotConvertedException) { + if ($previousException instanceof ParamNotConvertedException) { $routeName = $previousException->getRouteName(); $parameters = $previousException->getParameters(); - if ($routeName == 'entity.node.canonical' && isset($parameters['node'])) { + if ($routeName === 'entity.node.canonical' && isset($parameters['node'])) { $oldNid = $parameters['node']; $collection = $this->keyValue->get('node_translation_redirect'); if ($oldNid && $collection->has($oldNid)) { diff --git a/core/modules/node/src/EventSubscriber/NodeTranslationMigrateSubscriber.php b/core/modules/node/src/EventSubscriber/NodeTranslationMigrateSubscriber.php index 1b4ce65b97..e3ffd101ee 100644 --- a/core/modules/node/src/EventSubscriber/NodeTranslationMigrateSubscriber.php +++ b/core/modules/node/src/EventSubscriber/NodeTranslationMigrateSubscriber.php @@ -3,9 +3,7 @@ namespace Drupal\node\EventSubscriber; use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; -use Drupal\migrate\Event\EventBase; use Drupal\migrate\Event\MigrateEvents; -use Drupal\migrate\Event\MigrateImportEvent; use Drupal\migrate\Event\MigratePostRowSaveEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -22,7 +20,7 @@ class NodeTranslationMigrateSubscriber implements EventSubscriberInterface { protected $keyValue; /** - * Construct a NodeTranslationSubscriber. + * Construct a NodeTranslationMigrateSubscriber. * * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $keyValue * The key value factory. @@ -32,29 +30,16 @@ public function __construct(KeyValueFactoryInterface $keyValue) { } /** - * Helper method to check if we are migrating translated nodes. - * - * @param \Drupal\migrate\Event\EventBase $event - * The migrate event. - * - * @return bool - * True if we are migrating translated nodes, false otherwise. - */ - protected function isNodeTranslationsMigration(EventBase $event) { - $migration = $event->getMigration(); - $source_configuration = $migration->getSourceConfiguration(); - $destination_configuration = $migration->getDestinationConfiguration(); - return !empty($source_configuration['translations']) && $destination_configuration['plugin'] === 'entity:node'; - } - - /** * Maps the old nid to the new one in the key value collection. * * @param \Drupal\migrate\Event\MigratePostRowSaveEvent $event * The migrate post row save event. */ public function onPostRowSave(MigratePostRowSaveEvent $event) { - if ($this->isNodeTranslationsMigration($event)) { + $migration = $event->getMigration(); + $source_configuration = $migration->getSourceConfiguration(); + $destination_configuration = $migration->getDestinationConfiguration(); + if (!empty($source_configuration['translations']) && $destination_configuration['plugin'] === 'entity:node') { $row = $event->getRow(); $source = $row->getSource(); $destination = $row->getDestination(); @@ -64,26 +49,12 @@ public function onPostRowSave(MigratePostRowSaveEvent $event) { } /** - * Enables the node_translation_redirect module to handle the redirections. - * - * @param \Drupal\migrate\Event\MigrateImportEvent $event - * The migrate import event. - */ - public function onPostImport(MigrateImportEvent $event) { - if ($this->isNodeTranslationsMigration($event)) { - // Check if service.yml exists, is writable & is included. - // Add the container parameter. - } - } - - /** * {@inheritdoc} */ public static function getSubscribedEvents() { $events = []; $events[MigrateEvents::POST_ROW_SAVE] = ['onPostRowSave']; - $events[MigrateEvents::POST_IMPORT] = ['onPostImport']; return $events; } diff --git a/core/modules/node/src/NodeServiceProvider.php b/core/modules/node/src/NodeServiceProvider.php index 56c03ce7fb..92a86859c6 100644 --- a/core/modules/node/src/NodeServiceProvider.php +++ b/core/modules/node/src/NodeServiceProvider.php @@ -22,11 +22,12 @@ public function register(ContainerBuilder $container) { $container->register('node.node_translation_migrate', 'Drupal\node\EventSubscriber\NodeTranslationMigrateSubscriber') ->addTag('event_subscriber') ->addArgument(new Reference('keyvalue')); + $container->setParameter('node.node_translation_redirect', TRUE); } // Register the node.node_translation_exception service to the container if - // the node.node_translation_redirect container parameter is set - if (TRUE) { + // the node.node_translation_redirect container parameter is set. + if ($container->hasParameter('node.node_translation_redirect') && $container->getParameter('node.node_translation_redirect')) { $container->register('node.node_translation_exception', 'Drupal\node\EventSubscriber\NodeTranslationExceptionSubscriber') ->addTag('event_subscriber') ->addArgument(new Reference('keyvalue'))