Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / MachineName.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4
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;
12
13 /**
14  * Creates a machine name.
15  *
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.
20  *
21  * Letters will have language decorations and accents removed.
22  *
23  * Example:
24  *
25  * @code
26  * process:
27  *   bar:
28  *     plugin: machine_name
29  *     source: foo
30  * @endcode
31  *
32  * If the value of foo in the source is 'áéí!' then the destination value of bar
33  * will be 'aei_'.
34  *
35  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
36  *
37  * @MigrateProcessPlugin(
38  *   id = "machine_name"
39  * )
40  */
41 class MachineName extends ProcessPluginBase implements ContainerFactoryPluginInterface {
42
43   /**
44    * The transliteration service.
45    *
46    * @var \Drupal\Component\Transliteration\TransliterationInterface
47    */
48   protected $transliteration;
49
50   /**
51    * Constructs a MachineName plugin.
52    *
53    * @param array $configuration
54    *   The plugin configuration.
55    * @param string $plugin_id
56    *   The plugin ID.
57    * @param mixed $plugin_definition
58    *   The plugin definition.
59    * @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
60    *   The transliteration service.
61    */
62   public function __construct(array $configuration, $plugin_id, $plugin_definition, TransliterationInterface $transliteration) {
63     parent::__construct($configuration, $plugin_id, $plugin_definition);
64     $this->transliteration = $transliteration;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
71     return new static(
72       $configuration,
73       $plugin_id,
74       $plugin_definition,
75       $container->get('transliteration')
76     );
77   }
78
79   /**
80    * {@inheritdoc}
81    */
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);
87   }
88
89 }