diff --git a/core/modules/migrate/src/Plugin/migrate/process/SkipOnEmpty.php b/core/modules/migrate/src/Plugin/migrate/process/SkipOnEmpty.php index 45626bed8d63dba57fa5e98fab8c77bbff0dfd4b..c7d3ad5ea131a9d7708e247281667cbcd3732ea8 100644 --- a/core/modules/migrate/src/Plugin/migrate/process/SkipOnEmpty.php +++ b/core/modules/migrate/src/Plugin/migrate/process/SkipOnEmpty.php @@ -21,6 +21,9 @@ * - row: Skips the entire row when an empty value is encountered. * - process: Prevents further processing of the input property when the value * is empty. + * - message: (optional) A message to be logged in the {migrate_message_*} table + * for this row. Messages are only logged for the 'row' skip level. If not + * set, nothing is logged in the message table. * * Examples: * @@ -30,9 +33,11 @@ * plugin: skip_on_empty * method: row * source: field_name + * message: 'Field field_name is missed' * @endcode * - * If field_name is empty, skips the entire row. + * If field_name is empty, skips the entire row and the message 'Field + * field_name is missed' is logged in the message table. * * @code * process: @@ -79,7 +84,8 @@ class SkipOnEmpty extends ProcessPluginBase { */ public function row($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { if (!$value) { - throw new MigrateSkipRowException(); + $message = !empty($this->configuration['message']) ? $this->configuration['message'] : ''; + throw new MigrateSkipRowException($message); } return $value; } diff --git a/core/modules/migrate/src/Plugin/migrate/process/SkipRowIfNotSet.php b/core/modules/migrate/src/Plugin/migrate/process/SkipRowIfNotSet.php index ab7552e6cfc3e3b989972fe1f7d2c786c28ab9d3..fa9b3f618070c80987aac29b2f11dc68eb38cd64 100644 --- a/core/modules/migrate/src/Plugin/migrate/process/SkipRowIfNotSet.php +++ b/core/modules/migrate/src/Plugin/migrate/process/SkipRowIfNotSet.php @@ -16,6 +16,8 @@ * * Available configuration keys: * - index: The source property to check for. + * - message: (optional) A message to be logged in the {migrate_message_*} table + * for this row. If not set, nothing is logged in the message table. * * Example: * @@ -26,10 +28,12 @@ * plugin: skip_row_if_not_set * index: contact * source: data + * message: "Missed the 'data' key" * @endcode * * This will return $data['contact'] if it exists. Otherwise, the row will be - * skipped. + * skipped and the message "Missed the 'data' key" will be logged in the + * message table. * * @see \Drupal\migrate\Plugin\MigrateProcessInterface * @@ -45,7 +49,8 @@ class SkipRowIfNotSet extends ProcessPluginBase { */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { if (!isset($value[$this->configuration['index']])) { - throw new MigrateSkipRowException(); + $message = !empty($this->configuration['message']) ? $this->configuration['message'] : ''; + throw new MigrateSkipRowException($message); } return $value[$this->configuration['index']]; } diff --git a/core/modules/migrate/tests/src/Unit/process/SkipOnEmptyTest.php b/core/modules/migrate/tests/src/Unit/process/SkipOnEmptyTest.php index 66b27cc34f788d29aaaa5f48e172a6a18605fb8b..08fbef238e4c35cf1df02420a1ef8f9ff4f1ac7d 100644 --- a/core/modules/migrate/tests/src/Unit/process/SkipOnEmptyTest.php +++ b/core/modules/migrate/tests/src/Unit/process/SkipOnEmptyTest.php @@ -1,6 +1,7 @@ assertSame($value, ' '); } + /** + * Tests that a skip row exception without a message is raised. + * + * @covers ::row + */ + public function testRowSkipWithoutMessage() { + $configuration = [ + 'method' => 'row', + ]; + $process = new SkipOnEmpty($configuration, 'skip_on_empty', []); + $this->setExpectedException(MigrateSkipRowException::class); + $process->transform('', $this->migrateExecutable, $this->row, 'destinationproperty'); + } + + /** + * Tests that a skip row exception with a message is raised. + * + * @covers ::row + */ + public function testRowSkipWithMessage() { + $configuration = [ + 'method' => 'row', + 'message' => 'The value is empty', + ]; + $process = new SkipOnEmpty($configuration, 'skip_on_empty', []); + $this->setExpectedException(MigrateSkipRowException::class, 'The value is empty'); + $process->transform('', $this->migrateExecutable, $this->row, 'destinationproperty'); + } + } diff --git a/core/modules/migrate/tests/src/Unit/process/SkipRowIfNotSetTest.php b/core/modules/migrate/tests/src/Unit/process/SkipRowIfNotSetTest.php new file mode 100644 index 0000000000000000000000000000000000000000..26e5b3fb4afbf3fdf0ef610f60b35e1b943f2b50 --- /dev/null +++ b/core/modules/migrate/tests/src/Unit/process/SkipRowIfNotSetTest.php @@ -0,0 +1,45 @@ + 'some_key', + ]; + $process = new SkipRowIfNotSet($configuration, 'skip_row_if_not_set', []); + $this->setExpectedException(MigrateSkipRowException::class); + $process->transform('', $this->migrateExecutable, $this->row, 'destinationproperty'); + } + + /** + * Tests that a skip row exception with a message is raised. + * + * @covers ::transform + */ + public function testRowSkipWithMessage() { + $configuration = [ + 'index' => 'some_key', + 'message' => "The 'some_key' key is not set", + ]; + $process = new SkipRowIfNotSet($configuration, 'skip_row_if_not_set', []); + $this->setExpectedException(MigrateSkipRowException::class, "The 'some_key' key is not set"); + $process->transform('', $this->migrateExecutable, $this->row, 'destinationproperty'); + } + +}