Skip to content
Snippets Groups Projects
Commit f52cc182 authored by Alex Pott's avatar Alex Pott
Browse files

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

Issue #2488126 by phenaproxima: MachineName process plugin should use injected transliteration service
parent 89267598
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -7,10 +7,13 @@ ...@@ -7,10 +7,13 @@
namespace Drupal\migrate\Plugin\migrate\process; namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\ProcessPluginBase; use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface; use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row; use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* This plugin creates a machine name. * This plugin creates a machine name.
...@@ -23,34 +26,50 @@ ...@@ -23,34 +26,50 @@
* id = "machine_name" * 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; protected $transliteration;
/** /**
* {@inheritdoc} * 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 transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { public function __construct(array $configuration, $plugin_id, $plugin_definition, TransliterationInterface $transliteration) {
$new_value = $this->getTransliteration()->transliterate($value, LanguageInterface::LANGCODE_DEFAULT, '_'); parent::__construct($configuration, $plugin_id, $plugin_definition);
$new_value = strtolower($new_value); $this->transliteration = $transliteration;
$new_value = preg_replace('/[^a-z0-9_]+/', '_', $new_value);
return preg_replace('/_+/', '_', $new_value);
} }
/** /**
* Get the transliteration object. * {@inheritdoc}
*
* @return \Drupal\Core\Transliteration\PhpTransliteration
* The transliteration object.
*/ */
protected function getTransliteration() { public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
if (!isset($this->transliteration)) { return new static(
$this->transliteration = \Drupal::transliteration(); $configuration,
$plugin_id,
$plugin_definition,
$container->get('transliteration')
);
} }
return $this->transliteration;
/**
* {@inheritdoc}
*/
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 @@ ...@@ -6,7 +6,7 @@
namespace Drupal\Tests\migrate\Unit\process; 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. * Tests the machine name process plugin.
...@@ -57,20 +57,9 @@ public function testMachineNames() { ...@@ -57,20 +57,9 @@ public function testMachineNames() {
->with($human_name) ->with($human_name)
->will($this->returnValue($human_name_ascii . 'aeo')); ->will($this->returnValue($human_name_ascii . 'aeo'));
$plugin = new TestMachineName(array(), 'machine_name', array()); $plugin = new MachineName(array(), 'machine_name', array(), $this->transliteration);
$plugin->setTransliteration($this->transliteration);
$value = $plugin->transform($human_name, $this->migrateExecutable, $this->row, 'destinationproperty'); $value = $plugin->transform($human_name, $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertEquals($expected_result, $value); $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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment