You are here

protected function RelationshipNormalizer::buildSubContext in JSON:API 8

Builds the sub-context for the relationship include.

@todo This is duplicated code from the reference item. Reuse code instead.

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.

See also

EntityReferenceItemNormalizer::buildSubContext()

File

src/Normalizer/RelationshipNormalizer.php, line 120

Class

RelationshipNormalizer
Normalizes a Relationship according to the JSON API specification.

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);
  return $context;
}