Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / SkipRowIfNotSet.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4
5 use Drupal\migrate\ProcessPluginBase;
6 use Drupal\migrate\MigrateExecutableInterface;
7 use Drupal\migrate\Row;
8 use Drupal\migrate\MigrateSkipRowException;
9
10 /**
11  * Skips processing the current row when a source value is not set.
12  *
13  * The skip_row_if_not_set process plugin checks whether a value is set. If the
14  * value is set, it is returned. Otherwise, a MigrateSkipRowException
15  * is thrown.
16  *
17  * Available configuration keys:
18  * - index: The source property to check for.
19  * - message: (optional) A message to be logged in the {migrate_message_*} table
20  *   for this row. If not set, nothing is logged in the message table.
21  *
22  * Example:
23  *
24  * @code
25  *  process:
26  *    settings:
27  *      # Check if the "contact" key exists in the "data" array.
28  *      plugin: skip_row_if_not_set
29  *      index: contact
30  *      source: data
31  *      message: "Missed the 'data' key"
32  * @endcode
33  *
34  * This will return $data['contact'] if it exists. Otherwise, the row will be
35  * skipped and the message "Missed the 'data' key" will be logged in the
36  * message table.
37  *
38  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
39  *
40  * @MigrateProcessPlugin(
41  *   id = "skip_row_if_not_set",
42  *   handle_multiples = TRUE
43  * )
44  */
45 class SkipRowIfNotSet extends ProcessPluginBase {
46
47   /**
48    * {@inheritdoc}
49    */
50   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
51     if (!isset($value[$this->configuration['index']])) {
52       $message = !empty($this->configuration['message']) ? $this->configuration['message'] : '';
53       throw new MigrateSkipRowException($message);
54     }
55     return $value[$this->configuration['index']];
56   }
57
58 }