comparison core/modules/migrate_drupal/src/MigrationPluginManager.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php
2
3 namespace Drupal\migrate_drupal;
4
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Language\LanguageManagerInterface;
9 use Drupal\migrate\Plugin\Exception\BadPluginDefinitionException;
10 use Drupal\migrate\Plugin\MigrateSourcePluginManager;
11 use Drupal\migrate\Plugin\MigrationPluginManager as BaseMigrationPluginManager;
12
13 /**
14 * Manages migration plugins.
15 *
16 * Analyzes migration definitions to ensure that the source plugin of any
17 * migration tagged with particular tags ('Drupal 6' or 'Drupal 7' by default)
18 * defines a source_module property in its plugin annotation. This is done in
19 * order to support the Migrate Drupal UI, which needs to know which modules
20 * "own" the data being migrated into Drupal 8, on both the source and
21 * destination sides.
22 *
23 * @todo Enforce the destination_module property too, in
24 * https://www.drupal.org/project/drupal/issues/2923810.
25 */
26 class MigrationPluginManager extends BaseMigrationPluginManager {
27
28 /**
29 * The Migrate source plugin manager service.
30 *
31 * @var \Drupal\migrate\Plugin\MigrateSourcePluginManager
32 */
33 protected $sourceManager;
34
35 /**
36 * The config factory service.
37 *
38 * @var \Drupal\Core\Config\ConfigFactoryInterface
39 */
40 protected $configFactory;
41
42 /**
43 * The migration tags which will trigger source_module enforcement.
44 *
45 * @var string[]
46 */
47 protected $enforcedSourceModuleTags;
48
49 /**
50 * MigrationPluginManager constructor.
51 *
52 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
53 * The module handler service.
54 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
55 * The cache backend.
56 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
57 * The language manager service.
58 * @param \Drupal\migrate\Plugin\MigrateSourcePluginManager $source_manager
59 * The Migrate source plugin manager service.
60 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
61 * The config factory service.
62 */
63 public function __construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager, MigrateSourcePluginManager $source_manager, ConfigFactoryInterface $config_factory) {
64 parent::__construct($module_handler, $cache_backend, $language_manager);
65 $this->sourceManager = $source_manager;
66 $this->configFactory = $config_factory;
67 }
68
69 /**
70 * Returns the migration tags that trigger source_module enforcement.
71 *
72 * @return string[]
73 */
74 protected function getEnforcedSourceModuleTags() {
75 if ($this->enforcedSourceModuleTags === NULL) {
76 $this->enforcedSourceModuleTags = $this->configFactory
77 ->get('migrate_drupal.settings')
78 ->get('enforce_source_module_tags') ?: [];
79 }
80 return $this->enforcedSourceModuleTags;
81 }
82
83 /**
84 * {@inheritdoc}
85 */
86 public function processDefinition(&$definition, $plugin_id) {
87 parent::processDefinition($definition, $plugin_id);
88
89 // If the migration has no tags, we don't need to enforce the source_module
90 // annotation property.
91 if (empty($definition['migration_tags'])) {
92 return;
93 }
94
95 // Check if the migration has any of the tags that trigger source_module
96 // enforcement.
97 $applied_tags = array_intersect($this->getEnforcedSourceModuleTags(), $definition['migration_tags']);
98 if ($applied_tags) {
99 // Throw an exception if the source plugin definition does not define a
100 // source_module.
101 $source_id = $definition['source']['plugin'];
102 $source_definition = $this->sourceManager->getDefinition($source_id);
103 if (empty($source_definition['source_module'])) {
104 throw new BadPluginDefinitionException($source_id, 'source_module');
105 }
106 }
107 }
108
109 }