3 namespace Drupal\taxonomy\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 7 taxonomy term migrations based on vocabularies.
18 class D7TaxonomyTermDeriver extends DeriverBase implements ContainerDeriverInterface {
20 use MigrationDeriverTrait;
23 * The base plugin ID this derivative is for.
27 protected $basePluginId;
30 * Already-instantiated cckfield plugins, keyed by ID.
32 * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldInterface[]
34 protected $cckPluginCache;
37 * The CCK plugin manager.
39 * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface
41 protected $cckPluginManager;
44 * Already-instantiated field plugins, keyed by ID.
46 * @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface[]
48 protected $fieldPluginCache;
51 * The field plugin manager.
53 * @var \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface
55 protected $fieldPluginManager;
58 * D7TaxonomyTermDeriver constructor.
60 * @param string $base_plugin_id
61 * The base plugin ID for the plugin ID.
62 * @param \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface $cck_manager
63 * The CCK plugin manager.
64 * @param \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface $field_manager
65 * The field plugin manager.
67 public function __construct($base_plugin_id, MigrateCckFieldPluginManagerInterface $cck_manager, MigrateFieldPluginManagerInterface $field_manager) {
68 $this->basePluginId = $base_plugin_id;
69 $this->cckPluginManager = $cck_manager;
70 $this->fieldPluginManager = $field_manager;
76 public static function create(ContainerInterface $container, $base_plugin_id) {
79 $container->get('plugin.manager.migrate.cckfield'),
80 $container->get('plugin.manager.migrate.field')
87 public function getDerivativeDefinitions($base_plugin_definition) {
90 $source_plugin = static::getSourcePlugin('d7_field_instance');
91 $source_plugin->checkRequirements();
93 // Read all field instance definitions in the source database.
94 foreach ($source_plugin as $row) {
95 if ($row->getSourceProperty('entity_type') == 'taxonomy_term') {
96 $fields[$row->getSourceProperty('bundle')][$row->getSourceProperty('field_name')] = $row->getSource();
100 catch (RequirementsException $e) {
101 // If checkRequirements() failed then the field module did not exist and
102 // we do not have any fields. Therefore, $fields will be empty and below
103 // we'll create a migration just for the node properties.
106 $vocabulary_source_plugin = static::getSourcePlugin('d7_taxonomy_vocabulary');
108 $vocabulary_source_plugin->checkRequirements();
110 catch (RequirementsException $e) {
111 // If the d7_taxonomy_vocabulary requirements failed, that means we do not
112 // have a Drupal source database configured - there is nothing to
114 return $this->derivatives;
118 foreach ($vocabulary_source_plugin as $row) {
119 $bundle = $row->getSourceProperty('machine_name');
120 $values = $base_plugin_definition;
122 $values['label'] = t('@label (@type)', [
123 '@label' => $values['label'],
124 '@type' => $row->getSourceProperty('name'),
126 $values['source']['bundle'] = $bundle;
127 $values['destination']['default_bundle'] = $bundle;
129 /** @var Migration $migration */
130 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($values);
131 if (isset($fields[$bundle])) {
132 foreach ($fields[$bundle] as $field_name => $info) {
133 $field_type = $info['type'];
135 $plugin_id = $this->fieldPluginManager->getPluginIdFromFieldType($field_type, ['core' => 7], $migration);
136 if (!isset($this->fieldPluginCache[$field_type])) {
137 $this->fieldPluginCache[$field_type] = $this->fieldPluginManager->createInstance($plugin_id, ['core' => 7], $migration);
139 $this->fieldPluginCache[$field_type]
140 ->processFieldValues($migration, $field_name, $info);
142 catch (PluginNotFoundException $ex) {
144 $plugin_id = $this->cckPluginManager->getPluginIdFromFieldType($field_type, ['core' => 7], $migration);
145 if (!isset($this->cckPluginCache[$field_type])) {
146 $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($plugin_id, ['core' => 7], $migration);
148 $this->cckPluginCache[$field_type]
149 ->processCckFieldValues($migration, $field_name, $info);
151 catch (PluginNotFoundException $ex) {
152 $migration->setProcessOfProperty($field_name, $field_name);
157 $this->derivatives[$bundle] = $migration->getPluginDefinition();
160 catch (DatabaseExceptionWrapper $e) {
161 // Once we begin iterating the source plugin it is possible that the
162 // source tables will not exist. This can happen when the
163 // MigrationPluginManager gathers up the migration definitions but we do
164 // not actually have a Drupal 7 source database.
167 return $this->derivatives;