You are here

protected function MenuLinkContentNormalizer::embedEntity in Better Normalizers 8

Embeds an entity in the normalized data.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity being serialized.

string $format: The serialization format.

array $context: Serializer context.

\Drupal\Core\Entity\EntityInterface $target_entity: Entity being embedded.

array $normalized: Current normalized values.

string $embedded_field_name: Field name to embed the entity using.

Return value

array Updated normalized values.

1 call to MenuLinkContentNormalizer::embedEntity()
MenuLinkContentNormalizer::normalize in src/Normalizer/MenuLinkContentNormalizer.php
Normalizes an object into a set of arrays/scalars.

File

src/Normalizer/MenuLinkContentNormalizer.php, line 153

Class

MenuLinkContentNormalizer
A normalizer to handle menu-link content links to entities.

Namespace

Drupal\better_normalizers\Normalizer

Code

protected function embedEntity(EntityInterface $entity, $format, array $context, EntityInterface $target_entity, array $normalized, $embedded_field_name) {

  // If the parent entity passed in a langcode, unset it before
  // normalizing the target entity. Otherwise, untranslatable fields
  // of the target entity will include the langcode.
  $langcode = isset($context['langcode']) ? $context['langcode'] : NULL;
  unset($context['langcode']);
  $context['included_fields'] = [
    'uuid',
  ];

  // Normalize the target entity.
  $embedded = $this->serializer
    ->normalize($target_entity, $format, $context);
  $link = $embedded['_links']['self'];

  // If the field is translatable, add the langcode to the link
  // relation object. This does not indicate the language of the
  // target entity.
  if ($langcode) {
    $embedded['lang'] = $link['lang'] = $langcode;
  }

  // The returned structure will be recursively merged into the
  // normalized entity so that the items are properly added to the
  // _links and _embedded objects.
  $embedded_field_uri = $this->linkManager
    ->getRelationUri($entity
    ->getEntityTypeId(), $entity
    ->bundle(), $embedded_field_name, $context);
  $normalized['_links'][$embedded_field_uri] = [
    $link,
  ];
  $normalized['_embedded'][$embedded_field_uri] = [
    $embedded,
  ];
  return $normalized;
}