3 namespace Drupal\field\Plugin\migrate\process;
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;
17 * Get the value from a method call on a field plugin instance.
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.
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.
26 * If no field plugin for the given field type is found, NULL will be returned.
33 * plugin: process_field
35 * method: getFieldType
38 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
39 * @see \Drupal\migrate_drupal\Plugin\MigrateFieldInterface;
41 * @MigrateProcessPlugin(
42 * id = "process_field"
45 class ProcessField extends ProcessPluginBase implements ContainerFactoryPluginInterface {
48 * The cckfield plugin manager.
50 * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface
52 protected $cckPluginManager;
55 * The field plugin manager.
57 * @var \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface
59 protected $fieldPluginManager;
62 * The migration being run.
64 * @var \Drupal\migrate\Plugin\MigrationInterface
69 * Constructs a ProcessField plugin.
71 * @param array $configuration
72 * The plugin configuration.
73 * @param string $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.
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;
94 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
99 $container->get('plugin.manager.migrate.cckfield'),
100 $container->get('plugin.manager.migrate.field'),
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.');
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.');
116 $method = $this->configuration['method'];
119 return $this->callMethodOnFieldPlugin($this->fieldPluginManager, $value, $method, $row);
121 catch (PluginNotFoundException $e) {
123 return $this->callMethodOnFieldPlugin($this->cckPluginManager, $value, $method, $row);
125 catch (PluginNotFoundException $e) {
132 * Instantiate a field plugin and call a method on it.
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.
144 * The return value from the method called on the field plugin.
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.');
152 return call_user_func_array([$plugin_instance, $method], [$row]);