public function RelationshipItem::__construct in JSON:API 8
Relationship item constructor.
Parameters
\Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository: The JSON API resource type repository.
\Drupal\Core\Entity\EntityInterface|null $target_entity: The entity this relationship points to, if any.
\Drupal\jsonapi\Normalizer\Relationship $parent: The parent of this item.
string $target_key: The key name of the target relationship.
array $metadata: The list of metadata associated with this relationship item value.
File
- src/
Normalizer/ RelationshipItem.php, line 64
Class
- RelationshipItem
- Value object representing a JSON API relationship item.
Namespace
Drupal\jsonapi\NormalizerCode
public function __construct(ResourceTypeRepositoryInterface $resource_type_repository, $target_entity, Relationship $parent, $target_key = 'target_id', array $metadata = []) {
assert($target_entity === NULL || $target_entity instanceof EntityInterface);
if ($target_entity === NULL) {
$host_entity = $parent
->getHostEntity();
$relatable_resource_types = $resource_type_repository
->get($host_entity
->getEntityTypeId(), $host_entity
->bundle())
->getRelatableResourceTypes()[$parent
->getPropertyName()];
if (count($relatable_resource_types) !== 1) {
throw new \RuntimeException('Relationships to virtual resources are possible only if a single resource type is relatable.');
}
$this->targetResourceType = reset($relatable_resource_types);
}
else {
$this->targetResourceType = $resource_type_repository
->get($target_entity
->getEntityTypeId(), $target_entity
->bundle());
}
$this->targetKey = $target_key;
$this->targetEntity = $target_entity;
$this->parent = $parent;
$this->metadata = $metadata;
}