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\Core\Database\DatabaseExceptionWrapper;
|
Chris@0
|
7 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
|
Chris@0
|
8 use Drupal\migrate\Exception\RequirementsException;
|
Chris@0
|
9 use Drupal\migrate\Plugin\MigrationDeriverTrait;
|
Chris@18
|
10 use Drupal\migrate_drupal\FieldDiscoveryInterface;
|
Chris@0
|
11 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Deriver for Drupal 7 taxonomy term migrations based on vocabularies.
|
Chris@0
|
15 */
|
Chris@0
|
16 class D7TaxonomyTermDeriver extends DeriverBase implements ContainerDeriverInterface {
|
Chris@0
|
17 use MigrationDeriverTrait;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * The base plugin ID this derivative is for.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var string
|
Chris@0
|
23 */
|
Chris@0
|
24 protected $basePluginId;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@18
|
27 * The migration field discovery service.
|
Chris@0
|
28 *
|
Chris@18
|
29 * @var \Drupal\migrate_drupal\FieldDiscoveryInterface
|
Chris@0
|
30 */
|
Chris@18
|
31 protected $fieldDiscovery;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * D7TaxonomyTermDeriver constructor.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @param string $base_plugin_id
|
Chris@0
|
37 * The base plugin ID for the plugin ID.
|
Chris@18
|
38 * @param \Drupal\migrate_drupal\FieldDiscoveryInterface $field_discovery
|
Chris@18
|
39 * The migration field discovery service.
|
Chris@0
|
40 */
|
Chris@18
|
41 public function __construct($base_plugin_id, FieldDiscoveryInterface $field_discovery) {
|
Chris@0
|
42 $this->basePluginId = $base_plugin_id;
|
Chris@18
|
43 $this->fieldDiscovery = $field_discovery;
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * {@inheritdoc}
|
Chris@0
|
48 */
|
Chris@0
|
49 public static function create(ContainerInterface $container, $base_plugin_id) {
|
Chris@0
|
50 return new static(
|
Chris@0
|
51 $base_plugin_id,
|
Chris@18
|
52 $container->get('migrate_drupal.field_discovery')
|
Chris@0
|
53 );
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * {@inheritdoc}
|
Chris@0
|
58 */
|
Chris@0
|
59 public function getDerivativeDefinitions($base_plugin_definition) {
|
Chris@0
|
60
|
Chris@0
|
61 $vocabulary_source_plugin = static::getSourcePlugin('d7_taxonomy_vocabulary');
|
Chris@0
|
62 try {
|
Chris@0
|
63 $vocabulary_source_plugin->checkRequirements();
|
Chris@0
|
64 }
|
Chris@0
|
65 catch (RequirementsException $e) {
|
Chris@0
|
66 // If the d7_taxonomy_vocabulary requirements failed, that means we do not
|
Chris@0
|
67 // have a Drupal source database configured - there is nothing to
|
Chris@0
|
68 // generate.
|
Chris@0
|
69 return $this->derivatives;
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 try {
|
Chris@0
|
73 foreach ($vocabulary_source_plugin as $row) {
|
Chris@0
|
74 $bundle = $row->getSourceProperty('machine_name');
|
Chris@0
|
75 $values = $base_plugin_definition;
|
Chris@0
|
76
|
Chris@0
|
77 $values['label'] = t('@label (@type)', [
|
Chris@0
|
78 '@label' => $values['label'],
|
Chris@0
|
79 '@type' => $row->getSourceProperty('name'),
|
Chris@0
|
80 ]);
|
Chris@0
|
81 $values['source']['bundle'] = $bundle;
|
Chris@0
|
82 $values['destination']['default_bundle'] = $bundle;
|
Chris@0
|
83
|
Chris@18
|
84 /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
|
Chris@0
|
85 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($values);
|
Chris@18
|
86 $this->fieldDiscovery->addBundleFieldProcesses($migration, 'taxonomy_term', $bundle);
|
Chris@0
|
87 $this->derivatives[$bundle] = $migration->getPluginDefinition();
|
Chris@0
|
88 }
|
Chris@0
|
89 }
|
Chris@0
|
90 catch (DatabaseExceptionWrapper $e) {
|
Chris@0
|
91 // Once we begin iterating the source plugin it is possible that the
|
Chris@0
|
92 // source tables will not exist. This can happen when the
|
Chris@0
|
93 // MigrationPluginManager gathers up the migration definitions but we do
|
Chris@0
|
94 // not actually have a Drupal 7 source database.
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 return $this->derivatives;
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 }
|