3 namespace Drupal\migrate_plus\Plugin\migrate\process;
5 use Drupal\migrate\MigrateException;
6 use Drupal\migrate\MigrateExecutableInterface;
7 use Drupal\migrate\MigrateSkipProcessException;
8 use Drupal\migrate\MigrateSkipRowException;
9 use Drupal\migrate\ProcessPluginBase;
10 use Drupal\migrate\Row;
13 * If the source evaluates to a configured value, skip processing or whole row.
15 * @MigrateProcessPlugin(
16 * id = "skip_on_value"
19 * Available configuration keys:
20 * - value: An single value or array of values against which the source value
22 * - not_equals: (optional) If set, skipping occurs when values are not equal.
23 * - method: What to do if the input value is empty. Possible values:
24 * - row: Skips the entire row when an empty value is encountered.
25 * - process: Prevents further processing of the input property when the value
30 * Example usage with minimal configuration:
33 * plugin: skip_on_value
34 * source: content_type
39 * The above example will skip processing the input property if the content_type
40 * source field equals "blog".
42 * Example usage with full configuration:
45 * plugin: skip_on_value
47 * source: content_type
54 * The above example will skip processing any row for which the source row's
55 * content type field is not "article" or "testimonial".
57 class SkipOnValue extends ProcessPluginBase {
62 public function row($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
63 if (empty($this->configuration['value']) && !array_key_exists('value', $this->configuration)) {
64 throw new MigrateException('Skip on value plugin is missing value configuration.');
67 if (is_array($this->configuration['value'])) {
68 $value_in_array = FALSE;
69 $not_equals = isset($this->configuration['not_equals']);
71 foreach ($this->configuration['value'] as $skipValue) {
72 $value_in_array |= $this->compareValue($value, $skipValue);
75 if (($not_equals && !$value_in_array) || (!$not_equals && $value_in_array)) {
76 throw new MigrateSkipRowException();
79 elseif ($this->compareValue($value, $this->configuration['value'], !isset($this->configuration['not_equals']))) {
80 throw new MigrateSkipRowException();
89 public function process($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
90 if (empty($this->configuration['value']) && !array_key_exists('value', $this->configuration)) {
91 throw new MigrateException('Skip on value plugin is missing value configuration.');
94 if (is_array($this->configuration['value'])) {
95 $value_in_array = FALSE;
96 $not_equals = isset($this->configuration['not_equals']);
98 foreach ($this->configuration['value'] as $skipValue) {
99 $value_in_array |= $this->compareValue($value, $skipValue);
102 if (($not_equals && !$value_in_array) || (!$not_equals && $value_in_array)) {
103 throw new MigrateSkipProcessException();
106 elseif ($this->compareValue($value, $this->configuration['value'], !isset($this->configuration['not_equals']))) {
107 throw new MigrateSkipProcessException();
114 * Compare values to see if they are equal.
116 * @param mixed $value
118 * @param mixed $skipValue
119 * Value to compare against.
121 * Compare as equal or not equal.
124 * True if the compare successfully, FALSE otherwise.
126 protected function compareValue($value, $skipValue, $equal = TRUE) {
128 return (string) $value == (string) $skipValue;
131 return (string) $value != (string) $skipValue;