diff --git a/core/modules/migrate/src/Plugin/MigrationPluginManager.php b/core/modules/migrate/src/Plugin/MigrationPluginManager.php index 3ea5727..6209e7d 100644 --- a/core/modules/migrate/src/Plugin/MigrationPluginManager.php +++ b/core/modules/migrate/src/Plugin/MigrationPluginManager.php @@ -9,16 +9,20 @@ use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Plugin\DefaultPluginManager; use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator; -use Drupal\migrate\Plugin\Discovery\ProviderFilterDecorator; use Drupal\Core\Plugin\Discovery\YamlDirectoryDiscovery; use Drupal\Core\Plugin\Factory\ContainerFactory; use Drupal\migrate\MigrateBuildDependencyInterface; +use Drupal\migrate\Plugin\Discovery\ProviderFilterDecorator; +use Drupal\migrate\Plugin\migrate\destination\ComponentEntityDisplayBase; +use Drupal\migrate\Plugin\migrate\destination\Config; +use Drupal\migrate\Plugin\migrate\destination\EntityConfigBase; +use Drupal\user\Plugin\migrate\destination\UserData; /** * Plugin manager for migration plugins. */ class MigrationPluginManager extends DefaultPluginManager implements MigrationPluginManagerInterface, MigrateBuildDependencyInterface { - + /** * Provides default values for migrations. * @@ -27,21 +31,21 @@ class MigrationPluginManager extends DefaultPluginManager implements MigrationPl protected $defaults = array( 'class' => '\Drupal\migrate\Plugin\Migration', ); - + /** * The interface the plugins should implement. * * @var string */ protected $pluginInterface = 'Drupal\migrate\Plugin\MigrationInterface'; - + /** * The module handler. * * @var \Drupal\Core\Extension\ModuleHandlerInterface */ protected $moduleHandler; - + /** * Construct a migration plugin manager. * @@ -58,30 +62,70 @@ public function __construct(ModuleHandlerInterface $module_handler, CacheBackend $this->setCacheBackend($cache_backend, 'migration_plugins', array('migration_plugins')); $this->moduleHandler = $module_handler; } - + + /** + * {@inheritdoc} + */ + public function processDefinition(&$definition, $plugin_id) { + parent::processDefinition($definition, $plugin_id); + + // Loop over each of the definitions and tag it as config or content. + /** @var \Drupal\migrate\Plugin\MigratePluginManagerInterface $migrate_plugin_manager */ + $migrate_plugin_manager = \Drupal::getContainer() + ->get('plugin.manager.migrate.destination'); + $destination = $migrate_plugin_manager->getDefinition($definition['destination']['plugin'], FALSE); + if (!empty($destination['class'])) { + $definition += [ + 'migration_tags' => [], + ]; + $is_config_class = function ($class) { + return is_a($class, EntityConfigBase::class, TRUE) + || is_a($class, Config::class, TRUE) + || is_a($class, UserData::class, TRUE) + || is_a($class, ComponentEntityDisplayBase::class, TRUE); + }; + $is_already_assigned = function ($existing_tags) { + return in_array('Configuration', $existing_tags) || in_array('Content', $existing_tags); + }; + if ($is_config_class($destination['class']) && !$is_already_assigned($definition['migration_tags'])) { + $definition['migration_tags'][] = 'Configuration'; + } + // Default to content. + elseif (!$is_already_assigned($definition['migration_tags'])) { + $definition['migration_tags'][] = 'Content'; + } + } + } + /** * {@inheritdoc} */ protected function getDiscovery() { if (!isset($this->discovery)) { - $directories = array_map(function($directory) { - return [$directory . '/migration_templates', $directory . '/migrations']; + $directories = array_map(function ($directory) { + return [ + $directory . '/migration_templates', + $directory . '/migrations' + ]; }, $this->moduleHandler->getModuleDirectories()); - + $yaml_discovery = new YamlDirectoryDiscovery($directories, 'migrate'); // This gets rid of migrations which try to use a non-existent source // plugin. The common case for this is if the source plugin has, or // specifies, a non-existent provider. - $only_with_source_discovery = new NoSourcePluginDecorator($yaml_discovery); + $only_with_source_discovery = new NoSourcePluginDecorator($yaml_discovery); // This gets rid of migrations with explicit providers set if one of the // providers do not exist before we try to use a potentially non-existing // deriver. This is a rare case. - $filtered_discovery = new ProviderFilterDecorator($only_with_source_discovery, [$this->moduleHandler, 'moduleExists']); + $filtered_discovery = new ProviderFilterDecorator($only_with_source_discovery, [ + $this->moduleHandler, + 'moduleExists' + ]); $this->discovery = new ContainerDerivativeDiscoveryDecorator($filtered_discovery); } return $this->discovery; } - + /** * {@inheritdoc} */ @@ -89,7 +133,7 @@ public function createInstance($plugin_id, array $configuration = array()) { $instances = $this->createInstances([$plugin_id], [$plugin_id => $configuration]); return reset($instances); } - + /** * {@inheritdoc} */ @@ -97,24 +141,27 @@ public function createInstances($migration_id, array $configuration = array()) { if (empty($migration_id)) { $migration_id = array_keys($this->getDefinitions()); } - + $factory = $this->getFactory(); $migration_ids = (array) $migration_id; $plugin_ids = $this->expandPluginIds($migration_ids); - + $instances = []; foreach ($plugin_ids as $plugin_id) { $instances[$plugin_id] = $factory->createInstance($plugin_id, isset($configuration[$plugin_id]) ? $configuration[$plugin_id] : []); } - + foreach ($instances as $migration) { - $migration->set('migration_dependencies', array_map([$this, 'expandPluginIds'], $migration->getMigrationDependencies())); + $migration->set('migration_dependencies', array_map([ + $this, + 'expandPluginIds' + ], $migration->getMigrationDependencies())); } - + // Sort the migrations based on their dependencies. return $this->buildDependencyMigration($instances, []); } - + /** * Create migrations given a tag. * @@ -125,12 +172,12 @@ public function createInstances($migration_id, array $configuration = array()) { * An array of migration objects with the given tag. */ public function createInstancesByTag($tag) { - $migrations = array_filter($this->getDefinitions(), function($migration) use ($tag) { + $migrations = array_filter($this->getDefinitions(), function ($migration) use ($tag) { return !empty($migration['migration_tags']) && in_array($tag, $migration['migration_tags']); }); return $this->createInstances(array_keys($migrations)); } - + /** * Expand derivative migration dependencies. * @@ -154,8 +201,8 @@ protected function expandPluginIds(array $migration_ids) { } return $plugin_ids; } - - + + /** * {@inheritdoc} */ @@ -173,7 +220,7 @@ public function buildDependencyMigration(array $migrations, array $dynamic_ids) $requirements[$id] = []; $dependency_graph[$id]['edges'] = []; $migration_dependencies = $migration->getMigrationDependencies(); - + if (isset($migration_dependencies['required'])) { foreach ($migration_dependencies['required'] as $dependency) { if (!isset($dynamic_ids[$dependency])) { @@ -205,10 +252,10 @@ public function buildDependencyMigration(array $migrations, array $dynamic_ids) } } array_multisort($weights, SORT_DESC, SORT_NUMERIC, $migrations); - + return $migrations; } - + /** * Add one or more dependencies to a graph. * @@ -228,7 +275,7 @@ protected function addDependency(array &$graph, $id, $dependency, $dynamic_ids) } $graph[$id]['edges'] += array_combine($dependencies, $dependencies); } - + /** * {@inheritdoc} */ @@ -236,7 +283,7 @@ public function createStubMigration(array $definition) { $id = isset($definition['id']) ? $definition['id'] : uniqid(); return Migration::create(\Drupal::getContainer(), [], $id, $definition); } - + /** * Finds plugin definitions. * @@ -257,5 +304,4 @@ protected function findDefinitions() { return $this->providerExists($provider); }); } - }