Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / taxonomy / src / Plugin / migrate / D7TaxonomyTermDeriver.php
1 <?php
2
3 namespace Drupal\taxonomy\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 7 taxonomy term migrations based on vocabularies.
17  */
18 class D7TaxonomyTermDeriver extends DeriverBase implements ContainerDeriverInterface {
19
20   use MigrationDeriverTrait;
21
22   /**
23    * The base plugin ID this derivative is for.
24    *
25    * @var string
26    */
27   protected $basePluginId;
28
29   /**
30    * Already-instantiated cckfield plugins, keyed by ID.
31    *
32    * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldInterface[]
33    */
34   protected $cckPluginCache;
35
36   /**
37    * The CCK plugin manager.
38    *
39    * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface
40    */
41   protected $cckPluginManager;
42
43   /**
44    * Already-instantiated field plugins, keyed by ID.
45    *
46    * @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface[]
47    */
48   protected $fieldPluginCache;
49
50   /**
51    * The field plugin manager.
52    *
53    * @var \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface
54    */
55   protected $fieldPluginManager;
56
57   /**
58    * D7TaxonomyTermDeriver constructor.
59    *
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.
66    */
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;
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public static function create(ContainerInterface $container, $base_plugin_id) {
77     return new static(
78       $base_plugin_id,
79       $container->get('plugin.manager.migrate.cckfield'),
80       $container->get('plugin.manager.migrate.field')
81     );
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function getDerivativeDefinitions($base_plugin_definition) {
88     $fields = [];
89     try {
90       $source_plugin = static::getSourcePlugin('d7_field_instance');
91       $source_plugin->checkRequirements();
92
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();
97         }
98       }
99     }
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.
104     }
105
106     $vocabulary_source_plugin = static::getSourcePlugin('d7_taxonomy_vocabulary');
107     try {
108       $vocabulary_source_plugin->checkRequirements();
109     }
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
113       // generate.
114       return $this->derivatives;
115     }
116
117     try {
118       foreach ($vocabulary_source_plugin as $row) {
119         $bundle = $row->getSourceProperty('machine_name');
120         $values = $base_plugin_definition;
121
122         $values['label'] = t('@label (@type)', [
123           '@label' => $values['label'],
124           '@type' => $row->getSourceProperty('name'),
125         ]);
126         $values['source']['bundle'] = $bundle;
127         $values['destination']['default_bundle'] = $bundle;
128
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'];
134             try {
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);
138               }
139               $this->fieldPluginCache[$field_type]
140                 ->processFieldValues($migration, $field_name, $info);
141             }
142             catch (PluginNotFoundException $ex) {
143               try {
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);
147                 }
148                 $this->cckPluginCache[$field_type]
149                   ->processCckFieldValues($migration, $field_name, $info);
150               }
151               catch (PluginNotFoundException $ex) {
152                 $migration->setProcessOfProperty($field_name, $field_name);
153               }
154             }
155           }
156         }
157         $this->derivatives[$bundle] = $migration->getPluginDefinition();
158       }
159     }
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.
165     }
166
167     return $this->derivatives;
168   }
169
170 }