Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\migrate_drupal\Plugin;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
|
Chris@14
|
6 use Drupal\migrate\Plugin\Exception\BadPluginDefinitionException;
|
Chris@0
|
7 use Drupal\migrate\Plugin\MigratePluginManager;
|
Chris@0
|
8 use Drupal\migrate\Plugin\MigrationInterface;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Plugin manager for migrate field plugins.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @see \Drupal\migrate_drupal\Plugin\MigrateFieldInterface
|
Chris@0
|
14 * @see \Drupal\migrate\Annotation\MigrateField
|
Chris@0
|
15 * @see plugin_api
|
Chris@0
|
16 *
|
Chris@0
|
17 * @ingroup migration
|
Chris@0
|
18 */
|
Chris@0
|
19 class MigrateFieldPluginManager extends MigratePluginManager implements MigrateFieldPluginManagerInterface {
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * The default version of core to use for field plugins.
|
Chris@0
|
23 *
|
Chris@0
|
24 * These plugins were initially only built and used for Drupal 6 fields.
|
Chris@0
|
25 * Having been extended for Drupal 7 with a "core" annotation, we fall back to
|
Chris@0
|
26 * Drupal 6 where none exists.
|
Chris@0
|
27 */
|
Chris@0
|
28 const DEFAULT_CORE_VERSION = 6;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@18
|
31 * Get the plugin ID from the field type.
|
Chris@18
|
32 *
|
Chris@18
|
33 * This method determines which field plugin should be used for a given field
|
Chris@18
|
34 * type and Drupal core version, returning the lowest weighted plugin
|
Chris@18
|
35 * supporting the provided core version, and which matches the field type
|
Chris@18
|
36 * either by plugin ID, or in the type_map annotation keys.
|
Chris@18
|
37 *
|
Chris@18
|
38 * @param string $field_type
|
Chris@18
|
39 * The field type being migrated.
|
Chris@18
|
40 * @param array $configuration
|
Chris@18
|
41 * (optional) An array of configuration relevant to the plugin instance.
|
Chris@18
|
42 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
|
Chris@18
|
43 * (optional) The current migration instance.
|
Chris@18
|
44 *
|
Chris@18
|
45 * @return string
|
Chris@18
|
46 * The ID of the plugin for the field type if available.
|
Chris@18
|
47 *
|
Chris@18
|
48 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
|
Chris@18
|
49 * If the plugin cannot be determined, such as if the field type is invalid.
|
Chris@18
|
50 *
|
Chris@18
|
51 * @see \Drupal\migrate_drupal\Annotation\MigrateField
|
Chris@0
|
52 */
|
Chris@0
|
53 public function getPluginIdFromFieldType($field_type, array $configuration = [], MigrationInterface $migration = NULL) {
|
Chris@0
|
54 $core = static::DEFAULT_CORE_VERSION;
|
Chris@0
|
55 if (!empty($configuration['core'])) {
|
Chris@0
|
56 $core = $configuration['core'];
|
Chris@0
|
57 }
|
Chris@0
|
58 elseif (!empty($migration->getPluginDefinition()['migration_tags'])) {
|
Chris@0
|
59 foreach ($migration->getPluginDefinition()['migration_tags'] as $tag) {
|
Chris@0
|
60 if ($tag == 'Drupal 7') {
|
Chris@0
|
61 $core = 7;
|
Chris@0
|
62 }
|
Chris@0
|
63 }
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 $definitions = $this->getDefinitions();
|
Chris@0
|
67 foreach ($definitions as $plugin_id => $definition) {
|
Chris@0
|
68 if (in_array($core, $definition['core'])) {
|
Chris@0
|
69 if (array_key_exists($field_type, $definition['type_map']) || $field_type === $plugin_id) {
|
Chris@0
|
70 return $plugin_id;
|
Chris@0
|
71 }
|
Chris@0
|
72 }
|
Chris@0
|
73 }
|
Chris@0
|
74 throw new PluginNotFoundException($field_type);
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@14
|
77 /**
|
Chris@14
|
78 * {@inheritdoc}
|
Chris@14
|
79 */
|
Chris@14
|
80 public function processDefinition(&$definition, $plugin_id) {
|
Chris@14
|
81 parent::processDefinition($definition, $plugin_id);
|
Chris@14
|
82
|
Chris@14
|
83 foreach (['core', 'source_module', 'destination_module'] as $required_property) {
|
Chris@14
|
84 if (empty($definition[$required_property])) {
|
Chris@14
|
85 throw new BadPluginDefinitionException($plugin_id, $required_property);
|
Chris@14
|
86 }
|
Chris@14
|
87 }
|
Chris@14
|
88 }
|
Chris@14
|
89
|
Chris@18
|
90 /**
|
Chris@18
|
91 * {@inheritdoc}
|
Chris@18
|
92 */
|
Chris@18
|
93 protected function findDefinitions() {
|
Chris@18
|
94 $definitions = parent::findDefinitions();
|
Chris@18
|
95 $this->sortDefinitions($definitions);
|
Chris@18
|
96 return $definitions;
|
Chris@18
|
97 }
|
Chris@18
|
98
|
Chris@18
|
99 /**
|
Chris@18
|
100 * Sorts a definitions array.
|
Chris@18
|
101 *
|
Chris@18
|
102 * This sorts the definitions array first by the weight column, and then by
|
Chris@18
|
103 * the plugin ID, ensuring a stable, deterministic, and testable ordering of
|
Chris@18
|
104 * plugins.
|
Chris@18
|
105 *
|
Chris@18
|
106 * @param array $definitions
|
Chris@18
|
107 * The definitions array to sort.
|
Chris@18
|
108 */
|
Chris@18
|
109 protected function sortDefinitions(array &$definitions) {
|
Chris@18
|
110 array_multisort(array_column($definitions, 'weight'), SORT_ASC, SORT_NUMERIC, array_keys($definitions), SORT_ASC, SORT_NATURAL, $definitions);
|
Chris@18
|
111 }
|
Chris@18
|
112
|
Chris@0
|
113 }
|