You are here

protected function EntityResourceBase::createCollectionDataFromEntities in JSON:API Resources 8

Creates a JSON:API resource object from the given entity.

Parameters

\Drupal\Core\Entity\EntityInterface[] $entities: The entities from which to create a resource objects.

bool $check_access: (optional) Whether to check access on the entities or not. Defaults to TRUE. Careful consideration should be made whenever passing FALSE. There are many subtle access checks to consider beyond the entity 'view' operation. For example, the 'view label' operation and access to the loaded revision, etc.

Return value

\Drupal\jsonapi\JsonApiResource\ResourceObjectData A ResourceObjectData object containing a resource object with unlimited cardinality. This corresponds to a top-level document's primary data on a collection response.

2 calls to EntityResourceBase::createCollectionDataFromEntities()
EntityQueryResourceBase::loadResourceObjectsByEntityIds in src/Resource/EntityQueryResourceBase.php
Loads and access checks entities loaded by ID as JSON:API resource objects.
EntityResourceBase::createIndividualDataFromEntity in src/Resource/EntityResourceBase.php
Creates a JSON:API resource object from the given entity.

File

src/Resource/EntityResourceBase.php, line 93

Class

EntityResourceBase
Defines basic functionality for an entity-oriented JSON:API Resource.

Namespace

Drupal\jsonapi_resources\Resource

Code

protected function createCollectionDataFromEntities(array $entities, $check_access = TRUE) : ResourceObjectData {
  if (!$check_access) {
    throw new ResourceImplementationException('It is not yet allowed to create entity-oriented resources that do not check entity access. If this is a requirement for your project, please open a feature request in the issue queue: https://www.drupal.org/project/issues/jsonapi_resources');
  }
  $resource_objects = [];
  foreach ($entities as $entity) {
    $resource_objects[$entity
      ->id()] = $check_access ? $this->entityAccessChecker
      ->getAccessCheckedResourceObject($entity) : ResourceObject::createFromEntity($this->resourceTypeRepository
      ->get($entity
      ->getEntityTypeId(), $entity
      ->bundle()), $entity);
  }
  return new ResourceObjectData(array_values($resource_objects));
}