Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\taxonomy\Plugin\migrate;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Plugin\Derivative\DeriverBase;
|
Chris@0
|
6 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
|
Chris@0
|
7 use Drupal\Core\Database\DatabaseExceptionWrapper;
|
Chris@0
|
8 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
|
Chris@0
|
9 use Drupal\migrate\Exception\RequirementsException;
|
Chris@0
|
10 use Drupal\migrate\Plugin\MigrationDeriverTrait;
|
Chris@0
|
11 use Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface;
|
Chris@0
|
12 use Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface;
|
Chris@0
|
13 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Deriver for Drupal 7 taxonomy term migrations based on vocabularies.
|
Chris@0
|
17 */
|
Chris@0
|
18 class D7TaxonomyTermDeriver extends DeriverBase implements ContainerDeriverInterface {
|
Chris@0
|
19
|
Chris@0
|
20 use MigrationDeriverTrait;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * The base plugin ID this derivative is for.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var string
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $basePluginId;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * Already-instantiated cckfield plugins, keyed by ID.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldInterface[]
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $cckPluginCache;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * The CCK plugin manager.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface
|
Chris@0
|
40 */
|
Chris@0
|
41 protected $cckPluginManager;
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Already-instantiated field plugins, keyed by ID.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface[]
|
Chris@0
|
47 */
|
Chris@0
|
48 protected $fieldPluginCache;
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * The field plugin manager.
|
Chris@0
|
52 *
|
Chris@0
|
53 * @var \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface
|
Chris@0
|
54 */
|
Chris@0
|
55 protected $fieldPluginManager;
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * D7TaxonomyTermDeriver constructor.
|
Chris@0
|
59 *
|
Chris@0
|
60 * @param string $base_plugin_id
|
Chris@0
|
61 * The base plugin ID for the plugin ID.
|
Chris@0
|
62 * @param \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface $cck_manager
|
Chris@0
|
63 * The CCK plugin manager.
|
Chris@0
|
64 * @param \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface $field_manager
|
Chris@0
|
65 * The field plugin manager.
|
Chris@0
|
66 */
|
Chris@0
|
67 public function __construct($base_plugin_id, MigrateCckFieldPluginManagerInterface $cck_manager, MigrateFieldPluginManagerInterface $field_manager) {
|
Chris@0
|
68 $this->basePluginId = $base_plugin_id;
|
Chris@0
|
69 $this->cckPluginManager = $cck_manager;
|
Chris@0
|
70 $this->fieldPluginManager = $field_manager;
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * {@inheritdoc}
|
Chris@0
|
75 */
|
Chris@0
|
76 public static function create(ContainerInterface $container, $base_plugin_id) {
|
Chris@0
|
77 return new static(
|
Chris@0
|
78 $base_plugin_id,
|
Chris@0
|
79 $container->get('plugin.manager.migrate.cckfield'),
|
Chris@0
|
80 $container->get('plugin.manager.migrate.field')
|
Chris@0
|
81 );
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * {@inheritdoc}
|
Chris@0
|
86 */
|
Chris@0
|
87 public function getDerivativeDefinitions($base_plugin_definition) {
|
Chris@0
|
88 $fields = [];
|
Chris@0
|
89 try {
|
Chris@0
|
90 $source_plugin = static::getSourcePlugin('d7_field_instance');
|
Chris@0
|
91 $source_plugin->checkRequirements();
|
Chris@0
|
92
|
Chris@0
|
93 // Read all field instance definitions in the source database.
|
Chris@0
|
94 foreach ($source_plugin as $row) {
|
Chris@0
|
95 if ($row->getSourceProperty('entity_type') == 'taxonomy_term') {
|
Chris@0
|
96 $fields[$row->getSourceProperty('bundle')][$row->getSourceProperty('field_name')] = $row->getSource();
|
Chris@0
|
97 }
|
Chris@0
|
98 }
|
Chris@0
|
99 }
|
Chris@0
|
100 catch (RequirementsException $e) {
|
Chris@0
|
101 // If checkRequirements() failed then the field module did not exist and
|
Chris@0
|
102 // we do not have any fields. Therefore, $fields will be empty and below
|
Chris@0
|
103 // we'll create a migration just for the node properties.
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 $vocabulary_source_plugin = static::getSourcePlugin('d7_taxonomy_vocabulary');
|
Chris@0
|
107 try {
|
Chris@0
|
108 $vocabulary_source_plugin->checkRequirements();
|
Chris@0
|
109 }
|
Chris@0
|
110 catch (RequirementsException $e) {
|
Chris@0
|
111 // If the d7_taxonomy_vocabulary requirements failed, that means we do not
|
Chris@0
|
112 // have a Drupal source database configured - there is nothing to
|
Chris@0
|
113 // generate.
|
Chris@0
|
114 return $this->derivatives;
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 try {
|
Chris@0
|
118 foreach ($vocabulary_source_plugin as $row) {
|
Chris@0
|
119 $bundle = $row->getSourceProperty('machine_name');
|
Chris@0
|
120 $values = $base_plugin_definition;
|
Chris@0
|
121
|
Chris@0
|
122 $values['label'] = t('@label (@type)', [
|
Chris@0
|
123 '@label' => $values['label'],
|
Chris@0
|
124 '@type' => $row->getSourceProperty('name'),
|
Chris@0
|
125 ]);
|
Chris@0
|
126 $values['source']['bundle'] = $bundle;
|
Chris@0
|
127 $values['destination']['default_bundle'] = $bundle;
|
Chris@0
|
128
|
Chris@0
|
129 /** @var Migration $migration */
|
Chris@0
|
130 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($values);
|
Chris@0
|
131 if (isset($fields[$bundle])) {
|
Chris@0
|
132 foreach ($fields[$bundle] as $field_name => $info) {
|
Chris@0
|
133 $field_type = $info['type'];
|
Chris@0
|
134 try {
|
Chris@0
|
135 $plugin_id = $this->fieldPluginManager->getPluginIdFromFieldType($field_type, ['core' => 7], $migration);
|
Chris@0
|
136 if (!isset($this->fieldPluginCache[$field_type])) {
|
Chris@0
|
137 $this->fieldPluginCache[$field_type] = $this->fieldPluginManager->createInstance($plugin_id, ['core' => 7], $migration);
|
Chris@0
|
138 }
|
Chris@0
|
139 $this->fieldPluginCache[$field_type]
|
Chris@0
|
140 ->processFieldValues($migration, $field_name, $info);
|
Chris@0
|
141 }
|
Chris@0
|
142 catch (PluginNotFoundException $ex) {
|
Chris@0
|
143 try {
|
Chris@0
|
144 $plugin_id = $this->cckPluginManager->getPluginIdFromFieldType($field_type, ['core' => 7], $migration);
|
Chris@0
|
145 if (!isset($this->cckPluginCache[$field_type])) {
|
Chris@0
|
146 $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($plugin_id, ['core' => 7], $migration);
|
Chris@0
|
147 }
|
Chris@0
|
148 $this->cckPluginCache[$field_type]
|
Chris@0
|
149 ->processCckFieldValues($migration, $field_name, $info);
|
Chris@0
|
150 }
|
Chris@0
|
151 catch (PluginNotFoundException $ex) {
|
Chris@0
|
152 $migration->setProcessOfProperty($field_name, $field_name);
|
Chris@0
|
153 }
|
Chris@0
|
154 }
|
Chris@0
|
155 }
|
Chris@0
|
156 }
|
Chris@0
|
157 $this->derivatives[$bundle] = $migration->getPluginDefinition();
|
Chris@0
|
158 }
|
Chris@0
|
159 }
|
Chris@0
|
160 catch (DatabaseExceptionWrapper $e) {
|
Chris@0
|
161 // Once we begin iterating the source plugin it is possible that the
|
Chris@0
|
162 // source tables will not exist. This can happen when the
|
Chris@0
|
163 // MigrationPluginManager gathers up the migration definitions but we do
|
Chris@0
|
164 // not actually have a Drupal 7 source database.
|
Chris@0
|
165 }
|
Chris@0
|
166
|
Chris@0
|
167 return $this->derivatives;
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 }
|