Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / migrate_plus / src / Plugin / migrate / process / ArrayShift.php
diff --git a/web/modules/contrib/migrate_plus/src/Plugin/migrate/process/ArrayShift.php b/web/modules/contrib/migrate_plus/src/Plugin/migrate/process/ArrayShift.php
new file mode 100644 (file)
index 0000000..02ef190
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+namespace Drupal\migrate_plus\Plugin\migrate\process;
+
+use Drupal\migrate\MigrateException;
+use Drupal\migrate\MigrateExecutableInterface;
+use Drupal\migrate\ProcessPluginBase;
+use Drupal\migrate\Row;
+
+/**
+ * Performs an array_shift() on a source array.
+ *
+ * @MigrateProcessPlugin(
+ *   id = "array_shift",
+ *   handle_multiples = TRUE
+ * )
+ *
+ * The "extract" plugin in core can extract array values when indexes are
+ * already known. This plugin helps extract the first value in an array by
+ * performing a "shift" operation.
+ *
+ * Example: Say, the migration source has an associative array of names in
+ * a property called "authors" and the keys in the array can vary, you
+ * can extract the first value like this:
+ *
+ * @code
+ *   first_author:
+ *     plugin: array_shift
+ *     source: authors
+ * @endcode
+ */
+class ArrayShift extends ProcessPluginBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
+    if (!is_array($value)) {
+      throw new MigrateException('Input should be an array.');
+    }
+    return array_shift($value);
+  }
+
+}