/** * @covers ::normalize */ public function testNormalize() { $context = ['test' => 'test']; $serializer_prophecy = $this->prophesize(Serializer::class); $serializer_prophecy->normalize('A', static::TEST_FORMAT, $context)->shouldBeCalled(); $serializer_prophecy->normalize('B', static::TEST_FORMAT, $context)->shouldBeCalled(); $this->normalizer->setSerializer($serializer_prophecy->reveal()); $complex_data = new TestComplexData(['a' => 'A', 'b' => 'B']); $this->normalizer->normalize($complex_data, static::TEST_FORMAT, $context); }
/** * {@inheritdoc} */ public function normalize($field_item, $format = NULL, array $context = []) { $values = parent::normalize($field_item, $format, $context); // Add a 'url' value if there is a reference and a canonical URL. Hard code // 'canonical' here as config entities override the default $rel parameter // value to 'edit-form. /** @var \Drupal\Core\Entity\EntityInterface $entity */ if (($entity = $field_item->get('entity')->getValue()) && ($url = $entity->url('canonical'))) { $values['url'] = $url; } return $values; }
/** * {@inheritdoc} */ public function normalize($field_item, $format = NULL, array $context = []) { $values = parent::normalize($field_item, $format, $context); /** @var \Drupal\Core\Entity\EntityInterface $entity */ if ($entity = $field_item->get('entity')->getValue()) { $values['target_type'] = $entity->getEntityTypeId(); // Add the target entity UUID to the normalized output values. $values['target_uuid'] = $entity->uuid(); // Add a 'url' value if there is a reference and a canonical URL. Hard // code 'canonical' here as config entities override the default $rel // parameter value to 'edit-form. if ($url = $entity->url('canonical')) { $values['url'] = $url; } } return $values; }