Gate.php in Migrate Plus 8.5
File
src/Plugin/migrate/process/Gate.php
View source
<?php
namespace Drupal\migrate_plus\Plugin\migrate\process;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
class Gate extends ProcessPluginBase {
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);
}
}
}
Classes
Name |
Description |
Gate |
Allow a source value to pass through the gate conditionally. |