Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions modules/migrate_tamper/migrate_tamper.info.yml
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions modules/migrate_tamper/src/Adapter/TamperableMigrateRowAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Drupal\migrate_tamper\Adapter;

use Drupal\migrate\Row;
use Drupal\tamper\TamperableItemInterface;

/**
* Provides an adapter to use the migrate row as a tamperable item.
*/
class TamperableMigrateRowAdapter implements TamperableItemInterface {

/**
* A migrate row.
*
* @var \Drupal\migrate\Row
*/
protected $row;

/**
* Creates a new instance of the adapter.
*
* @param \Drupal\migrate\Row $row
* A migrate row.
*/
public function __construct(Row $row) {
$this->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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Drupal\migrate_tamper\Plugin\Derivative;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\tamper\TamperManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides Tamper plugins as Migrate process plugins.
*
* @see \Drupal\migrate_tamper\Plugin\migrate\process\Tamper
*/
class TamperProcessPluginDeriver extends DeriverBase implements ContainerDeriverInterface {

/**
* The tamper plugin manager.
*
* @var \Drupal\tamper\TamperManagerInterface
*/
protected $tamperManager;

/**
* Constructs new TamperProcessPluginDeriver.
*
* @param \Drupal\tamper\TamperManagerInterface $tamper_manager
* The tamper plugin manager.
*/
public function __construct(TamperManagerInterface $tamper_manager) {
$this->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;
}

}
136 changes: 136 additions & 0 deletions modules/migrate_tamper/src/Plugin/migrate/process/Tamper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace Drupal\migrate_tamper\Plugin\migrate\process;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Drupal\migrate_tamper\Adapter\TamperableMigrateRowAdapter;
use Drupal\tamper\Exception\SkipTamperDataException;
use Drupal\tamper\Exception\SkipTamperItemException;
use Drupal\tamper\SourceDefinition;
use Drupal\tamper\TamperManagerInterface;
use ReflectionClass;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides tamper plugins as process plugins.
*
* @MigrateProcessPlugin(
* id = "tamper",
* deriver = "Drupal\migrate_tamper\Plugin\Derivative\TamperProcessPluginDeriver"
* )
*/
class Tamper extends ProcessPluginBase implements ContainerFactoryPluginInterface {

/**
* The tamper plugin manager.
*
* @var \Drupal\tamper\TamperManagerInterface
*/
protected $tamperManager;

/**
* Flag indicating whether there are multiple values.
*
* @var bool
*/
protected $multiple = FALSE;

/**
* Constructs a new Tamper object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\tamper\TamperManagerInterface $tamper
* The tamper plugin manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, TamperManagerInterface $tamper_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->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());
}

}