3 namespace Drupal\migrate\Plugin\migrate\process;
5 use Drupal\migrate\ProcessPluginBase;
6 use Drupal\migrate\MigrateExecutableInterface;
7 use Drupal\migrate\Row;
8 use Drupal\migrate\MigrateException;
9 use Drupal\Component\Utility\Unicode;
12 * This plugin ensures the source value is unique.
14 * The MakeUniqueBase process plugin is used to avoid duplication at the
15 * destination. For example, when creating filter format names, the source
16 * value is checked against the existing filter format names and if it exists,
17 * a numeric postfix is added and incremented until a unique value is created.
18 * An optional postfix string can be insert before the numeric postfix.
20 * Available configuration keys
21 * - start: (optional) The position at which to start reading.
22 * - length: (optional) The number of characters to read.
23 * - postfix: (optional) A string to insert before the numeric postfix.
25 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
27 abstract class MakeUniqueBase extends ProcessPluginBase {
30 * Creates a unique value based on the source value.
32 * @param string $value
34 * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
35 * The migration in which this process is being executed.
36 * @param \Drupal\migrate\Row $row
37 * The row from the source to process.
38 * @param string $destination_property
39 * The destination property currently worked on. This is only used together
40 * with the $row above.
43 * The unique version of the input value.
45 * @throws \Drupal\migrate\MigrateException
47 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
49 $postfix = isset($this->configuration['postfix']) ? $this->configuration['postfix'] : '';
50 $start = isset($this->configuration['start']) ? $this->configuration['start'] : 0;
51 if (!is_int($start)) {
52 throw new MigrateException('The start position configuration key should be an integer. Omit this key to capture from the beginning of the string.');
54 $length = isset($this->configuration['length']) ? $this->configuration['length'] : NULL;
55 if (!is_null($length) && !is_int($length)) {
56 throw new MigrateException('The character length configuration key should be an integer. Omit this key to capture the entire string.');
58 // Use optional start or length to return a portion of the unique value.
59 $value = Unicode::substr($value, $start, $length);
61 while ($this->exists($new_value)) {
62 $new_value = $value . $postfix . $i++;
68 * This is a query checking the existence of some value.
74 * TRUE if the value exists.
76 abstract protected function exists($value);