Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[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   use EntityReferenceFieldItemNormalizerTrait;
16
17   /**
18    * The interface or class that this Normalizer supports.
19    *
20    * @var string
21    */
22   protected $supportedInterfaceOrClass = EntityReferenceItem::class;
23
24   /**
25    * The entity repository.
26    *
27    * @var \Drupal\Core\Entity\EntityRepositoryInterface
28    */
29   protected $entityRepository;
30
31   /**
32    * Constructs a EntityReferenceFieldItemNormalizer object.
33    *
34    * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
35    *   The entity repository.
36    */
37   public function __construct(EntityRepositoryInterface $entity_repository) {
38     $this->entityRepository = $entity_repository;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function normalize($field_item, $format = NULL, array $context = []) {
45     $values = parent::normalize($field_item, $format, $context);
46
47     $this->normalizeRootReferenceValue($values, $field_item);
48
49     /** @var \Drupal\Core\Entity\EntityInterface $entity */
50     if ($entity = $field_item->get('entity')->getValue()) {
51       $values['target_type'] = $entity->getEntityTypeId();
52       // Add the target entity UUID to the normalized output values.
53       $values['target_uuid'] = $entity->uuid();
54
55       // Add a 'url' value if there is a reference and a canonical URL. Hard
56       // code 'canonical' here as config entities override the default $rel
57       // parameter value to 'edit-form.
58       if ($url = $entity->url('canonical')) {
59         $values['url'] = $url;
60       }
61     }
62
63     return $values;
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   protected function constructValue($data, $context) {
70     if (isset($data['target_uuid'])) {
71       /** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $field_item */
72       $field_item = $context['target_instance'];
73       if (empty($data['target_uuid'])) {
74         throw new InvalidArgumentException(sprintf('If provided "target_uuid" cannot be empty for field "%s".', $data['target_type'], $data['target_uuid'], $field_item->getName()));
75       }
76       $target_type = $field_item->getFieldDefinition()->getSetting('target_type');
77       if (!empty($data['target_type']) && $target_type !== $data['target_type']) {
78         throw new UnexpectedValueException(sprintf('The field "%s" property "target_type" must be set to "%s" or omitted.', $field_item->getFieldDefinition()->getName(), $target_type));
79       }
80       if ($entity = $this->entityRepository->loadEntityByUuid($target_type, $data['target_uuid'])) {
81         return ['target_id' => $entity->id()] + array_intersect_key($data, $field_item->getProperties());
82       }
83       else {
84         // Unable to load entity by uuid.
85         throw new InvalidArgumentException(sprintf('No "%s" entity found with UUID "%s" for field "%s".', $data['target_type'], $data['target_uuid'], $field_item->getName()));
86       }
87     }
88     return parent::constructValue($data, $context);
89   }
90
91 }