Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / field / src / Plugin / migrate / process / ProcessField.php
1 <?php
2
3 namespace Drupal\field\Plugin\migrate\process;
4
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\migrate\MigrateException;
8 use Drupal\migrate\MigrateExecutableInterface;
9 use Drupal\migrate\Plugin\MigrationInterface;
10 use Drupal\migrate\ProcessPluginBase;
11 use Drupal\migrate\Row;
12 use Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface;
13 use Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15
16 /**
17  * Get the value from a method call on a field plugin instance.
18  *
19  * This process plugin will instantiate a field plugin based on the given
20  * field type and then call the given method on it for the return value.
21  *
22  * Available configuration keys:
23  * - source: The source field type to use to instantiate a field plugin.
24  * - method: The method to be called on the field plugin instance.
25  *
26  * If no field plugin for the given field type is found, NULL will be returned.
27  *
28  * Example:
29  *
30  * @code
31  * process:
32  *   type:
33  *     plugin: process_field
34  *     source: type
35  *     method: getFieldType
36  * @endcode
37  *
38  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
39  * @see \Drupal\migrate_drupal\Plugin\MigrateFieldInterface;
40  *
41  * @MigrateProcessPlugin(
42  *   id = "process_field"
43  * )
44  */
45 class ProcessField extends ProcessPluginBase implements ContainerFactoryPluginInterface {
46
47   /**
48    * The cckfield plugin manager.
49    *
50    * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface
51    */
52   protected $cckPluginManager;
53
54   /**
55    * The field plugin manager.
56    *
57    * @var \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface
58    */
59   protected $fieldPluginManager;
60
61   /**
62    * The migration being run.
63    *
64    * @var \Drupal\migrate\Plugin\MigrationInterface
65    */
66   protected $migration;
67
68   /**
69    * Constructs a ProcessField plugin.
70    *
71    * @param array $configuration
72    *   The plugin configuration.
73    * @param string $plugin_id
74    *   The plugin ID.
75    * @param mixed $plugin_definition
76    *   The plugin definition.
77    * @param \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface $cck_plugin_manager
78    *   The cckfield plugin manager.
79    * @param \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface $field_plugin_manager
80    *   The field plugin manager.
81    * @param \Drupal\migrate\Plugin\MigrationInterface $migration
82    *   The migration being run.
83    */
84   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrateCckFieldPluginManagerInterface $cck_plugin_manager, MigrateFieldPluginManagerInterface $field_plugin_manager, MigrationInterface $migration = NULL) {
85     parent::__construct($configuration, $plugin_id, $plugin_definition);
86     $this->cckPluginManager = $cck_plugin_manager;
87     $this->fieldPluginManager = $field_plugin_manager;
88     $this->migration = $migration;
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
95     return new static(
96       $configuration,
97       $plugin_id,
98       $plugin_definition,
99       $container->get('plugin.manager.migrate.cckfield'),
100       $container->get('plugin.manager.migrate.field'),
101       $migration
102     );
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
109     if (!is_string($value)) {
110       throw new MigrateException('The input value must be a string.');
111     }
112
113     if (empty($this->configuration['method'])) {
114       throw new MigrateException('You need to specify the name of a method to be called on the Field plugin.');
115     }
116     $method = $this->configuration['method'];
117
118     try {
119       return $this->callMethodOnFieldPlugin($this->fieldPluginManager, $value, $method, $row);
120     }
121     catch (PluginNotFoundException $e) {
122       try {
123         return $this->callMethodOnFieldPlugin($this->cckPluginManager, $value, $method, $row);
124       }
125       catch (PluginNotFoundException $e) {
126         return NULL;
127       }
128     }
129   }
130
131   /**
132    * Instantiate a field plugin and call a method on it.
133    *
134    * @param \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface $field_plugin_manager
135    *   The field plugin manager.
136    * @param string $field_type
137    *   The field type for which to get the field plugin.
138    * @param string $method
139    *   The method to call on the field plugin.
140    * @param \Drupal\migrate\Row $row
141    *   The row from the source to process.
142    *
143    * @return mixed
144    *   The return value from the method called on the field plugin.
145    */
146   protected function callMethodOnFieldPlugin(MigrateFieldPluginManagerInterface $field_plugin_manager, $field_type, $method, Row $row) {
147     $plugin_id = $field_plugin_manager->getPluginIdFromFieldType($field_type, [], $this->migration);
148     $plugin_instance = $field_plugin_manager->createInstance($plugin_id, [], $this->migration);
149     if (!is_callable([$plugin_instance, $method])) {
150       throw new MigrateException('The specified method does not exists or is not callable.');
151     }
152     return call_user_func_array([$plugin_instance, $method], [$row]);
153   }
154
155 }