Commit f52cc182 authored by alexpott's avatar alexpott

Issue #2488126 by phenaproxima: MachineName process plugin should use injected...

Issue #2488126 by phenaproxima: MachineName process plugin should use injected transliteration service
parent 89267598
......@@ -7,10 +7,13 @@
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* This plugin creates a machine name.
......@@ -23,34 +26,50 @@
* id = "machine_name"
* )
*/
class MachineName extends ProcessPluginBase {
class MachineName extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* @var \Drupal\Core\Transliteration\PhpTransliteration
* @var \Drupal\Component\Transliteration\TransliterationInterface
*/
protected $transliteration;
/**
* Constructs a MachineName plugin.
*
* @param array $configuration
* The plugin configuration.
* @param string $plugin_id
* The plugin ID.
* @param mixed $plugin_definition
* The plugin definition.
* @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
* The transliteration service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, TransliterationInterface $transliteration) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->transliteration = $transliteration;
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$new_value = $this->getTransliteration()->transliterate($value, LanguageInterface::LANGCODE_DEFAULT, '_');
$new_value = strtolower($new_value);
$new_value = preg_replace('/[^a-z0-9_]+/', '_', $new_value);
return preg_replace('/_+/', '_', $new_value);
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('transliteration')
);
}
/**
* Get the transliteration object.
*
* @return \Drupal\Core\Transliteration\PhpTransliteration
* The transliteration object.
* {@inheritdoc}
*/
protected function getTransliteration() {
if (!isset($this->transliteration)) {
$this->transliteration = \Drupal::transliteration();
}
return $this->transliteration;
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$new_value = $this->transliteration->transliterate($value, LanguageInterface::LANGCODE_DEFAULT, '_');
$new_value = strtolower($new_value);
$new_value = preg_replace('/[^a-z0-9_]+/', '_', $new_value);
return preg_replace('/_+/', '_', $new_value);
}
}
......@@ -6,7 +6,7 @@
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\Plugin\migrate\process\TestMachineName;
use Drupal\migrate\Plugin\migrate\process\MachineName;
/**
* Tests the machine name process plugin.
......@@ -57,20 +57,9 @@ public function testMachineNames() {
->with($human_name)
->will($this->returnValue($human_name_ascii . 'aeo'));
$plugin = new TestMachineName(array(), 'machine_name', array());
$plugin->setTransliteration($this->transliteration);
$plugin = new MachineName(array(), 'machine_name', array(), $this->transliteration);
$value = $plugin->transform($human_name, $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertEquals($expected_result, $value);
}
}
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\Component\Transliteration\TransliterationInterface;
class TestMachineName extends MachineName {
public function setTransliteration(TransliterationInterface $transliteration) {
$this->transliteration = $transliteration;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment