X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Ftaxonomy%2Fsrc%2FPlugin%2Fmigrate%2FD7TaxonomyTermDeriver.php;fp=web%2Fcore%2Fmodules%2Ftaxonomy%2Fsrc%2FPlugin%2Fmigrate%2FD7TaxonomyTermDeriver.php;h=561a9afe8035e40e25aa06c4b2dbdf0f843a316e;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/taxonomy/src/Plugin/migrate/D7TaxonomyTermDeriver.php b/web/core/modules/taxonomy/src/Plugin/migrate/D7TaxonomyTermDeriver.php new file mode 100644 index 000000000..561a9afe8 --- /dev/null +++ b/web/core/modules/taxonomy/src/Plugin/migrate/D7TaxonomyTermDeriver.php @@ -0,0 +1,170 @@ +basePluginId = $base_plugin_id; + $this->cckPluginManager = $cck_manager; + $this->fieldPluginManager = $field_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, $base_plugin_id) { + return new static( + $base_plugin_id, + $container->get('plugin.manager.migrate.cckfield'), + $container->get('plugin.manager.migrate.field') + ); + } + + /** + * {@inheritdoc} + */ + public function getDerivativeDefinitions($base_plugin_definition) { + $fields = []; + try { + $source_plugin = static::getSourcePlugin('d7_field_instance'); + $source_plugin->checkRequirements(); + + // Read all field instance definitions in the source database. + foreach ($source_plugin as $row) { + if ($row->getSourceProperty('entity_type') == 'taxonomy_term') { + $fields[$row->getSourceProperty('bundle')][$row->getSourceProperty('field_name')] = $row->getSource(); + } + } + } + catch (RequirementsException $e) { + // If checkRequirements() failed then the field module did not exist and + // we do not have any fields. Therefore, $fields will be empty and below + // we'll create a migration just for the node properties. + } + + $vocabulary_source_plugin = static::getSourcePlugin('d7_taxonomy_vocabulary'); + try { + $vocabulary_source_plugin->checkRequirements(); + } + catch (RequirementsException $e) { + // If the d7_taxonomy_vocabulary requirements failed, that means we do not + // have a Drupal source database configured - there is nothing to + // generate. + return $this->derivatives; + } + + try { + foreach ($vocabulary_source_plugin as $row) { + $bundle = $row->getSourceProperty('machine_name'); + $values = $base_plugin_definition; + + $values['label'] = t('@label (@type)', [ + '@label' => $values['label'], + '@type' => $row->getSourceProperty('name'), + ]); + $values['source']['bundle'] = $bundle; + $values['destination']['default_bundle'] = $bundle; + + /** @var Migration $migration */ + $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($values); + if (isset($fields[$bundle])) { + foreach ($fields[$bundle] as $field_name => $info) { + $field_type = $info['type']; + try { + $plugin_id = $this->fieldPluginManager->getPluginIdFromFieldType($field_type, ['core' => 7], $migration); + if (!isset($this->fieldPluginCache[$field_type])) { + $this->fieldPluginCache[$field_type] = $this->fieldPluginManager->createInstance($plugin_id, ['core' => 7], $migration); + } + $this->fieldPluginCache[$field_type] + ->processFieldValues($migration, $field_name, $info); + } + catch (PluginNotFoundException $ex) { + try { + $plugin_id = $this->cckPluginManager->getPluginIdFromFieldType($field_type, ['core' => 7], $migration); + if (!isset($this->cckPluginCache[$field_type])) { + $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($plugin_id, ['core' => 7], $migration); + } + $this->cckPluginCache[$field_type] + ->processCckFieldValues($migration, $field_name, $info); + } + catch (PluginNotFoundException $ex) { + $migration->setProcessOfProperty($field_name, $field_name); + } + } + } + } + $this->derivatives[$bundle] = $migration->getPluginDefinition(); + } + } + catch (DatabaseExceptionWrapper $e) { + // Once we begin iterating the source plugin it is possible that the + // source tables will not exist. This can happen when the + // MigrationPluginManager gathers up the migration definitions but we do + // not actually have a Drupal 7 source database. + } + + return $this->derivatives; + } + +}