From 171827b1e8e61921d11699dff5a29a2fd996a658 Mon Sep 17 00:00:00 2001
From: Tadej Basa <tadej.basa@gmail.com>
Date: Fri, 25 Nov 2016 19:10:45 +0100
Subject: [PATCH] Issue #2614292 by paranojik, das-peter: Added HAL
 serialization support.

---
 field_collection.services.yml                      |  6 +++++
 src/Entity/FieldCollectionItem.php                 | 25 ++++++++++++++++++-
 src/Normalizer/FieldCollectionItemNormalizer.php   |  9 +++++++
 .../FieldCollectionItemNormalizerHal.php           | 28 ++++++++++++++++++++++
 4 files changed, 67 insertions(+), 1 deletion(-)
 create mode 100644 src/Normalizer/FieldCollectionItemNormalizerHal.php

diff --git a/field_collection.services.yml b/field_collection.services.yml
index 3c65d08..1c93d75 100644
--- a/field_collection.services.yml
+++ b/field_collection.services.yml
@@ -21,3 +21,9 @@ services:
     arguments: ['@entity.manager']
     tags:
       - { name: normalizer, priority: 20 }
+
+  field_collection.normalizer.field_collection_item.hal:
+    class: Drupal\field_collection\Normalizer\FieldCollectionItemNormalizerHal
+    arguments: ['@rest.link_manager', '@entity.manager', '@module_handler']
+    tags:
+      - { name: normalizer, priority: 20 }
\ No newline at end of file
diff --git a/src/Entity/FieldCollectionItem.php b/src/Entity/FieldCollectionItem.php
index a33e776..54cafed 100644
--- a/src/Entity/FieldCollectionItem.php
+++ b/src/Entity/FieldCollectionItem.php
@@ -72,6 +72,17 @@ class FieldCollectionItem extends ContentEntityBase implements FieldCollectionIt
   protected $host_revision_id;
 
   /**
+   * A flag that acts same as the $skip_host_save parameter to FieldCollectionItem::save().
+   *
+   * Automatically resets back to FALSE after first call to FieldCollectionItem::save().
+   *
+   * @see: FieldCollectionItem::save()
+   *
+   * @var bool
+   */
+  protected $skip_host_check;
+
+  /**
    * Implements Drupal\Core\Entity\EntityInterface::id().
    */
   public function id() {
@@ -133,6 +144,17 @@ class FieldCollectionItem extends ContentEntityBase implements FieldCollectionIt
   }
 
   /**
+   * Set a flag that works same as the $skip_host_save parameter to FieldCollectionItem::save().
+   *
+   * @see: FieldCollectionItem::save()
+   * 
+   * @param $bool
+   */
+  public function skipHostCheck($bool) {
+    $this->skip_host_check = $bool;
+  }
+
+  /**
    * Save the field collection item.
    *
    * By default, always save the host entity, so modules are able to react
@@ -161,7 +183,8 @@ class FieldCollectionItem extends ContentEntityBase implements FieldCollectionIt
     // Only save directly if we are told to skip saving the host entity. Else,
     // we always save via the host as saving the host might trigger saving
     // field collection items anyway (e.g. if a new revision is created).
-    if ($skip_host_save) {
+    if ($skip_host_save || $this->skip_host_check) {
+      $this->skip_host_check = FALSE;
       return parent::save();
     }
     else {
diff --git a/src/Normalizer/FieldCollectionItemNormalizer.php b/src/Normalizer/FieldCollectionItemNormalizer.php
index 7ff2aac..8802aaa 100644
--- a/src/Normalizer/FieldCollectionItemNormalizer.php
+++ b/src/Normalizer/FieldCollectionItemNormalizer.php
@@ -27,4 +27,13 @@ class FieldCollectionItemNormalizer extends ComplexDataNormalizer {
     return $values;
   }
 
+  /**
+   * @return string
+   */
+  public function supportsNormalization($data, $format = NULL) {
+    // Don't use this normalizer for hal_json. FieldCollectionItemNormalizerHal
+    // will be used instead and just export the references.
+    return ($format != 'hal_json') && parent::supportsNormalization($data, $format);
+  }
+
 }
diff --git a/src/Normalizer/FieldCollectionItemNormalizerHal.php b/src/Normalizer/FieldCollectionItemNormalizerHal.php
new file mode 100644
index 0000000..8846437
--- /dev/null
+++ b/src/Normalizer/FieldCollectionItemNormalizerHal.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace Drupal\field_collection\Normalizer;
+
+use Drupal\hal\Normalizer\ContentEntityNormalizer;
+use Drupal\field_collection\Entity\FieldCollectionItem;
+
+/**
+ * Adds field collection items to field collections.
+ */
+class FieldCollectionItemNormalizerHal extends ContentEntityNormalizer {
+
+  /**
+   * The interface or class that this Normalizer supports.
+   *
+   * @var string
+   */
+  protected $supportedInterfaceOrClass = FieldCollectionItem::class;
+
+  /**
+   * @inheritdoc
+   */
+  public function denormalize($data, $class, $format = NULL, array $context = array()) {
+    $entity = parent::denormalize($data, $class, $format, $context);
+    $entity->skipHostCheck(TRUE);
+    return $entity;
+  }
+}
-- 
2.8.1

