3 namespace Drupal\node\Plugin\migrate;
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;
16 * Deriver for Drupal 6 node and node revision migrations based on node types.
18 class D6NodeDeriver extends DeriverBase implements ContainerDeriverInterface {
19 use MigrationDeriverTrait;
22 * The base plugin ID this derivative is for.
26 protected $basePluginId;
29 * Already-instantiated cckfield plugins, keyed by ID.
31 * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldInterface[]
33 protected $cckPluginCache;
36 * The CCK plugin manager.
38 * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface
40 protected $cckPluginManager;
43 * Already-instantiated field plugins, keyed by ID.
45 * @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface[]
47 protected $fieldPluginCache;
50 * The field plugin manager.
52 * @var \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface
54 protected $fieldPluginManager;
57 * Whether or not to include translations.
61 protected $includeTranslations;
64 * D6NodeDeriver constructor.
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.
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;
85 public static function create(ContainerInterface $container, $base_plugin_id) {
86 // Translations don't make sense unless we have content_translation.
89 $container->get('plugin.manager.migrate.cckfield'),
90 $container->get('plugin.manager.migrate.field'),
91 $container->get('module_handler')->moduleExists('content_translation')
96 * Gets the definition of all derivatives of a base plugin.
98 * @param array $base_plugin_definition
99 * The definition array of the base plugin.
102 * An array of full derivative definitions keyed on derivative id.
104 * @see \Drupal\Component\Plugin\Derivative\DeriverBase::getDerivativeDefinition()
106 public function getDerivativeDefinitions($base_plugin_definition) {
107 if ($base_plugin_definition['id'] == 'd6_node_translation' && !$this->includeTranslations) {
108 // Refuse to generate anything.
109 return $this->derivatives;
112 $node_types = static::getSourcePlugin('d6_node_type');
114 $node_types->checkRequirements();
116 catch (RequirementsException $e) {
117 // If the d6_node_type requirements failed, that means we do not have a
118 // Drupal source database configured - there is nothing to generate.
119 return $this->derivatives;
122 // Read all field instance definitions in the source database.
125 $source_plugin = static::getSourcePlugin('d6_field_instance');
126 $source_plugin->checkRequirements();
128 foreach ($source_plugin as $row) {
129 $fields[$row->getSourceProperty('type_name')][$row->getSourceProperty('field_name')] = $row->getSource();
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.
139 foreach ($node_types as $row) {
140 $node_type = $row->getSourceProperty('type');
141 $values = $base_plugin_definition;
143 $values['label'] = t("@label (@type)", [
144 '@label' => $values['label'],
145 '@type' => $node_type,
147 $values['source']['node_type'] = $node_type;
148 $values['destination']['default_bundle'] = $node_type;
150 // If this migration is based on the d6_node_revision migration or
151 // is for translations of nodes, it should explicitly depend on the
152 // corresponding d6_node variant.
153 if (in_array($base_plugin_definition['id'], ['d6_node_revision', 'd6_node_translation'])) {
154 $values['migration_dependencies']['required'][] = 'd6_node:' . $node_type;
157 /** @var \Drupal\migrate\Plugin\Migration $migration */
158 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($values);
159 if (isset($fields[$node_type])) {
160 foreach ($fields[$node_type] as $field_name => $info) {
161 $field_type = $info['type'];
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);
167 $this->fieldPluginCache[$field_type]
168 ->defineValueProcessPipeline($migration, $field_name, $info);
170 catch (PluginNotFoundException $ex) {
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);
176 $this->cckPluginCache[$field_type]
177 ->processCckFieldValues($migration, $field_name, $info);
179 catch (PluginNotFoundException $ex) {
180 $migration->setProcessOfProperty($field_name, $field_name);
185 $this->derivatives[$node_type] = $migration->getPluginDefinition();
188 catch (DatabaseExceptionWrapper $e) {
189 // Once we begin iterating the source plugin it is possible that the
190 // source tables will not exist. This can happen when the
191 // MigrationPluginManager gathers up the migration definitions but we do
192 // not actually have a Drupal 6 source database.
195 return $this->derivatives;