Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / SkipOnEmpty.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4
5 use Drupal\migrate\MigrateSkipProcessException;
6 use Drupal\migrate\ProcessPluginBase;
7 use Drupal\migrate\MigrateExecutableInterface;
8 use Drupal\migrate\Row;
9 use Drupal\migrate\MigrateSkipRowException;
10
11 /**
12  * Skips processing the current row when the input value is empty.
13  *
14  * The skip_on_empty process plugin checks to see if the current input value
15  * is empty (empty string, NULL, FALSE, 0, '0', or an empty array). If so, the
16  * further processing of the property or the entire row (depending on the chosen
17  * method) is skipped and will not be migrated.
18  *
19  * Available configuration keys:
20  * - method: (optional) What to do if the input value is empty. Possible values:
21  *   - row: Skips the entire row when an empty value is encountered.
22  *   - process: Prevents further processing of the input property when the value
23  *     is empty.
24  * - message: (optional) A message to be logged in the {migrate_message_*} table
25  *   for this row. Messages are only logged for the 'row' skip level. If not
26  *   set, nothing is logged in the message table.
27  *
28  * Examples:
29  *
30  * @code
31  * process:
32  *   field_type_exists:
33  *     plugin: skip_on_empty
34  *     method: row
35  *     source: field_name
36  *     message: 'Field field_name is missed'
37  * @endcode
38  *
39  * If field_name is empty, skips the entire row and the message 'Field
40  * field_name is missed' is logged in the message table.
41  *
42  * @code
43  * process:
44  *   parent:
45  *     -
46  *       plugin: skip_on_empty
47  *       method: process
48  *       source: parent
49  *     -
50  *       plugin: migration
51  *       migration: d6_taxonomy_term
52  * @endcode
53  *
54  * If parent is empty, any further processing of the property is skipped - thus,
55  * the next plugin (migration) will not be run.
56  *
57  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
58  *
59  * @MigrateProcessPlugin(
60  *   id = "skip_on_empty"
61  * )
62  */
63 class SkipOnEmpty extends ProcessPluginBase {
64
65   /**
66    * Skips the current row when value is not set.
67    *
68    * @param mixed $value
69    *   The input value.
70    * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
71    *   The migration in which this process is being executed.
72    * @param \Drupal\migrate\Row $row
73    *   The row from the source to process.
74    * @param string $destination_property
75    *   The destination property currently worked on. This is only used together
76    *   with the $row above.
77    *
78    * @return mixed
79    *   The input value, $value, if it is not empty.
80    *
81    * @throws \Drupal\migrate\MigrateSkipRowException
82    *   Thrown if the source property is not set and the row should be skipped,
83    *   records with STATUS_IGNORED status in the map.
84    */
85   public function row($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
86     if (!$value) {
87       $message = !empty($this->configuration['message']) ? $this->configuration['message'] : '';
88       throw new MigrateSkipRowException($message);
89     }
90     return $value;
91   }
92
93   /**
94    * Stops processing the current property when value is not set.
95    *
96    * @param mixed $value
97    *   The input value.
98    * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
99    *   The migration in which this process is being executed.
100    * @param \Drupal\migrate\Row $row
101    *   The row from the source to process.
102    * @param string $destination_property
103    *   The destination property currently worked on. This is only used together
104    *   with the $row above.
105    *
106    * @return mixed
107    *   The input value, $value, if it is not empty.
108    *
109    * @throws \Drupal\migrate\MigrateSkipProcessException
110    *   Thrown if the source property is not set and rest of the process should
111    *   be skipped.
112    */
113   public function process($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
114     if (!$value) {
115       throw new MigrateSkipProcessException();
116     }
117     return $value;
118   }
119
120 }