You are here

public function EntityNormalizerValue::__construct in JSON:API 8

Instantiate a EntityNormalizerValue object.

Parameters

FieldNormalizerValueInterface[] $values: The normalized result.

array $context: The context for the normalizer.

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

array $link_context: All the objects and variables needed to generate the links for this relationship.

File

src/Normalizer/Value/EntityNormalizerValue.php, line 67

Class

EntityNormalizerValue
Helps normalize entities in compliance with the JSON API spec.

Namespace

Drupal\jsonapi\Normalizer\Value

Code

public function __construct(array $values, array $context, EntityInterface $entity, array $link_context) {
  $this
    ->setCacheability(static::mergeCacheableDependencies(array_merge([
    $entity,
  ], $values)));

  // Gather includes from all normalizer values, before filtering away null
  // and include-only normalizer values.
  $this->includes = array_map(function ($value) {
    return $value
      ->getIncludes();
  }, $values);
  $this->values = array_filter($values, function ($value) {
    return !($value instanceof NullFieldNormalizerValue || $value instanceof IncludeOnlyRelationshipNormalizerValue);
  });
  $this->context = $context;
  $this->entity = $entity;
  $this->linkManager = $link_context['link_manager'];

  // Flatten the includes.
  $this->includes = array_reduce($this->includes, function ($carry, $includes) {
    return array_merge($carry, $includes);
  }, []);

  // Filter the empty values.
  $this->includes = array_filter($this->includes);
}