Version 1
[yaffs-website] / web / core / modules / serialization / src / Normalizer / ListNormalizer.php
diff --git a/web/core/modules/serialization/src/Normalizer/ListNormalizer.php b/web/core/modules/serialization/src/Normalizer/ListNormalizer.php
new file mode 100644 (file)
index 0000000..471886e
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+
+namespace Drupal\serialization\Normalizer;
+
+/**
+ * Converts list objects to arrays.
+ *
+ * Ordinarily, this would be handled automatically by Serializer, but since
+ * there is a TypedDataNormalizer and the Field class extends TypedData, any
+ * Field will be handled by that Normalizer instead of being traversed. This
+ * class ensures that TypedData classes that also implement ListInterface are
+ * traversed instead of simply returning getValue().
+ */
+class ListNormalizer extends NormalizerBase {
+
+  /**
+   * The interface or class that this Normalizer supports.
+   *
+   * @var string
+   */
+  protected $supportedInterfaceOrClass = 'Drupal\Core\TypedData\ListInterface';
+
+  /**
+   * {@inheritdoc}
+   */
+  public function normalize($object, $format = NULL, array $context = []) {
+    $attributes = [];
+    foreach ($object as $fieldItem) {
+      $attributes[] = $this->serializer->normalize($fieldItem, $format, $context);
+    }
+    return $attributes;
+  }
+
+}