diff --git a/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php b/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php index 671a1eed3b..1a4ef54e12 100644 --- a/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php +++ b/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php @@ -4,6 +4,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; +use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\hal\LinkManager\LinkManagerInterface; use Drupal\serialization\EntityResolver\EntityResolverInterface; @@ -68,11 +69,7 @@ public function normalize($field_item, $format = NULL, array $context = []) { // If this is not a fieldable entity, let the parent implementation handle // it, only fieldable entities are supported as embedded resources. if (!$this->targetEntityIsFieldable($field_item)) { - $normalized = parent::normalize($field_item, $format, $context); - - $this->normalizeRootReferenceValue($normalized, $field_item); - - return $normalized; + return parent::normalize($field_item, $format, $context); } /** @var $field_item \Drupal\Core\Field\FieldItemInterface */ @@ -157,6 +154,22 @@ protected function constructValue($data, $context) { return NULL; } + /** + * {@inheritdoc} + */ + protected function normalizedFieldValues(FieldItemInterface $field_item, $format, array $context) { + // Normalize root reference values here so we don't need to deal with hal's + // nested data structure for field items. This will be called from + // \Drupal\hal\Normalizer\FieldItemNormalizer::normalize. Which will only + // be called from this class for entities that are not fieldable. + $normalized = parent::normalizedFieldValues($field_item, $format, $context); + + $this->normalizeRootReferenceValue($normalized, $field_item); + + return $normalized; + } + + /** * {@inheritdoc} */ diff --git a/core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php b/core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php index c6806f8c31..9320d17ba2 100644 --- a/core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php +++ b/core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php @@ -120,6 +120,13 @@ public function testNormalize() { ->willReturn($entity->reveal()) ->shouldBeCalled(); + $field_definition = $this->prophesize(FieldDefinitionInterface::class); + $field_definition->getSetting('target_type') + ->willReturn('test_type'); + + $this->fieldItem->getFieldDefinition() + ->willReturn($field_definition->reveal()); + $this->fieldItem->get('entity') ->willReturn($entity_reference) ->shouldBeCalled(); @@ -188,6 +195,13 @@ public function testNormalizeWithNoEntity() { ->willReturn(NULL) ->shouldBeCalled(); + $field_definition = $this->prophesize(FieldDefinitionInterface::class); + $field_definition->getSetting('target_type') + ->willReturn('test_type'); + + $this->fieldItem->getFieldDefinition() + ->willReturn($field_definition->reveal()); + $this->fieldItem->get('entity') ->willReturn($entity_reference->reveal()) ->shouldBeCalled();