diff --git a/modules/migrate_tamper/migrate_tamper.info.yml b/modules/migrate_tamper/migrate_tamper.info.yml new file mode 100644 index 0000000..6ef069e --- /dev/null +++ b/modules/migrate_tamper/migrate_tamper.info.yml @@ -0,0 +1,9 @@ +name: Migrate Tamper +description: Provides Tamper plugins as Migrate process plugins. +package: 'Migration' +type: module +core: 8.x + +dependencies: + - drupal:migrate + - tamper:tamper diff --git a/modules/migrate_tamper/src/Adapter/TamperableMigrateRowAdapter.php b/modules/migrate_tamper/src/Adapter/TamperableMigrateRowAdapter.php new file mode 100644 index 0000000..15acc54 --- /dev/null +++ b/modules/migrate_tamper/src/Adapter/TamperableMigrateRowAdapter.php @@ -0,0 +1,51 @@ +row = $row; + } + + /** + * {@inheritdoc} + */ + public function getSource() { + return $this->row->getSource(); + } + + /** + * {@inheritdoc} + */ + public function setSourceProperty($property, $data) { + $this->row->setSourceProperty($property, $data); + } + + /** + * {@inheritdoc} + */ + public function getSourceProperty($property) { + $this->row->getSourceProperty($property); + } + +} diff --git a/modules/migrate_tamper/src/Plugin/Derivative/TamperProcessPluginDeriver.php b/modules/migrate_tamper/src/Plugin/Derivative/TamperProcessPluginDeriver.php new file mode 100644 index 0000000..da84663 --- /dev/null +++ b/modules/migrate_tamper/src/Plugin/Derivative/TamperProcessPluginDeriver.php @@ -0,0 +1,56 @@ +tamperManager = $tamper_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, $base_plugin_id) { + return new static( + $container->get('plugin.manager.tamper') + ); + } + + /** + * {@inheritdoc} + */ + public function getDerivativeDefinitions($base_plugin_definition) { + foreach ($this->tamperManager->getDefinitions() as $tamper_id => $tamper_definition) { + $this->derivatives[$tamper_id] = $base_plugin_definition + $tamper_definition; + $this->derivatives[$tamper_id]['handle_multiples'] = $tamper_definition['handle_multiples']; + $this->derivatives[$tamper_id]['provider'] = $tamper_definition['provider']; + $this->derivatives[$tamper_id]['tamper_plugin_id'] = $tamper_id; + } + return $this->derivatives; + } + +} diff --git a/modules/migrate_tamper/src/Plugin/migrate/process/Tamper.php b/modules/migrate_tamper/src/Plugin/migrate/process/Tamper.php new file mode 100644 index 0000000..815cd3a --- /dev/null +++ b/modules/migrate_tamper/src/Plugin/migrate/process/Tamper.php @@ -0,0 +1,136 @@ +tamperManager = $tamper_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('plugin.manager.tamper') + ); + } + + /** + * {@inheritdoc} + */ + public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { + // Instantiate tamper plugin. + $tamper = $this->createTamperInstance($migrate_executable); + + // Create tamperable item. + $tamperable_item = new TamperableMigrateRowAdapter($row); + + // And apply tamper! + try { + $value = $tamper->tamper($value, $tamperable_item); + $this->multiple = $tamper->multiple(); + return $value; + } + catch (SkipTamperDataException $e) { + throw new MigrateSkipProcessException(); + } + catch (SkipTamperItemException $e) { + throw new MigrateSkipRowException(); + } + } + + /** + * {@inheritdoc} + */ + public function multiple() { + return $this->multiple; + } + + /** + * Creates a tamper instance. + * + * @param \Drupal\migrate\MigrateExecutableInterface + * The migrate executable. + * + * @return \Drupal\tamper\TamperInterface + * A tamper instance. + */ + protected function createTamperInstance(MigrateExecutableInterface $migrate_executable) { + return $this->tamperManager->createInstance($this->pluginDefinition['tamper_plugin_id'], $this->configuration + [ + 'source_definition' => $this->getSourceDefinitionFromMigrateExecutable($migrate_executable), + ]); + } + + /** + * Creates a source definition based on the migrate executable. + * + * @param \Drupal\migrate\MigrateExecutableInterface + * The migrate executable. + * + * @return \Drupal\tamper\SourceDefinition + * A source definition. + */ + protected function getSourceDefinitionFromMigrateExecutable(MigrateExecutableInterface $migrate_executable) { + // We need to use reflection since getSource() is protected. + $class = new ReflectionClass(get_class($migrate_executable)); + $method = $class->getMethod('getSource'); + $method->setAccessible(TRUE); + + return new SourceDefinition($method->invoke($migrate_executable)->fields()); + } + +}