Browse Source

Moved user import filtering to Reinstall event.

Frederic G. MARAND 7 years ago
parent
commit
20e484d278

+ 10 - 7
src/EventSubscriber/UserPreImport.php

@@ -2,22 +2,25 @@
 
 namespace Drupal\reinstall\EventSubscriber;
 
-use Drupal\migrate\Event\MigrateEvents;
-use Drupal\migrate\Event\MigrateImportEvent;
+use Drupal\reinstall\ReinstallEvents;
+use Drupal\reinstall\SourceEvent;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 
 class UserPreImport implements EventSubscriberInterface {
 
   public static function getSubscribedEvents() {
     return [
-      MigrateEvents::PRE_IMPORT => 'onPreImport',
+      ReinstallEvents::POST_SOURCE_PARSE => 'onPreImport',
     ];
   }
 
-  public function onPreImport(MigrateImportEvent $event) {
-    /** @var \Drupal\reinstall\Plugin\migrate\source\ReinstallUserSourceBase $plugin */
-    $plugin = $event->getMigration()->getSourcePlugin();
-    $plugin->records = array_filter($plugin->records, [$this, 'filter01']);
+  public function onPreImport(SourceEvent $event) {
+    $source = $event->source;
+    if ($source->getConfiguration()['type'] !== 'user') {
+      return;
+    }
+
+    $event->source->records = array_filter($event->source->records, [$this, 'filter01']);
     return;
   }
 

+ 61 - 1
src/Plugin/migrate/source/ReinstallSourceBase.php

@@ -2,11 +2,14 @@
 
 namespace Drupal\reinstall\Plugin\migrate\source;
 
+use Drupal\Component\Plugin\ConfigurablePluginInterface;
 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
 use Drupal\migrate\MigrateException;
 use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
 use Drupal\migrate\Plugin\MigrationInterface;
 use Drupal\migrate\Row;
+use Drupal\reinstall\ReinstallEvents;
+use Drupal\reinstall\SourceEvent;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -20,7 +23,7 @@ use Symfony\Component\Yaml\Yaml;
  *   id = "reinstall_base"
  * )
  */
-class ReinstallSourceBase extends SourcePluginBase implements ContainerFactoryPluginInterface {
+class ReinstallSourceBase extends SourcePluginBase implements ContainerFactoryPluginInterface, ConfigurablePluginInterface {
   use SimpleSourceTrait;
 
   /**
@@ -49,6 +52,7 @@ class ReinstallSourceBase extends SourcePluginBase implements ContainerFactoryPl
     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
     $this->eventDispatcher = $eventDispatcher;
     $this->records = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
+    $eventDispatcher->dispatch(ReinstallEvents::POST_SOURCE_PARSE, new SourceEvent($this));
   }
 
   /**
@@ -193,4 +197,60 @@ class ReinstallSourceBase extends SourcePluginBase implements ContainerFactoryPl
     return $ret;
   }
 
+  /**
+   * Gets this plugin's configuration.
+   *
+   * @return array
+   *   An array of this plugin's configuration.
+   */
+  public function getConfiguration() {
+    return $this->configuration;
+  }
+
+  /**
+   * Sets the configuration for this plugin instance.
+   *
+   * @param array $configuration
+   *   An associative array containing the plugin's configuration.
+   */
+  public function setConfiguration(array $configuration) {
+    $this->configuration = $configuration;
+  }
+
+  /**
+   * Gets default configuration for this plugin.
+   *
+   * @return array
+   *   An associative array with the default configuration.
+   */
+  public function defaultConfiguration() {
+    return [];
+  }
+
+  /**
+   * Calculates dependencies for the configured plugin.
+   *
+   * Dependencies are saved in the plugin's configuration entity and are used to
+   * determine configuration synchronization order. For example, if the plugin
+   * integrates with specific user roles, this method should return an array of
+   * dependencies listing the specified roles.
+   *
+   * @return array
+   *   An array of dependencies grouped by type (config, content, module,
+   *   theme). For example:
+   * @code
+   *   array(
+   *     'config' => array('user.role.anonymous', 'user.role.authenticated'),
+   *     'content' => array('node:article:f0a189e6-55fb-47fb-8005-5bef81c44d6d'),
+   *     'module' => array('node', 'user'),
+   *     'theme' => array('seven'),
+   *   );
+   * @endcode
+   *
+   * @see \Drupal\Core\Config\Entity\ConfigDependencyManager
+   * @see \Drupal\Core\Entity\EntityInterface::getConfigDependencyName()
+   */
+  public function calculateDependencies() {
+    return [];
+  }
 }

+ 1 - 0
src/ReinstallEvents.php

@@ -50,4 +50,5 @@ final class ReinstallEvents {
    */
   const PRE_DUMP = 'reinstall.dump.pre';
 
+  const POST_SOURCE_PARSE = 'reinstall.source_parse.post';
 }

+ 30 - 0
src/SourceEvent.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace Drupal\reinstall;
+
+use Drupal\migrate\Plugin\MigrateSourceInterface;
+use Drupal\reinstall\Plugin\migrate\source\ReinstallSourceBase;
+use Symfony\Component\EventDispatcher\Event;
+
+/**
+ * @see \Drupal\reinstall\ReinstallEvents::POST_SOURCE_PARSE
+ */
+class SourceEvent extends Event {
+
+  /**
+   * The source for the migration.
+   *
+   * @var MigrateSourceInterface
+   */
+  public $source;
+
+  /**
+   * DumperEvent constructor.
+   *
+   * @param \Drupal\reinstall\Plugin\migrate\source\ReinstallSourceBase $source
+   */
+  public function __construct(ReinstallSourceBase $source) {
+    $this->source = $source;
+  }
+
+}