diff --git a/core/modules/migrate/src/MigrateTemplateStorage.php b/core/modules/migrate/src/MigrateTemplateStorage.php index c0fba0e937e6567ea29f409683c91c3fc9bb89c6..d08f1aa8b27b3073ac0740e1542b25b90a09b081 100644 --- a/core/modules/migrate/src/MigrateTemplateStorage.php +++ b/core/modules/migrate/src/MigrateTemplateStorage.php @@ -14,10 +14,10 @@ * Storage to access migration template configuration in enabled extensions. */ class MigrateTemplateStorage { - /** * Extension sub-directory containing default configuration for migrations. */ + const MIGRATION_TEMPLATE_DIRECTORY = 'migration_templates'; /** @@ -62,6 +62,20 @@ public function findTemplatesByTag($tag) { return $matched_templates; } + /** + * Retrieve a template given a specific name. + * + * @param string $name + * A migration template name. + * + * @return NULL|array + * A parsed migration template, or NULL if it doesn't exist. + */ + public function getTemplateByName($name) { + $templates = $this->getAllTemplates(); + return isset($templates[$name]) ? $templates[$name] : NULL; + } + /** * Retrieves all migration templates belonging to enabled extensions. * @@ -69,7 +83,6 @@ public function findTemplatesByTag($tag) { * Array of parsed templates, keyed by the fully-qualified id. */ public function getAllTemplates() { - $templates = []; foreach ($this->moduleHandler->getModuleDirectories() as $directory) { $full_directory = $directory . '/' . $this->directory; diff --git a/core/modules/migrate/src/Tests/TemplateTest.php b/core/modules/migrate/src/Tests/TemplateTest.php index b110f6cb6949730a660782958acc4e7897a3229f..15c2305576795a6ad4cec6324ddaa70fcad10a0a 100644 --- a/core/modules/migrate/src/Tests/TemplateTest.php +++ b/core/modules/migrate/src/Tests/TemplateTest.php @@ -7,8 +7,6 @@ namespace Drupal\migrate\Tests; -use Drupal\migrate\MigrationStorage; - /** * Test the migration template functionality. * @@ -29,24 +27,52 @@ class TemplateTest extends MigrateTestBase { public function testTemplates() { $migration_templates = \Drupal::service('migrate.template_storage')->findTemplatesByTag("Template Test"); $expected_url = [ - 'id' => 'url_template', - 'label' => 'Template test - url', - 'migration_tags' => ['Template Test'], - 'source' => ['plugin' => 'empty'], - 'process' => ['src' => 'foobar'], - 'destination' => ['plugin' => 'url_alias'], + 'id' => 'url_template', + 'label' => 'Template test - url', + 'migration_tags' => ['Template Test'], + 'source' => ['plugin' => 'empty'], + 'process' => ['src' => 'foobar'], + 'destination' => ['plugin' => 'url_alias'], ]; $expected_node = [ - 'id' => 'node_template', - 'label' => 'Template test - node', - 'migration_tags' => ['Template Test'], - 'source' => ['plugin' => 'empty'], - 'process' => ['src' => 'barfoo'], - 'destination' => ['plugin' => 'entity:node'], + 'id' => 'node_template', + 'label' => 'Template test - node', + 'migration_tags' => ['Template Test'], + 'source' => ['plugin' => 'empty'], + 'process' => ['src' => 'barfoo'], + 'destination' => ['plugin' => 'entity:node'], ]; $this->assertIdentical($migration_templates['migrate.migration.url_template'], $expected_url); $this->assertIdentical($migration_templates['migrate.migration.node_template'], $expected_node); $this->assertFalse(isset($migration_templates['migrate.migration.other_template'])); } + /** + * Tests retrieving a template by name. + */ + public function testGetTemplateByName() { + /** @var \Drupal\migrate\MigrateTemplateStorage $template_storage */ + $template_storage = \Drupal::service('migrate.template_storage'); + + $expected_url = [ + 'id' => 'url_template', + 'label' => 'Template test - url', + 'migration_tags' => ['Template Test'], + 'source' => ['plugin' => 'empty'], + 'process' => ['src' => 'foobar'], + 'destination' => ['plugin' => 'url_alias'], + ]; + $expected_node = [ + 'id' => 'node_template', + 'label' => 'Template test - node', + 'migration_tags' => ['Template Test'], + 'source' => ['plugin' => 'empty'], + 'process' => ['src' => 'barfoo'], + 'destination' => ['plugin' => 'entity:node'], + ]; + $this->assertIdentical($template_storage->getTemplateByName('migrate.migration.url_template'), $expected_url); + $this->assertIdentical($template_storage->getTemplateByName('migrate.migration.node_template'), $expected_node); + $this->assertNull($template_storage->getTemplateByName('migrate.migration.dne')); + } + }