ea2e020bf00f558513e9cff01201db4b55f1cc31
[yaffs-website] / web / core / modules / serialization / src / Normalizer / EntityReferenceFieldItemNormalizer.php
1 <?php
2
3 namespace Drupal\serialization\Normalizer;
4
5 use Drupal\Core\Entity\EntityRepositoryInterface;
6 use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
7 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
8 use Symfony\Component\Serializer\Exception\UnexpectedValueException;
9
10 /**
11  * Adds the file URI to embedded file entities.
12  */
13 class EntityReferenceFieldItemNormalizer extends FieldItemNormalizer {
14
15   /**
16    * The interface or class that this Normalizer supports.
17    *
18    * @var string
19    */
20   protected $supportedInterfaceOrClass = EntityReferenceItem::class;
21
22   /**
23    * The entity repository.
24    *
25    * @var \Drupal\Core\Entity\EntityRepositoryInterface
26    */
27   protected $entityRepository;
28
29   /**
30    * Constructs a EntityReferenceFieldItemNormalizer object.
31    *
32    * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
33    *   The entity repository.
34    */
35   public function __construct(EntityRepositoryInterface $entity_repository) {
36     $this->entityRepository = $entity_repository;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public function normalize($field_item, $format = NULL, array $context = []) {
43     $values = parent::normalize($field_item, $format, $context);
44
45     /** @var \Drupal\Core\Entity\EntityInterface $entity */
46     if ($entity = $field_item->get('entity')->getValue()) {
47       $values['target_type'] = $entity->getEntityTypeId();
48       // Add the target entity UUID to the normalized output values.
49       $values['target_uuid'] = $entity->uuid();
50
51       // Add a 'url' value if there is a reference and a canonical URL. Hard
52       // code 'canonical' here as config entities override the default $rel
53       // parameter value to 'edit-form.
54       if ($url = $entity->url('canonical')) {
55         $values['url'] = $url;
56       }
57     }
58     return $values;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   protected function constructValue($data, $context) {
65     if (isset($data['target_uuid'])) {
66       /** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $field_item */
67       $field_item = $context['target_instance'];
68       if (empty($data['target_uuid'])) {
69         throw new InvalidArgumentException(sprintf('If provided "target_uuid" cannot be empty for field "%s".', $data['target_type'], $data['target_uuid'], $field_item->getName()));
70       }
71       $target_type = $field_item->getFieldDefinition()->getSetting('target_type');
72       if (!empty($data['target_type']) && $target_type !== $data['target_type']) {
73         throw new UnexpectedValueException(sprintf('The field "%s" property "target_type" must be set to "%s" or omitted.', $field_item->getFieldDefinition()->getName(), $target_type));
74       }
75       if ($entity = $this->entityRepository->loadEntityByUuid($target_type, $data['target_uuid'])) {
76         return ['target_id' => $entity->id()];
77       }
78       else {
79         // Unable to load entity by uuid.
80         throw new InvalidArgumentException(sprintf('No "%s" entity found with UUID "%s" for field "%s".', $data['target_type'], $data['target_uuid'], $field_item->getName()));
81       }
82     }
83     return parent::constructValue($data, $context);
84   }
85
86 }