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;
11 * This plugin ensures the source value is unique.
13 * The MakeUniqueBase process plugin is used to avoid duplication at the
14 * destination. For example, when creating filter format names, the source
15 * value is checked against the existing filter format names and if it exists,
16 * a numeric postfix is added and incremented until a unique value is created.
17 * An optional postfix string can be insert before the numeric postfix.
19 * Available configuration keys
20 * - start: (optional) The position at which to start reading.
21 * - length: (optional) The number of characters to read.
22 * - postfix: (optional) A string to insert before the numeric postfix.
24 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
26 abstract class MakeUniqueBase extends ProcessPluginBase {
29 * Creates a unique value based on the source value.
31 * @param string $value
33 * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
34 * The migration in which this process is being executed.
35 * @param \Drupal\migrate\Row $row
36 * The row from the source to process.
37 * @param string $destination_property
38 * The destination property currently worked on. This is only used together
39 * with the $row above.
42 * The unique version of the input value.
44 * @throws \Drupal\migrate\MigrateException
46 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
48 $postfix = isset($this->configuration['postfix']) ? $this->configuration['postfix'] : '';
49 $start = isset($this->configuration['start']) ? $this->configuration['start'] : 0;
50 if (!is_int($start)) {
51 throw new MigrateException('The start position configuration key should be an integer. Omit this key to capture from the beginning of the string.');
53 $length = isset($this->configuration['length']) ? $this->configuration['length'] : NULL;
54 if (!is_null($length) && !is_int($length)) {
55 throw new MigrateException('The character length configuration key should be an integer. Omit this key to capture the entire string.');
57 // Use optional start or length to return a portion of the unique value.
58 $value = mb_substr($value, $start, $length);
60 while ($this->exists($new_value)) {
61 $new_value = $value . $postfix . $i++;
67 * This is a query checking the existence of some value.
73 * TRUE if the value exists.
75 abstract protected function exists($value);