comparison core/modules/node/src/Plugin/migrate/D6NodeDeriver.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 129ea1e6d783
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php 1 <?php
2 2
3 namespace Drupal\node\Plugin\migrate; 3 namespace Drupal\node\Plugin\migrate;
4 4
5 use Drupal\Component\Plugin\Derivative\DeriverBase; 5 use Drupal\Component\Plugin\Derivative\DeriverBase;
6 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
7 use Drupal\Core\Database\DatabaseExceptionWrapper; 6 use Drupal\Core\Database\DatabaseExceptionWrapper;
8 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; 7 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
9 use Drupal\migrate\Exception\RequirementsException; 8 use Drupal\migrate\Exception\RequirementsException;
10 use Drupal\migrate\Plugin\MigrationDeriverTrait; 9 use Drupal\migrate\Plugin\MigrationDeriverTrait;
11 use Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface; 10 use Drupal\migrate_drupal\FieldDiscoveryInterface;
12 use Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface; 11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 12
15 /** 13 /**
16 * Deriver for Drupal 6 node and node revision migrations based on node types. 14 * Deriver for Drupal 6 node and node revision migrations based on node types.
17 */ 15 */
24 * @var string 22 * @var string
25 */ 23 */
26 protected $basePluginId; 24 protected $basePluginId;
27 25
28 /** 26 /**
29 * Already-instantiated cckfield plugins, keyed by ID.
30 *
31 * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldInterface[]
32 */
33 protected $cckPluginCache;
34
35 /**
36 * The CCK plugin manager.
37 *
38 * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface
39 */
40 protected $cckPluginManager;
41
42 /**
43 * Already-instantiated field plugins, keyed by ID.
44 *
45 * @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface[]
46 */
47 protected $fieldPluginCache;
48
49 /**
50 * The field plugin manager.
51 *
52 * @var \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface
53 */
54 protected $fieldPluginManager;
55
56 /**
57 * Whether or not to include translations. 27 * Whether or not to include translations.
58 * 28 *
59 * @var bool 29 * @var bool
60 */ 30 */
61 protected $includeTranslations; 31 protected $includeTranslations;
62 32
63 /** 33 /**
34 * The migration field discovery service.
35 *
36 * @var \Drupal\migrate_drupal\FieldDiscoveryInterface
37 */
38 protected $fieldDiscovery;
39
40 /**
64 * D6NodeDeriver constructor. 41 * D6NodeDeriver constructor.
65 * 42 *
66 * @param string $base_plugin_id 43 * @param string $base_plugin_id
67 * The base plugin ID for the plugin ID. 44 * The base plugin ID for the plugin ID.
68 * @param \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface $cck_manager
69 * The CCK plugin manager.
70 * @param \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface $field_manager
71 * The field plugin manager.
72 * @param bool $translations 45 * @param bool $translations
73 * Whether or not to include translations. 46 * Whether or not to include translations.
47 * @param \Drupal\migrate_drupal\FieldDiscoveryInterface $field_discovery
48 * The migration field discovery service.
74 */ 49 */
75 public function __construct($base_plugin_id, MigrateCckFieldPluginManagerInterface $cck_manager, MigrateFieldPluginManagerInterface $field_manager, $translations) { 50 public function __construct($base_plugin_id, $translations, FieldDiscoveryInterface $field_discovery) {
76 $this->basePluginId = $base_plugin_id; 51 $this->basePluginId = $base_plugin_id;
77 $this->cckPluginManager = $cck_manager;
78 $this->fieldPluginManager = $field_manager;
79 $this->includeTranslations = $translations; 52 $this->includeTranslations = $translations;
53 $this->fieldDiscovery = $field_discovery;
80 } 54 }
81 55
82 /** 56 /**
83 * {@inheritdoc} 57 * {@inheritdoc}
84 */ 58 */
85 public static function create(ContainerInterface $container, $base_plugin_id) { 59 public static function create(ContainerInterface $container, $base_plugin_id) {
86 // Translations don't make sense unless we have content_translation. 60 // Translations don't make sense unless we have content_translation.
87 return new static( 61 return new static(
88 $base_plugin_id, 62 $base_plugin_id,
89 $container->get('plugin.manager.migrate.cckfield'), 63 $container->get('module_handler')->moduleExists('content_translation'),
90 $container->get('plugin.manager.migrate.field'), 64 $container->get('migrate_drupal.field_discovery')
91 $container->get('module_handler')->moduleExists('content_translation')
92 ); 65 );
93 } 66 }
94 67
95 /** 68 /**
96 * Gets the definition of all derivatives of a base plugin. 69 * {@inheritdoc}
97 *
98 * @param array $base_plugin_definition
99 * The definition array of the base plugin.
100 *
101 * @return array
102 * An array of full derivative definitions keyed on derivative id.
103 *
104 * @see \Drupal\Component\Plugin\Derivative\DeriverBase::getDerivativeDefinition()
105 */ 70 */
106 public function getDerivativeDefinitions($base_plugin_definition) { 71 public function getDerivativeDefinitions($base_plugin_definition) {
107 if ($base_plugin_definition['id'] == 'd6_node_translation' && !$this->includeTranslations) { 72 if ($base_plugin_definition['id'] == 'd6_node_translation' && !$this->includeTranslations) {
108 // Refuse to generate anything. 73 // Refuse to generate anything.
109 return $this->derivatives; 74 return $this->derivatives;
115 } 80 }
116 catch (RequirementsException $e) { 81 catch (RequirementsException $e) {
117 // If the d6_node_type requirements failed, that means we do not have a 82 // If the d6_node_type requirements failed, that means we do not have a
118 // Drupal source database configured - there is nothing to generate. 83 // Drupal source database configured - there is nothing to generate.
119 return $this->derivatives; 84 return $this->derivatives;
120 }
121
122 // Read all field instance definitions in the source database.
123 $fields = [];
124 try {
125 $source_plugin = static::getSourcePlugin('d6_field_instance');
126 $source_plugin->checkRequirements();
127
128 foreach ($source_plugin as $row) {
129 $fields[$row->getSourceProperty('type_name')][$row->getSourceProperty('field_name')] = $row->getSource();
130 }
131 }
132 catch (RequirementsException $e) {
133 // If checkRequirements() failed then the content module did not exist and
134 // we do not have any fields. Therefore, $fields will be empty and
135 // below we'll create a migration just for the node properties.
136 } 85 }
137 86
138 try { 87 try {
139 foreach ($node_types as $row) { 88 foreach ($node_types as $row) {
140 $node_type = $row->getSourceProperty('type'); 89 $node_type = $row->getSourceProperty('type');
154 $values['migration_dependencies']['required'][] = 'd6_node:' . $node_type; 103 $values['migration_dependencies']['required'][] = 'd6_node:' . $node_type;
155 } 104 }
156 105
157 /** @var \Drupal\migrate\Plugin\Migration $migration */ 106 /** @var \Drupal\migrate\Plugin\Migration $migration */
158 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($values); 107 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($values);
159 if (isset($fields[$node_type])) { 108 $this->fieldDiscovery->addBundleFieldProcesses($migration, 'node', $node_type);
160 foreach ($fields[$node_type] as $field_name => $info) {
161 $field_type = $info['type'];
162 try {
163 $plugin_id = $this->fieldPluginManager->getPluginIdFromFieldType($field_type, ['core' => 6], $migration);
164 if (!isset($this->fieldPluginCache[$field_type])) {
165 $this->fieldPluginCache[$field_type] = $this->fieldPluginManager->createInstance($plugin_id, ['core' => 6], $migration);
166 }
167 $this->fieldPluginCache[$field_type]
168 ->defineValueProcessPipeline($migration, $field_name, $info);
169 }
170 catch (PluginNotFoundException $ex) {
171 try {
172 $plugin_id = $this->cckPluginManager->getPluginIdFromFieldType($field_type, ['core' => 6], $migration);
173 if (!isset($this->cckPluginCache[$field_type])) {
174 $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($plugin_id, ['core' => 6], $migration);
175 }
176 $this->cckPluginCache[$field_type]
177 ->processCckFieldValues($migration, $field_name, $info);
178 }
179 catch (PluginNotFoundException $ex) {
180 $migration->setProcessOfProperty($field_name, $field_name);
181 }
182 }
183 }
184 }
185 $this->derivatives[$node_type] = $migration->getPluginDefinition(); 109 $this->derivatives[$node_type] = $migration->getPluginDefinition();
186 } 110 }
187 } 111 }
188 catch (DatabaseExceptionWrapper $e) { 112 catch (DatabaseExceptionWrapper $e) {
189 // Once we begin iterating the source plugin it is possible that the 113 // Once we begin iterating the source plugin it is possible that the