4b9565b72e24cbcda32dd6369f75f391cfce1ee3
[yaffs-website] / web / core / modules / hal / src / Normalizer / EntityReferenceItemNormalizer.php
1 <?php
2
3 namespace Drupal\hal\Normalizer;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Entity\FieldableEntityInterface;
7 use Drupal\Core\Field\FieldItemInterface;
8 use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
9 use Drupal\hal\LinkManager\LinkManagerInterface;
10 use Drupal\serialization\EntityResolver\EntityResolverInterface;
11 use Drupal\serialization\EntityResolver\UuidReferenceInterface;
12 use Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizerTrait;
13
14 /**
15  * Converts the Drupal entity reference item object to HAL array structure.
16  */
17 class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidReferenceInterface {
18
19   use EntityReferenceFieldItemNormalizerTrait;
20
21   /**
22    * The interface or class that this Normalizer supports.
23    *
24    * @var string
25    */
26   protected $supportedInterfaceOrClass = 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem';
27
28   /**
29    * The hypermedia link manager.
30    *
31    * @var \Drupal\hal\LinkManager\LinkManagerInterface
32    */
33   protected $linkManager;
34
35   /**
36    * The entity resolver.
37    *
38    * @var \Drupal\serialization\EntityResolver\EntityResolverInterface
39    */
40   protected $entityResolver;
41
42   /**
43    * The entity type manager.
44    *
45    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
46    */
47   protected $entityTypeManager;
48
49   /**
50    * Constructs an EntityReferenceItemNormalizer object.
51    *
52    * @param \Drupal\hal\LinkManager\LinkManagerInterface $link_manager
53    *   The hypermedia link manager.
54    * @param \Drupal\serialization\EntityResolver\EntityResolverInterface $entity_Resolver
55    *   The entity resolver.
56    * @param \Drupal\Core\Entity\EntityTypeManagerInterface|null $entity_type_manager
57    *   The entity type manager.
58    */
59   public function __construct(LinkManagerInterface $link_manager, EntityResolverInterface $entity_Resolver, EntityTypeManagerInterface $entity_type_manager = NULL) {
60     $this->linkManager = $link_manager;
61     $this->entityResolver = $entity_Resolver;
62     $this->entityTypeManager = $entity_type_manager ?: \Drupal::service('entity_type.manager');
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function normalize($field_item, $format = NULL, array $context = []) {
69     // If this is not a fieldable entity, let the parent implementation handle
70     // it, only fieldable entities are supported as embedded resources.
71     if (!$this->targetEntityIsFieldable($field_item)) {
72       return parent::normalize($field_item, $format, $context);
73     }
74
75     /** @var $field_item \Drupal\Core\Field\FieldItemInterface */
76     $target_entity = $field_item->get('entity')->getValue();
77
78     // If the parent entity passed in a langcode, unset it before normalizing
79     // the target entity. Otherwise, untranslatable fields of the target entity
80     // will include the langcode.
81     $langcode = isset($context['langcode']) ? $context['langcode'] : NULL;
82     unset($context['langcode']);
83     $context['included_fields'] = ['uuid'];
84
85     // Normalize the target entity.
86     $embedded = $this->serializer->normalize($target_entity, $format, $context);
87     $link = $embedded['_links']['self'];
88     // If the field is translatable, add the langcode to the link relation
89     // object. This does not indicate the language of the target entity.
90     if ($langcode) {
91       $embedded['lang'] = $link['lang'] = $langcode;
92     }
93
94     // The returned structure will be recursively merged into the normalized
95     // entity so that the items are properly added to the _links and _embedded
96     // objects.
97     $field_name = $field_item->getParent()->getName();
98     $entity = $field_item->getEntity();
99     $field_uri = $this->linkManager->getRelationUri($entity->getEntityTypeId(), $entity->bundle(), $field_name, $context);
100     return [
101       '_links' => [
102         $field_uri => [$link],
103       ],
104       '_embedded' => [
105         $field_uri => [$embedded],
106       ],
107     ];
108   }
109
110   /**
111    * Checks whether the referenced entity is of a fieldable entity type.
112    *
113    * @param \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item
114    *   The reference field item whose target entity needs to be checked.
115    *
116    * @return bool
117    *   TRUE when the referenced entity is of a fieldable entity type.
118    */
119   protected function targetEntityIsFieldable(EntityReferenceItem $item) {
120     $target_entity = $item->get('entity')->getValue();
121
122     if ($target_entity !== NULL) {
123       return $target_entity instanceof FieldableEntityInterface;
124     }
125
126     $referencing_entity = $item->getEntity();
127     $target_entity_type_id = $item->getFieldDefinition()->getSetting('target_type');
128
129     // If the entity type is the same as the parent, we can check that. This is
130     // just a shortcut to avoid getting the entity type definition and checking
131     // the class.
132     if ($target_entity_type_id === $referencing_entity->getEntityTypeId()) {
133       return $referencing_entity instanceof FieldableEntityInterface;
134     }
135
136     // Otherwise, we need to get the class for the type.
137     $target_entity_type = $this->entityTypeManager->getDefinition($target_entity_type_id);
138     $target_entity_type_class = $target_entity_type->getClass();
139
140     return is_a($target_entity_type_class, FieldableEntityInterface::class, TRUE);
141   }
142
143   /**
144    * {@inheritdoc}
145    */
146   protected function constructValue($data, $context) {
147     $field_item = $context['target_instance'];
148     $field_definition = $field_item->getFieldDefinition();
149     $target_type = $field_definition->getSetting('target_type');
150     $id = $this->entityResolver->resolve($this, $data, $target_type);
151     if (isset($id)) {
152       return ['target_id' => $id] + array_intersect_key($data, $field_item->getProperties());
153     }
154     return NULL;
155   }
156
157   /**
158    * {@inheritdoc}
159    */
160   protected function normalizedFieldValues(FieldItemInterface $field_item, $format, array $context) {
161     // Normalize root reference values here so we don't need to deal with hal's
162     // nested data structure for field items. This will be called from
163     // \Drupal\hal\Normalizer\FieldItemNormalizer::normalize. Which will only
164     // be called from this class for entities that are not fieldable.
165     $normalized = parent::normalizedFieldValues($field_item, $format, $context);
166
167     $this->normalizeRootReferenceValue($normalized, $field_item);
168
169     return $normalized;
170   }
171
172   /**
173    * {@inheritdoc}
174    */
175   public function getUuid($data) {
176     if (isset($data['uuid'])) {
177       $uuid = $data['uuid'];
178       // The value may be a nested array like $uuid[0]['value'].
179       if (is_array($uuid) && isset($uuid[0]['value'])) {
180         $uuid = $uuid[0]['value'];
181       }
182       return $uuid;
183     }
184   }
185
186 }