You are here

public function MakeUniqueBase::transform in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/migrate/src/Plugin/migrate/process/MakeUniqueBase.php \Drupal\migrate\Plugin\migrate\process\MakeUniqueBase::transform()
  2. 9 core/modules/migrate/src/Plugin/migrate/process/MakeUniqueBase.php \Drupal\migrate\Plugin\migrate\process\MakeUniqueBase::transform()

Creates a unique value based on the source value.

Parameters

string $value: The input string.

\Drupal\migrate\MigrateExecutableInterface $migrate_executable: The migration in which this process is being executed.

\Drupal\migrate\Row $row: The row from the source to process.

string $destination_property: The destination property currently worked on. This is only used together with the $row above.

Return value

string The unique version of the input value.

Throws

\Drupal\migrate\MigrateException

Overrides ProcessPluginBase::transform

File

core/modules/migrate/src/Plugin/migrate/process/MakeUniqueBase.php, line 46

Class

MakeUniqueBase
This plugin ensures the source value is unique.

Namespace

Drupal\migrate\Plugin\migrate\process

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  $i = 1;
  $postfix = $this->configuration['postfix'] ?? '';
  $start = $this->configuration['start'] ?? 0;
  if (!is_int($start)) {
    throw new MigrateException('The start position configuration key should be an integer. Omit this key to capture from the beginning of the string.');
  }
  $length = $this->configuration['length'] ?? NULL;
  if (!is_null($length) && !is_int($length)) {
    throw new MigrateException('The character length configuration key should be an integer. Omit this key to capture the entire string.');
  }

  // Use optional start or length to return a portion of the unique value.
  $value = mb_substr($value, $start, $length);
  $new_value = $value;
  while ($this
    ->exists($new_value)) {
    $new_value = $value . $postfix . $i++;
  }
  return $new_value;
}