3 namespace Drupal\hal\Normalizer;
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;
15 * Converts the Drupal entity reference item object to HAL array structure.
17 class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidReferenceInterface {
19 use EntityReferenceFieldItemNormalizerTrait;
22 * The interface or class that this Normalizer supports.
26 protected $supportedInterfaceOrClass = 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem';
29 * The hypermedia link manager.
31 * @var \Drupal\hal\LinkManager\LinkManagerInterface
33 protected $linkManager;
36 * The entity resolver.
38 * @var \Drupal\serialization\EntityResolver\EntityResolverInterface
40 protected $entityResolver;
43 * The entity type manager.
45 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
47 protected $entityTypeManager;
50 * Constructs an EntityReferenceItemNormalizer object.
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.
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');
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);
75 /** @var $field_item \Drupal\Core\Field\FieldItemInterface */
76 $target_entity = $field_item->get('entity')->getValue();
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'];
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.
91 $embedded['lang'] = $link['lang'] = $langcode;
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
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);
102 $field_uri => [$link],
105 $field_uri => [$embedded],
111 * Checks whether the referenced entity is of a fieldable entity type.
113 * @param \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item
114 * The reference field item whose target entity needs to be checked.
117 * TRUE when the referenced entity is of a fieldable entity type.
119 protected function targetEntityIsFieldable(EntityReferenceItem $item) {
120 $target_entity = $item->get('entity')->getValue();
122 if ($target_entity !== NULL) {
123 return $target_entity instanceof FieldableEntityInterface;
126 $referencing_entity = $item->getEntity();
127 $target_entity_type_id = $item->getFieldDefinition()->getSetting('target_type');
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
132 if ($target_entity_type_id === $referencing_entity->getEntityTypeId()) {
133 return $referencing_entity instanceof FieldableEntityInterface;
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();
140 return is_a($target_entity_type_class, FieldableEntityInterface::class, TRUE);
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);
152 return ['target_id' => $id] + array_intersect_key($data, $field_item->getProperties());
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);
167 $this->normalizeRootReferenceValue($normalized, $field_item);
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'];