diff --git a/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php b/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php index ee43451..81f528f 100644 --- a/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php +++ b/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php @@ -2,13 +2,16 @@ namespace Drupal\migrate\Plugin\migrate\source; +use Drupal\Core\Database\ConnectionNotDefinedException; use Drupal\Core\Database\Database; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\State\StateInterface; +use Drupal\migrate\Exception\RequirementsException; use Drupal\migrate\MigrateException; use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate\Plugin\migrate\id_map\Sql; use Drupal\migrate\Plugin\MigrateIdMapInterface; +use Drupal\migrate\Plugin\RequirementsInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -20,7 +23,7 @@ * is present, it is used as a database connection information array to define * the connection. */ -abstract class SqlBase extends SourcePluginBase implements ContainerFactoryPluginInterface { +abstract class SqlBase extends SourcePluginBase implements ContainerFactoryPluginInterface, RequirementsInterface { /** * The query string. @@ -127,12 +130,17 @@ public function getDatabase() { * * @return \Drupal\Core\Database\Connection * The connection to use for this plugin's queries. + * + * @throws \Drupal\migrate\Exception\RequirementsException + * Thrown if there is no properly-configured database. */ protected function setUpDatabase(array $database_info) { if (isset($database_info['key'])) { $key = $database_info['key']; } else { + // If there is no explicit database configuration at all, fallback to a + // connection named 'migrate'. $key = 'migrate'; } if (isset($database_info['target'])) { @@ -144,7 +152,29 @@ protected function setUpDatabase(array $database_info) { if (isset($database_info['database'])) { Database::addConnectionInfo($key, $target, $database_info['database']); } - return Database::getConnection($target, $key); + try { + $connection = Database::getConnection($target, $key); + } + catch (ConnectionNotDefinedException $e) { + // If we fell back to the magic 'migrate' connection and it doesn't exist, + // treat the lack of the connection as a RequirementsException. + if ($key == 'migrate') { + throw new RequirementsException("No database connection configured for source plugin " . $this->pluginId); + } + else { + throw $e; + } + } + return $connection; + } + + /** + * {@inheritdoc} + */ + public function checkRequirements() { + if ($this->pluginDefinition['requirements_met'] === TRUE) { + $this->getDatabase(); + } } /** diff --git a/core/modules/migrate_drupal/src/Plugin/migrate/source/DrupalSqlBase.php b/core/modules/migrate_drupal/src/Plugin/migrate/source/DrupalSqlBase.php index 3af971c..3c3624d 100644 --- a/core/modules/migrate_drupal/src/Plugin/migrate/source/DrupalSqlBase.php +++ b/core/modules/migrate_drupal/src/Plugin/migrate/source/DrupalSqlBase.php @@ -10,7 +10,6 @@ use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate\Exception\RequirementsException; use Drupal\migrate\Plugin\migrate\source\SqlBase; -use Drupal\migrate\Plugin\RequirementsInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -19,7 +18,7 @@ * Mainly to let children retrieve information from the origin system in an * easier way. */ -abstract class DrupalSqlBase extends SqlBase implements ContainerFactoryPluginInterface, RequirementsInterface, DependentPluginInterface { +abstract class DrupalSqlBase extends SqlBase implements ContainerFactoryPluginInterface, DependentPluginInterface { use DependencyTrait; @@ -106,6 +105,7 @@ public function checkRequirements() { } } } + parent::checkRequirements(); } /** diff --git a/core/modules/node/src/Plugin/migrate/D6NodeDeriver.php b/core/modules/node/src/Plugin/migrate/D6NodeDeriver.php index 3fba70a..9b2c3cd 100644 --- a/core/modules/node/src/Plugin/migrate/D6NodeDeriver.php +++ b/core/modules/node/src/Plugin/migrate/D6NodeDeriver.php @@ -90,6 +90,16 @@ public function getDerivativeDefinitions($base_plugin_definition) { return $this->derivatives; } + $node_types = static::getSourcePlugin('d6_node_type'); + try { + $node_types->checkRequirements(); + } + catch (RequirementsException $e) { + // If the d6_node_type requirements failed, that means we do not have a + // Drupal source database configured - there is nothing to generate. + return $this->derivatives; + } + // Read all CCK field instance definitions in the source database. $fields = array(); try { @@ -107,7 +117,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { } try { - foreach (static::getSourcePlugin('d6_node_type') as $row) { + foreach ($node_types as $row) { $node_type = $row->getSourceProperty('type'); $values = $base_plugin_definition; diff --git a/core/modules/node/src/Plugin/migrate/D7NodeDeriver.php b/core/modules/node/src/Plugin/migrate/D7NodeDeriver.php index bd3d8b9..3fc2682 100644 --- a/core/modules/node/src/Plugin/migrate/D7NodeDeriver.php +++ b/core/modules/node/src/Plugin/migrate/D7NodeDeriver.php @@ -65,6 +65,16 @@ public static function create(ContainerInterface $container, $base_plugin_id) { * {@inheritdoc} */ public function getDerivativeDefinitions($base_plugin_definition) { + $node_types = static::getSourcePlugin('d7_node_type'); + try { + $node_types->checkRequirements(); + } + catch (RequirementsException $e) { + // If the d7_node_type requirements failed, that means we do not have a + // Drupal source database configured - there is nothing to generate. + return $this->derivatives; + } + $fields = []; try { $source_plugin = static::getSourcePlugin('d7_field_instance'); @@ -84,7 +94,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { } try { - foreach (static::getSourcePlugin('d7_node_type') as $row) { + foreach ($node_types as $row) { $node_type = $row->getSourceProperty('type'); $values = $base_plugin_definition; diff --git a/core/modules/node/tests/src/Kernel/Migrate/MigratePluginListTest.php b/core/modules/node/tests/src/Kernel/Migrate/MigratePluginListTest.php new file mode 100644 index 0000000..e220717 --- /dev/null +++ b/core/modules/node/tests/src/Kernel/Migrate/MigratePluginListTest.php @@ -0,0 +1,59 @@ +container->get('plugin.manager.migration')->createInstances([]); + // Any database-based source plugins should fail a requirements test in the + // absence of a database connection. + /** @var \Drupal\migrate\Plugin\Migration $migration */ + foreach ($migration_plugins as $id => $migration) { + if ($migration->getSourcePlugin() instanceof RequirementsInterface) { + try { + $migration->getSourcePlugin()->checkRequirements(); + } + catch (RequirementsException $e) { + unset($migration_plugins[$id]); + } + } + } + + // Without a connection defined, no database-based plugins should be + // returned. + foreach ($migration_plugins as $id => $migration) { + $this->assertNotInstanceOf(SqlBase::class, $migration->getSourcePlugin()); + } + } + +}