Same filename and directory in other branches
- 8.9.x core/modules/field/src/Plugin/migrate/process/ProcessField.php
- 9 core/modules/field/src/Plugin/migrate/process/ProcessField.php
Namespace
Drupal\field\Plugin\migrate\process
File
core/modules/field/src/Plugin/migrate/process/ProcessField.php
View source
<?php
namespace Drupal\field\Plugin\migrate\process;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Attribute\MigrateProcess;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ProcessField extends ProcessPluginBase implements ContainerFactoryPluginInterface {
protected $fieldPluginManager;
protected $migration;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrateFieldPluginManagerInterface $field_plugin_manager, MigrationInterface $migration = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->fieldPluginManager = $field_plugin_manager;
$this->migration = $migration;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.migrate.field'), $migration);
}
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!is_string($value)) {
throw new MigrateException('The input value must be a string.');
}
if (empty($this->configuration['method'])) {
throw new MigrateException('You need to specify the name of a method to be called on the Field plugin.');
}
$method = $this->configuration['method'];
try {
return $this
->callMethodOnFieldPlugin($this->fieldPluginManager, $value, $method, $row);
} catch (PluginNotFoundException $e) {
return NULL;
}
}
protected function callMethodOnFieldPlugin(MigrateFieldPluginManagerInterface $field_plugin_manager, $field_type, $method, Row $row) {
$plugin_id = $field_plugin_manager
->getPluginIdFromFieldType($field_type, [], $this->migration);
$plugin_instance = $field_plugin_manager
->createInstance($plugin_id, [], $this->migration);
if (!is_callable([
$plugin_instance,
$method,
])) {
throw new MigrateException('The specified method does not exist or is not callable.');
}
return call_user_func_array([
$plugin_instance,
$method,
], [
$row,
]);
}
}
Classes