3ca8f26a8bbe88588bff99a8e00ae69d09cd6c3c
[yaffs-website] / web / core / modules / migrate_drupal / src / Plugin / migrate / FieldMigration.php
1 <?php
2
3 namespace Drupal\migrate_drupal\Plugin\migrate;
4
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\migrate\Exception\RequirementsException;
8 use Drupal\migrate\Plugin\MigrateDestinationPluginManager;
9 use Drupal\migrate\Plugin\MigratePluginManager;
10 use Drupal\migrate\Plugin\Migration;
11 use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
12 use Drupal\migrate\Plugin\RequirementsInterface;
13 use Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface;
14 use Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16
17 /**
18  * Migration plugin class for migrations dealing with field config and values.
19  */
20 class FieldMigration extends Migration implements ContainerFactoryPluginInterface {
21
22   /**
23    * Defines which configuration option has the migration processing function.
24    *
25    * Default method is 'field_plugin_method'. For backwards compatibility,
26    * this constant is overridden in the CckMigration class, in order to
27    * fallback to the old 'cck_plugin_method'.
28    *
29    * @const string
30    */
31   const PLUGIN_METHOD = 'field_plugin_method';
32
33   /**
34    * Flag indicating whether the field data has been filled already.
35    *
36    * @var bool
37    */
38   protected $init = FALSE;
39
40   /**
41    * List of field plugin IDs which have already run.
42    *
43    * @var string[]
44    */
45   protected $processedFieldTypes = [];
46
47   /**
48    * Already-instantiated field plugins, keyed by ID.
49    *
50    * @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface[]
51    */
52   protected $fieldPluginCache;
53
54   /**
55    * The field plugin manager.
56    *
57    * @var \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface
58    */
59   protected $fieldPluginManager;
60
61   /**
62    * Already-instantiated cckfield plugins, keyed by ID.
63    *
64    * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldInterface[]
65    */
66   protected $cckPluginCache;
67
68   /**
69    * The cckfield plugin manager.
70    *
71    * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface
72    */
73   protected $cckPluginManager;
74
75   /**
76    * Constructs a FieldMigration.
77    *
78    * @param array $configuration
79    *   Plugin configuration.
80    * @param string $plugin_id
81    *   The plugin ID.
82    * @param mixed $plugin_definition
83    *   The plugin definition.
84    * @param \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface $cck_manager
85    *   The cckfield plugin manager.
86    * @param \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface $field_manager
87    *   The field plugin manager.
88    * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_plugin_manager
89    *   The migration plugin manager.
90    * @param \Drupal\migrate\Plugin\MigratePluginManager $source_plugin_manager
91    *   The source migration plugin manager.
92    * @param \Drupal\migrate\Plugin\MigratePluginManager $process_plugin_manager
93    *   The process migration plugin manager.
94    * @param \Drupal\migrate\Plugin\MigrateDestinationPluginManager $destination_plugin_manager
95    *   The destination migration plugin manager.
96    * @param \Drupal\migrate\Plugin\MigratePluginManager $idmap_plugin_manager
97    *   The ID map migration plugin manager.
98    */
99   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrateCckFieldPluginManagerInterface $cck_manager, MigrateFieldPluginManagerInterface $field_manager, MigrationPluginManagerInterface $migration_plugin_manager, MigratePluginManager $source_plugin_manager, MigratePluginManager $process_plugin_manager, MigrateDestinationPluginManager $destination_plugin_manager, MigratePluginManager $idmap_plugin_manager) {
100     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration_plugin_manager, $source_plugin_manager, $process_plugin_manager, $destination_plugin_manager, $idmap_plugin_manager);
101     $this->cckPluginManager = $cck_manager;
102     $this->fieldPluginManager = $field_manager;
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
109     return new static(
110       $configuration,
111       $plugin_id,
112       $plugin_definition,
113       $container->get('plugin.manager.migrate.cckfield'),
114       $container->get('plugin.manager.migrate.field'),
115       $container->get('plugin.manager.migration'),
116       $container->get('plugin.manager.migrate.source'),
117       $container->get('plugin.manager.migrate.process'),
118       $container->get('plugin.manager.migrate.destination'),
119       $container->get('plugin.manager.migrate.id_map')
120     );
121   }
122
123   /**
124    * {@inheritdoc}
125    */
126   public function getProcess() {
127     if (!$this->init) {
128       $this->init = TRUE;
129       $source_plugin = $this->migrationPluginManager->createInstance($this->pluginId)->getSourcePlugin();
130       if ($source_plugin instanceof RequirementsInterface) {
131         try {
132           $source_plugin->checkRequirements();
133         }
134         catch (RequirementsException $e) {
135           // Kill the rest of the method.
136           $source_plugin = [];
137         }
138       }
139       foreach ($source_plugin as $row) {
140         $field_type = $row->getSourceProperty('type');
141
142         try {
143           $plugin_id = $this->fieldPluginManager->getPluginIdFromFieldType($field_type, [], $this);
144           $manager = $this->fieldPluginManager;
145         }
146         catch (PluginNotFoundException $ex) {
147           try {
148             $plugin_id = $this->cckPluginManager->getPluginIdFromFieldType($field_type, [], $this);
149             $manager = $this->cckPluginManager;
150           }
151           catch (PluginNotFoundException $ex) {
152             continue;
153           }
154         }
155
156         if (!isset($this->processedFieldTypes[$field_type]) && $manager->hasDefinition($plugin_id)) {
157           $this->processedFieldTypes[$field_type] = TRUE;
158           // Allow the field plugin to alter the migration as necessary so that
159           // it knows how to handle fields of this type.
160           if (!isset($this->fieldPluginCache[$field_type])) {
161             $this->fieldPluginCache[$field_type] = $manager->createInstance($plugin_id, [], $this);
162           }
163         }
164         $method = $this->pluginDefinition[static::PLUGIN_METHOD];
165         call_user_func([$this->fieldPluginCache[$field_type], $method], $this);
166       }
167     }
168     return parent::getProcess();
169   }
170
171 }