"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/node/src/EventSubscriber/NodeTranslationMigrateSubscriber.php" (26 Nov 2020, 3651 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "NodeTranslationMigrateSubscriber.php" see the Fossies "Dox" file reference documentation.

    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 }