You are here

public function Gate::transform in Migrate Plus 8.5

Performs the associated process.

Parameters

mixed $value: The value to be transformed.

\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. Normally, just transforming the value is adequate but very rarely you might need to change two columns at the same time or something like that.

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

Return value

string|array The newly transformed value.

Overrides ProcessPluginBase::transform

File

src/Plugin/migrate/process/Gate.php, line 87

Class

Gate
Allow a source value to pass through the gate conditionally.

Namespace

Drupal\migrate_plus\Plugin\migrate\process

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  if (empty($this->configuration['valid_keys']) && !array_key_exists('valid_keys', $this->configuration)) {
    throw new MigrateException('Gate plugin is missing valid_keys configuration.');
  }
  if (empty($this->configuration['use_as_key']) && !array_key_exists('use_as_key', $this->configuration)) {
    throw new MigrateException('Gate plugin is missing use_as_key configuration.');
  }
  if (empty($this->configuration['key_direction']) && !array_key_exists('key_direction', $this->configuration)) {
    throw new MigrateException('Gate plugin is missing key_direction configuration.');
  }
  if (!in_array($this->configuration['key_direction'], [
    'lock',
    'unlock',
  ], TRUE)) {
    throw new MigrateException('Gate plugin only accepts the following values for key_direction: lock and unlock.');
  }
  $valid_keys = is_array($this->configuration['valid_keys']) ? $this->configuration['valid_keys'] : [
    $this->configuration['valid_keys'],
  ];
  $key = $row
    ->get($this->configuration['use_as_key']);
  $key_is_valid = in_array($key, $valid_keys, TRUE);
  $key_direction = $this->configuration['key_direction'];
  $value_can_pass = $key_is_valid && $key_direction == 'unlock' || !$key_is_valid && $key_direction == 'lock';
  if ($value_can_pass) {
    return $value;
  }
  else {
    if ($key_direction == 'lock') {
      $message = sprintf('Processing of destination property %s was skipped: Gate was locked by property %s with value %s.', $destination_property, $this->configuration['use_as_key'], $key);
    }
    else {
      $message = sprintf('Processing of destination property %s was skipped: Gate was not unlocked by property %s with value %s. ', $destination_property, $this->configuration['use_as_key'], $key);
    }
    throw new MigrateSkipProcessException($message);
  }
}