You are here

protected function RelationshipItemNormalizer::buildSubContext in JSON:API 8

Builds the sub-context for the relationship include.

Parameters

array $context: The serialization context.

\Drupal\Core\Entity\EntityInterface $entity: The related entity.

string $host_field_name: The name of the field reference.

Return value

array The modified new context.

1 call to RelationshipItemNormalizer::buildSubContext()
RelationshipItemNormalizer::normalize in src/Normalizer/RelationshipItemNormalizer.php
This normalizer leaves JSON API normalizer land and enters the land of Drupal core's serialization system. That system was never designed with cacheability in mind, and hence bubbles cacheability out of band. This must catch it, and pass it to…

File

src/Normalizer/RelationshipItemNormalizer.php, line 90

Class

RelationshipItemNormalizer
Converts the Drupal entity reference item object to a JSON API structure.

Namespace

Drupal\jsonapi\Normalizer

Code

protected function buildSubContext(array $context, EntityInterface $entity, $host_field_name) {

  // Swap out the context for the context of the referenced resource.
  $context['resource_type'] = $this->resourceTypeRepository
    ->get($entity
    ->getEntityTypeId(), $entity
    ->bundle());

  // Since we're going one level down the only includes we need are the ones
  // that apply to this level as well.
  $include_candidates = array_filter($context['include'], function ($include) use ($host_field_name) {
    return strpos($include, $host_field_name . '.') === 0;
  });
  $context['include'] = array_map(function ($include) use ($host_field_name) {
    return str_replace($host_field_name . '.', '', $include);
  }, $include_candidates);
  $context['is_include_normalization'] = TRUE;
  return $context;
}