You are here

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

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\process
View 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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DomMigrationLookup::$migration protected property The migration to be executed.
DomMigrationLookup::$processPluginManager protected property The process plugin manager.
DomMigrationLookup::$transformParameters protected property Parameters passed to transform method, except the first, value.
DomMigrationLookup::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
DomMigrationLookup::doReplace protected function Retrieves the right replace string based on configuration. Overrides DomStrReplace::doReplace
DomMigrationLookup::migrationLookup protected function Lookup the migration mapped ID on one migration.
DomMigrationLookup::transform public function Performs the associated process. Overrides DomStrReplace::transform
DomMigrationLookup::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides DomStrReplace::__construct
DomProcessBase::$document protected property Document to use.
DomProcessBase::$xpath protected property Xpath query object.
DomProcessBase::init protected function Initialize the class properties.
DomStrReplace::getReplace protected function Retrieves the right replace string based on configuration.
DomStrReplace::getSearch protected function Retrieves the right search string based on configuration.
DomStrReplace::getSubject protected function Retrieves the right subject string.
DomStrReplace::postReplace protected function Performs post-replace actions.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.