3 namespace Drupal\hal\Normalizer;
5 use Drupal\Core\Entity\FieldableEntityInterface;
6 use Drupal\hal\LinkManager\LinkManagerInterface;
7 use Drupal\serialization\EntityResolver\EntityResolverInterface;
8 use Drupal\serialization\EntityResolver\UuidReferenceInterface;
11 * Converts the Drupal entity reference item object to HAL array structure.
13 class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidReferenceInterface {
16 * The interface or class that this Normalizer supports.
20 protected $supportedInterfaceOrClass = 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem';
23 * The hypermedia link manager.
25 * @var \Drupal\hal\LinkManager\LinkManagerInterface
27 protected $linkManager;
30 * The entity resolver.
32 * @var \Drupal\serialization\EntityResolver\EntityResolverInterface
34 protected $entityResolver;
37 * Constructs an EntityReferenceItemNormalizer object.
39 * @param \Drupal\hal\LinkManager\LinkManagerInterface $link_manager
40 * The hypermedia link manager.
41 * @param \Drupal\serialization\EntityResolver\EntityResolverInterface $entity_Resolver
42 * The entity resolver.
44 public function __construct(LinkManagerInterface $link_manager, EntityResolverInterface $entity_Resolver) {
45 $this->linkManager = $link_manager;
46 $this->entityResolver = $entity_Resolver;
52 public function normalize($field_item, $format = NULL, array $context = []) {
53 /** @var $field_item \Drupal\Core\Field\FieldItemInterface */
54 $target_entity = $field_item->get('entity')->getValue();
56 // If this is not a content entity, let the parent implementation handle it,
57 // only content entities are supported as embedded resources.
58 if (!($target_entity instanceof FieldableEntityInterface)) {
59 return parent::normalize($field_item, $format, $context);
62 // If the parent entity passed in a langcode, unset it before normalizing
63 // the target entity. Otherwise, untranslatable fields of the target entity
64 // will include the langcode.
65 $langcode = isset($context['langcode']) ? $context['langcode'] : NULL;
66 unset($context['langcode']);
67 $context['included_fields'] = ['uuid'];
69 // Normalize the target entity.
70 $embedded = $this->serializer->normalize($target_entity, $format, $context);
71 $link = $embedded['_links']['self'];
72 // If the field is translatable, add the langcode to the link relation
73 // object. This does not indicate the language of the target entity.
75 $embedded['lang'] = $link['lang'] = $langcode;
78 // The returned structure will be recursively merged into the normalized
79 // entity so that the items are properly added to the _links and _embedded
81 $field_name = $field_item->getParent()->getName();
82 $entity = $field_item->getEntity();
83 $field_uri = $this->linkManager->getRelationUri($entity->getEntityTypeId(), $entity->bundle(), $field_name, $context);
86 $field_uri => [$link],
89 $field_uri => [$embedded],
97 protected function constructValue($data, $context) {
98 $field_item = $context['target_instance'];
99 $field_definition = $field_item->getFieldDefinition();
100 $target_type = $field_definition->getSetting('target_type');
101 $id = $this->entityResolver->resolve($this, $data, $target_type);
103 return ['target_id' => $id];
111 public function getUuid($data) {
112 if (isset($data['uuid'])) {
113 $uuid = $data['uuid'];
114 // The value may be a nested array like $uuid[0]['value'].
115 if (is_array($uuid) && isset($uuid[0]['value'])) {
116 $uuid = $uuid[0]['value'];