diff --git a/src/Controller/EntityResource.php b/src/Controller/EntityResource.php index ba0aec6..217d8ea 100644 --- a/src/Controller/EntityResource.php +++ b/src/Controller/EntityResource.php @@ -488,7 +488,10 @@ class EntityResource { public function getRelationship(ResourceType $resource_type, FieldableEntityInterface $entity, $related, Request $request, $response_code = 200) { /* @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $field_list */ $field_list = $entity->get($resource_type->getInternalName($related)); - $response = $this->buildWrappedResponse($field_list, $request, $this->getIncludes($request, $entity), $response_code); + // Access will have already been checked by the RelationshipFieldAccess + // service, so we don't need to call ::getAccessCheckedResourceObject(). + $resource_object = new ResourceObject($resource_type, $entity); + $response = $this->buildWrappedResponse($field_list, $request, $this->getIncludes($request, $resource_object), $response_code); // Add the host entity as a cacheable dependency. $response->addCacheableDependency($entity); return $response; diff --git a/src/IncludeResolver.php b/src/IncludeResolver.php index fcf4973..d290ca4 100644 --- a/src/IncludeResolver.php +++ b/src/IncludeResolver.php @@ -59,7 +59,7 @@ class IncludeResolver { public function resolve(ResourceType $base_resource_type, $data, $include_parameter, $related_field = NULL) { assert($data instanceof ResourceIdentifierInterface || $data instanceof EntityCollection); // Map a single entity into an EntityCollection. - $entity_collection = $data instanceof ResourceObject ? new EntityCollection([$data], 1) : $data; + $entity_collection = $data instanceof ResourceIdentifierInterface ? new EntityCollection([$data], 1) : $data; $include_tree = static::toIncludeTree($base_resource_type, $include_parameter, $related_field); return EntityCollection::deduplicate($this->resolveIncludeTree($include_tree, $entity_collection)); } diff --git a/src/JsonApiResource/EntityCollection.php b/src/JsonApiResource/EntityCollection.php index 39fa89a..8f67f1e 100644 --- a/src/JsonApiResource/EntityCollection.php +++ b/src/JsonApiResource/EntityCollection.php @@ -4,7 +4,6 @@ namespace Drupal\jsonapi\JsonApiResource; use Drupal\Component\Assertion\Inspector; use Drupal\jsonapi\Exception\EntityAccessDeniedHttpException; -use Drupal\jsonapi\LabelOnlyEntity; /** * Wrapper to normalize collections with multiple entities. @@ -44,7 +43,7 @@ class EntityCollection implements \IteratorAggregate, \Countable { /** * Instantiates a EntityCollection object. * - * @param \Drupal\Core\Entity\EntityInterface|null[]|false[] $resource_objects + * @param \Drupal\jsonapi\JsonApiResource\ResourceIdentifierInterface[]|null[]|false[] $resource_objects * The resources for the collection. * @param int $cardinality * The number of resources that this collection may contain. Related @@ -56,9 +55,7 @@ class EntityCollection implements \IteratorAggregate, \Countable { assert(Inspector::assertAll(function ($entity) { return $entity === NULL || $entity === FALSE - || $entity instanceof ResourceObject - || $entity instanceof LabelOnlyEntity - || $entity instanceof EntityAccessDeniedHttpException; + || $entity instanceof ResourceIdentifierInterface; }, $resource_objects)); assert($cardinality >= -1 && $cardinality !== 0, 'Cardinality must be -1 for unlimited cardinality or a positive integer.'); assert($cardinality === -1 || count($resource_objects) <= $cardinality, 'If cardinality is not unlimited, the number of given resources must not exceed the cardinality of the collection.');