Skip to content
Snippets Groups Projects

Issue #3214186: Scan for snippets of process plugins

2 files
+ 136
1
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 135
0
<?php
namespace Drupal\migrate_scanner\Plugin\migrate\process;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\MigratePluginManagerInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Yaml\Yaml;
/**
* Applies process plugins described in YAML snippets.
*
* Available configuration keys:
* - module: the module providing the snippet
* - path: path of the YAML file, relative to module/migrations/process and
* without the .yml extension.
*
* Example:
*
* Suppose the file mymodule/migrations/process/clean_whitespace.yml contains
* the following:
*
* @code
* -
* plugin: callback
* callable: htmlentities
* -
* plugin: str_replace
* search:
* - '&#160;'
* - '&nbsp;'
* replace: ' '
* -
* plugin: str_replace
* regex: true
* search: '@\s+@'
* replace: ' '
* -
* plugin: callback
* callable: trim
* @endcode
*
* Then that process pipeline can be used as follows:
*
* @code
* process:
* field_formatted_text:
* plugin: snippet
* module: mymodule
* path: clean_whitespace
* source: html_string
* @endcode
*
* @MigrateProcessPlugin(
* id = "snippet"
* )
*/
class Snippet extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The process pipeline.
*
* @var Drupal\migrate\Plugin\MigrateProcessInterface[]
*/
protected $pipeline = [];
/**
* Constructs a download process plugin.
*
* @param array $configuration
* The plugin configuration.
* @param string $plugin_id
* The plugin ID.
* @param array $plugin_definition
* The plugin definition.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\migrate\Plugin\MigratePluginManagerInterface $plugin_manager
* The process plugin manager.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ModuleHandlerInterface $module_handler, MigratePluginManagerInterface $plugin_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->moduleHandler = $module_handler;
// @todo Add error checking: the module is enabled, the file exists, the
// YAML is valid.
$snippet_file = implode('/', [
$this->moduleHandler->getModule($this->configuration['module'])->getPath(),
'migrations/process',
$this->configuration['path'],
]) . '.yml';
$steps = Yaml::parse(file_get_contents($snippet_file));
foreach ($steps as $step_configuration) {
$step_id = $step_configuration['plugin'];
unset($step_configuration['plugin']);
$this->pipeline[] = $plugin_manager->createInstance($step_id, $step_configuration);
}
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('module_handler'),
$container->get('plugin.manager.migrate.process')
);
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
foreach ($this->pipeline as $step) {
$value = $step->transform($value, $migrate_executable, $row, $destination_property);
}
return $value;
}
}
Loading