3 namespace Drupal\serialization\Normalizer;
5 use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
8 * Adds the file URI to embedded file entities.
10 class EntityReferenceFieldItemNormalizer extends ComplexDataNormalizer {
13 * The interface or class that this Normalizer supports.
17 protected $supportedInterfaceOrClass = EntityReferenceItem::class;
22 public function normalize($field_item, $format = NULL, array $context = []) {
23 $values = parent::normalize($field_item, $format, $context);
25 /** @var \Drupal\Core\Entity\EntityInterface $entity */
26 if ($entity = $field_item->get('entity')->getValue()) {
27 $values['target_type'] = $entity->getEntityTypeId();
28 // Add the target entity UUID to the normalized output values.
29 $values['target_uuid'] = $entity->uuid();
31 // Add a 'url' value if there is a reference and a canonical URL. Hard
32 // code 'canonical' here as config entities override the default $rel
33 // parameter value to 'edit-form.
34 if ($url = $entity->url('canonical')) {
35 $values['url'] = $url;