Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / node / src / Plugin / migrate / D6NodeDeriver.php
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 6 node and node revision migrations based on node types.
17  */
18 class D6NodeDeriver 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    * D6NodeDeriver 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    * Gets the definition of all derivatives of a base plugin.
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    */
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;
110     }
111
112     $node_types = static::getSourcePlugin('d6_node_type');
113     try {
114       $node_types->checkRequirements();
115     }
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;
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     }
137
138     try {
139       foreach ($node_types as $row) {
140         $node_type = $row->getSourceProperty('type');
141         $values = $base_plugin_definition;
142
143         $values['label'] = t("@label (@type)", [
144           '@label' => $values['label'],
145           '@type' => $node_type,
146         ]);
147         $values['source']['node_type'] = $node_type;
148         $values['destination']['default_bundle'] = $node_type;
149
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;
155         }
156
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'];
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();
186       }
187     }
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.
193     }
194
195     return $this->derivatives;
196   }
197
198 }