class DomMigrationLookup in Migrate Plus 8.5
String replacements on a source dom based on migration lookup.
Meant to be used after dom process plugin.
Available configuration keys:
- mode: What to modify. Possible values:
- attribute: One element attribute.
- xpath: XPath query expression that will produce the \DOMNodeList to walk.
- attribute_options: A map of options related to the attribute mode. Required
when mode is attribute. The keys can be:
- name: Name of the attribute to match and modify.
- search: Regular expression to use. It should contain at least one parenthesized subpattern which will be used as the ID passed to migration_lookup process plugin.
- replace: Default value to use for replacements on migrations, if not specified on the migration. It should contain the '[mapped-id]' string where the looked-up migration value will be placed.
- migrations: A map of options indexed by migration machine name. Possible
option values are:
- replace: See replace option lines above.
- no_stub: If TRUE, then do not create stub entities during migration lookup. Optional, defaults to TRUE.
Example:
process:
'body/value':
-
plugin: dom
method: import
source: 'body/0/value'
-
plugin: dom_migration_lookup
mode: attribute
xpath: '//a'
attribute_options:
name: href
search: '@/user/(\d+)@'
replace: '/user/[mapped-id]'
migrations:
users:
replace: '/user/[mapped-id]'
people:
replace: '/people/[mapped-id]'
-
plugin: dom
method: export
Plugin annotation
@MigrateProcessPlugin(
id = "dom_migration_lookup"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\migrate_plus\Plugin\migrate\process\DomProcessBase
- class \Drupal\migrate_plus\Plugin\migrate\process\DomStrReplace
- class \Drupal\migrate_plus\Plugin\migrate\process\DomMigrationLookup implements ContainerFactoryPluginInterface
- class \Drupal\migrate_plus\Plugin\migrate\process\DomStrReplace
- class \Drupal\migrate_plus\Plugin\migrate\process\DomProcessBase
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of DomMigrationLookup
1 file declares its use of DomMigrationLookup
- DomMigrationLookupTest.php in tests/
src/ Unit/ process/ DomMigrationLookupTest.php
File
- src/
Plugin/ migrate/ process/ DomMigrationLookup.php, line 68
Namespace
Drupal\migrate_plus\Plugin\migrate\processView source
class DomMigrationLookup extends DomStrReplace implements ContainerFactoryPluginInterface {
/**
* The migration to be executed.
*
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migration;
/**
* The process plugin manager.
*
* @var \Drupal\migrate\Plugin\MigratePluginManagerInterface
*/
protected $processPluginManager;
/**
* Parameters passed to transform method, except the first, value.
*
* This helps to pass values to another process plugin.
*
* @var array
*/
protected $transformParameters;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, MigratePluginManagerInterface $process_plugin_manager) {
$configuration += [
'no_stub' => TRUE,
];
$default_replace_missing = empty($configuration['replace']);
if ($default_replace_missing) {
$configuration['replace'] = 'prevent-requirement-fail';
}
parent::__construct($configuration, $plugin_id, $plugin_definition);
if ($default_replace_missing) {
unset($this->configuration['replace']);
}
$this->migration = $migration;
$this->processPluginManager = $process_plugin_manager;
if (empty($this->configuration['migrations'])) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), "Configuration option 'migration' is required.");
}
if (!is_array($this->configuration['migrations'])) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), "Configuration option 'migration' should be a keyed array.");
}
// Add missing values if possible.
$default_replace = isset($this->configuration['replace']) ? $this->configuration['replace'] : NULL;
foreach ($this->configuration['migrations'] as $migration_name => $configuration_item) {
if (!empty($configuration_item['replace'])) {
continue;
}
if (is_null($default_replace)) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), "Please define either a global replace for all migrations, or a specific one for 'migrations.{$migration_name}'.");
}
$this->configuration['migrations'][$migration_name]['replace'] = $default_replace;
}
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('plugin.manager.migrate.process'));
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$this
->init($value, $destination_property);
$this->transformParameters = [
'migrate_executable' => $migrate_executable,
'row' => $row,
'destination_property' => $destination_property,
];
foreach ($this->xpath
->query($this->configuration['xpath']) as $html_node) {
$subject = $this
->getSubject($html_node);
if (empty($subject)) {
// Could not find subject, skip processing.
continue;
}
$search = $this
->getSearch();
if (!preg_match($search, $subject, $matches)) {
// No match found, skip processing.
continue;
}
$id = $matches[1];
// Walk through defined migrations looking for a map.
foreach ($this->configuration['migrations'] as $migration_name => $configuration) {
$mapped_id = $this
->migrationLookup($id, $migration_name);
if (!is_null($mapped_id)) {
// Not using getReplace(), since this implementation depends on the
// migration.
$replace = str_replace('[mapped-id]', $mapped_id, $configuration['replace']);
$this
->doReplace($html_node, $search, $replace, $subject);
break;
}
}
}
return $this->document;
}
/**
* {@inheritdoc}
*/
protected function doReplace(\DOMElement $html_node, $search, $replace, $subject) {
$new_subject = preg_replace($search, $replace, $subject);
$this
->postReplace($html_node, $new_subject);
}
/**
* Lookup the migration mapped ID on one migration.
*
* @param mixed $id
* The ID to search with migration_lookup process plugin.
* @param string $migration_name
* The migration to look into machine name.
*
* @return string|null
* The found mapped ID, or NULL if not found on the provided migration.
*/
protected function migrationLookup($id, $migration_name) {
$mapped_id = NULL;
$parameters = [
$id,
$this->transformParameters['migrate_executable'],
$this->transformParameters['row'],
$this->transformParameters['destination_property'],
];
$plugin_configuration = [
'migration' => $migration_name,
'no_stub' => $this->configuration['no_stub'],
];
$migration_lookup_plugin = $this->processPluginManager
->createInstance('migration_lookup', $plugin_configuration, $this->migration);
$mapped_id = call_user_func_array([
$migration_lookup_plugin,
'transform',
], $parameters);
return $mapped_id;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DomMigrationLookup:: |
protected | property | The migration to be executed. | |
DomMigrationLookup:: |
protected | property | The process plugin manager. | |
DomMigrationLookup:: |
protected | property | Parameters passed to transform method, except the first, value. | |
DomMigrationLookup:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
DomMigrationLookup:: |
protected | function |
Retrieves the right replace string based on configuration. Overrides DomStrReplace:: |
|
DomMigrationLookup:: |
protected | function | Lookup the migration mapped ID on one migration. | |
DomMigrationLookup:: |
public | function |
Performs the associated process. Overrides DomStrReplace:: |
|
DomMigrationLookup:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides DomStrReplace:: |
|
DomProcessBase:: |
protected | property | Document to use. | |
DomProcessBase:: |
protected | property | Xpath query object. | |
DomProcessBase:: |
protected | function | Initialize the class properties. | |
DomStrReplace:: |
protected | function | Retrieves the right replace string based on configuration. | |
DomStrReplace:: |
protected | function | Retrieves the right search string based on configuration. | |
DomStrReplace:: |
protected | function | Retrieves the right subject string. | |
DomStrReplace:: |
protected | function | Performs post-replace actions. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
ProcessPluginBase:: |
public | function |
Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface:: |
3 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |