comparison core/modules/node/src/Plugin/migrate/D7NodeDeriver.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\node\Plugin\migrate;
4
5 use Drupal\Component\Plugin\Derivative\DeriverBase;
6 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
7 use Drupal\Core\Database\DatabaseExceptionWrapper;
8 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
9 use Drupal\migrate\Exception\RequirementsException;
10 use Drupal\migrate\Plugin\MigrationDeriverTrait;
11 use Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface;
12 use Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16 * Deriver for Drupal 7 node and node revision migrations based on node types.
17 */
18 class D7NodeDeriver extends DeriverBase implements ContainerDeriverInterface {
19 use MigrationDeriverTrait;
20
21 /**
22 * The base plugin ID this derivative is for.
23 *
24 * @var string
25 */
26 protected $basePluginId;
27
28 /**
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.
58 *
59 * @var bool
60 */
61 protected $includeTranslations;
62
63 /**
64 * D7NodeDeriver constructor.
65 *
66 * @param string $base_plugin_id
67 * 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
73 * Whether or not to include translations.
74 */
75 public function __construct($base_plugin_id, MigrateCckFieldPluginManagerInterface $cck_manager, MigrateFieldPluginManagerInterface $field_manager, $translations) {
76 $this->basePluginId = $base_plugin_id;
77 $this->cckPluginManager = $cck_manager;
78 $this->fieldPluginManager = $field_manager;
79 $this->includeTranslations = $translations;
80 }
81
82 /**
83 * {@inheritdoc}
84 */
85 public static function create(ContainerInterface $container, $base_plugin_id) {
86 // Translations don't make sense unless we have content_translation.
87 return new static(
88 $base_plugin_id,
89 $container->get('plugin.manager.migrate.cckfield'),
90 $container->get('plugin.manager.migrate.field'),
91 $container->get('module_handler')->moduleExists('content_translation')
92 );
93 }
94
95 /**
96 * {@inheritdoc}
97 */
98 public function getDerivativeDefinitions($base_plugin_definition) {
99 if (in_array('translation', $base_plugin_definition['migration_tags']) && !$this->includeTranslations) {
100 // Refuse to generate anything.
101 return $this->derivatives;
102 }
103
104 $node_types = static::getSourcePlugin('d7_node_type');
105 try {
106 $node_types->checkRequirements();
107 }
108 catch (RequirementsException $e) {
109 // If the d7_node_type requirements failed, that means we do not have a
110 // Drupal source database configured - there is nothing to generate.
111 return $this->derivatives;
112 }
113
114 $fields = [];
115 try {
116 $source_plugin = static::getSourcePlugin('d7_field_instance');
117 $source_plugin->checkRequirements();
118
119 // Read all field instance definitions in the source database.
120 foreach ($source_plugin as $row) {
121 if ($row->getSourceProperty('entity_type') == 'node') {
122 $fields[$row->getSourceProperty('bundle')][$row->getSourceProperty('field_name')] = $row->getSource();
123 }
124 }
125 }
126 catch (RequirementsException $e) {
127 // If checkRequirements() failed then the field module did not exist and
128 // we do not have any fields. Therefore, $fields will be empty and below
129 // we'll create a migration just for the node properties.
130 }
131
132 try {
133 foreach ($node_types as $row) {
134 $node_type = $row->getSourceProperty('type');
135 $values = $base_plugin_definition;
136
137 $values['label'] = t('@label (@type)', [
138 '@label' => $values['label'],
139 '@type' => $row->getSourceProperty('name'),
140 ]);
141 $values['source']['node_type'] = $node_type;
142 $values['destination']['default_bundle'] = $node_type;
143
144 // Comment status must be mapped to correct comment type.
145 // Comment type migration creates a separate comment type for each
146 // node type except for Forum which uses 'comment_forum'.
147 $comment_type = 'comment_node_' . $node_type;
148 if ($node_type == 'forum') {
149 $comment_type = 'comment_forum';
150 }
151 $nested_key = $comment_type . '/0/status';
152 $values['process'][$nested_key] = 'comment';
153
154 // If this migration is based on the d7_node_revision migration or
155 // is for translations of nodes, it should explicitly depend on the
156 // corresponding d7_node variant.
157 if ($base_plugin_definition['id'] == ['d7_node_revision'] || in_array('translation', $base_plugin_definition['migration_tags'])) {
158 $values['migration_dependencies']['required'][] = 'd7_node:' . $node_type;
159 }
160
161 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($values);
162 if (isset($fields[$node_type])) {
163 foreach ($fields[$node_type] as $field_name => $info) {
164 $field_type = $info['type'];
165 try {
166 $plugin_id = $this->fieldPluginManager->getPluginIdFromFieldType($field_type, ['core' => 7], $migration);
167 if (!isset($this->fieldPluginCache[$field_type])) {
168 $this->fieldPluginCache[$field_type] = $this->fieldPluginManager->createInstance($plugin_id, ['core' => 7], $migration);
169 }
170 $this->fieldPluginCache[$field_type]
171 ->processFieldValues($migration, $field_name, $info);
172 }
173 catch (PluginNotFoundException $ex) {
174 try {
175 $plugin_id = $this->cckPluginManager->getPluginIdFromFieldType($field_type, ['core' => 7], $migration);
176 if (!isset($this->cckPluginCache[$field_type])) {
177 $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($plugin_id, ['core' => 7], $migration);
178 }
179 $this->cckPluginCache[$field_type]
180 ->processCckFieldValues($migration, $field_name, $info);
181 }
182 catch (PluginNotFoundException $ex) {
183 $migration->setProcessOfProperty($field_name, $field_name);
184 }
185 }
186 }
187 }
188 $this->derivatives[$node_type] = $migration->getPluginDefinition();
189 }
190 }
191 catch (DatabaseExceptionWrapper $e) {
192 // Once we begin iterating the source plugin it is possible that the
193 // source tables will not exist. This can happen when the
194 // MigrationPluginManager gathers up the migration definitions but we do
195 // not actually have a Drupal 7 source database.
196 }
197 return $this->derivatives;
198 }
199
200 }