Version 1
[yaffs-website] / web / core / modules / serialization / src / Normalizer / EntityReferenceFieldItemNormalizer.php
1 <?php
2
3 namespace Drupal\serialization\Normalizer;
4
5 use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
6
7 /**
8  * Adds the file URI to embedded file entities.
9  */
10 class EntityReferenceFieldItemNormalizer extends ComplexDataNormalizer {
11
12   /**
13    * The interface or class that this Normalizer supports.
14    *
15    * @var string
16    */
17   protected $supportedInterfaceOrClass = EntityReferenceItem::class;
18
19   /**
20    * {@inheritdoc}
21    */
22   public function normalize($field_item, $format = NULL, array $context = []) {
23     $values = parent::normalize($field_item, $format, $context);
24
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();
30
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;
36       }
37     }
38
39     return $values;
40   }
41
42 }