3 namespace Drupal\migrate\Plugin\migrate\process;
5 use Drupal\Component\Transliteration\TransliterationInterface;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Drupal\migrate\ProcessPluginBase;
9 use Drupal\migrate\MigrateExecutableInterface;
10 use Drupal\migrate\Row;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Creates a machine name.
16 * The machine_name process plugin takes the source value and runs it through
17 * the transliteration service. This makes the source value lowercase,
18 * replaces anything that is not a number or a letter with an underscore,
19 * and removes duplicate underscores.
21 * Letters will have language decorations and accents removed.
28 * plugin: machine_name
32 * If the value of foo in the source is 'áéí!' then the destination value of bar
35 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
37 * @MigrateProcessPlugin(
41 class MachineName extends ProcessPluginBase implements ContainerFactoryPluginInterface {
44 * The transliteration service.
46 * @var \Drupal\Component\Transliteration\TransliterationInterface
48 protected $transliteration;
51 * Constructs a MachineName plugin.
53 * @param array $configuration
54 * The plugin configuration.
55 * @param string $plugin_id
57 * @param mixed $plugin_definition
58 * The plugin definition.
59 * @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
60 * The transliteration service.
62 public function __construct(array $configuration, $plugin_id, $plugin_definition, TransliterationInterface $transliteration) {
63 parent::__construct($configuration, $plugin_id, $plugin_definition);
64 $this->transliteration = $transliteration;
70 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
75 $container->get('transliteration')
82 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
83 $new_value = $this->transliteration->transliterate($value, LanguageInterface::LANGCODE_DEFAULT, '_');
84 $new_value = strtolower($new_value);
85 $new_value = preg_replace('/[^a-z0-9_]+/', '_', $new_value);
86 return preg_replace('/_+/', '_', $new_value);