You are here

abstract class EntityQueryResourceBase in JSON:API Resources 8

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

Hierarchy

Expanded class hierarchy of EntityQueryResourceBase

5 files declare their use of EntityQueryResourceBase
AddComment.php in tests/modules/jsonapi_resources_test/src/Resource/AddComment.php
AddReminder.php in tests/modules/jsonapi_resources_test/src/Resource/AddReminder.php
AuthorArticles.php in tests/modules/jsonapi_resources_test/src/Resource/AuthorArticles.php
FeaturedNodes.php in tests/modules/jsonapi_resources_test/src/Resource/FeaturedNodes.php
JsonapiResourceClassResolver.php in src/Unstable/DependencyInjection/JsonapiResourceClassResolver.php

File

src/Resource/EntityQueryResourceBase.php, line 17

Namespace

Drupal\jsonapi_resources\Resource
View source
abstract class EntityQueryResourceBase extends EntityResourceBase {

  /**
   * The entity query executor utility.
   *
   * @var \Drupal\jsonapi_resources\Unstable\Entity\Query\CacheabilityCapturingExecutor
   */
  private $entityQueryExecutor;

  /**
   * Sets the cacheability capturing entity query executor.
   *
   * @param \Drupal\jsonapi_resources\Unstable\Entity\Query\CacheabilityCapturingExecutor $entity_query_executor
   *   The entity query executor utility.
   */
  public function setCacheabilityCapturingExecutor(CacheabilityCapturingExecutor $entity_query_executor) {
    $this->entityQueryExecutor = $entity_query_executor;
  }

  /**
   * Gets an entity query for the given entity type.
   *
   * @param string $entity_type_id
   *   The entity type ID for the entity query.
   *
   * @return \Drupal\Core\Entity\Query\QueryInterface
   *   An entity query.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  protected function getEntityQuery($entity_type_id) {
    return $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->getQuery();
  }

  /**
   * Gets an entity query paginator for the current request.
   *
   * Currently, this will always returns an OffsetLimitPaginator, but it's
   * possible that it may return other paginator types in the future. Such as a
   * cursor-based paginator.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request object.
   *
   * @return \Drupal\jsonapi_resources\Entity\Query\PaginatorInterface
   *   A paginator for the request.
   */
  protected function getPaginatorForRequest(Request $request) : PaginatorInterface {
    return OffsetLimitPaginator::create($request, $this->entityQueryExecutor);
  }

  /**
   * Finds entity resource object using an entity query.
   *
   * @param \Drupal\Core\Entity\Query\QueryInterface $entity_query
   *   The entity query object.
   * @param \Drupal\Core\Cache\CacheableMetadata $cacheable_metadata
   *   A CacheableMetadata object that will be used to capture any cacheability
   *   information generated while generating pagination links. The same object
   *   that is passed to this method should be added to the cacheability of the
   *   final response by the caller.
   * @param bool $load_latest_revisions
   *   (optional) Whether to load the latest revisions instead of the defaults.
   *   Defaults to FALSE.
   * @param bool $check_access
   *   (optional) Whether to check access on the loaded entities or not.
   *   Defaults to TRUE.
   *
   * @return \Drupal\jsonapi\JsonApiResource\ResourceObjectData
   *   The resource object data that was found and access checked.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   *   Thrown if the entity type doesn't exist.
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   *   Thrown if the storage handler couldn't be loaded.
   */
  protected function loadResourceObjectDataFromEntityQuery(QueryInterface $entity_query, CacheableMetadata $cacheable_metadata, $load_latest_revisions = FALSE, $check_access = TRUE) : ResourceObjectData {
    $entity_type_id = $entity_query
      ->getEntityTypeId();
    $results = $this->entityQueryExecutor
      ->executeQueryAndCaptureCacheability($entity_query, $cacheable_metadata);
    return $this
      ->loadResourceObjectsByEntityIds($entity_type_id, $results, $load_latest_revisions, $check_access);
  }

  /**
   * Loads and access checks entities loaded by ID as JSON:API resource objects.
   *
   * @param string $entity_type_id
   *   The entity type ID of the entities to load.
   * @param int[] $ids
   *   An array of entity IDs, keyed by revision ID if the entity type is
   *   revisionable.
   * @param bool $load_latest_revisions
   *   (optional) Whether to load the latest revisions instead of the defaults.
   *   Defaults to FALSE.
   * @param bool $check_access
   *   (optional) Whether to check access on the loaded entities or not.
   *   Defaults to TRUE.
   *
   * @return \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.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   *   Thrown if the entity type doesn't exist.
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   *   Thrown if the storage handler couldn't be loaded.
   */
  private function loadResourceObjectsByEntityIds($entity_type_id, array $ids, $load_latest_revisions = FALSE, $check_access = TRUE) : ResourceObjectData {
    $storage = $this->entityTypeManager
      ->getStorage($entity_type_id);
    if ($load_latest_revisions) {
      assert($storage instanceof RevisionableStorageInterface);
      $entities = $storage
        ->loadMultipleRevisions(array_keys($ids));
    }
    else {
      $entities = $storage
        ->loadMultiple($ids);
    }
    return $this
      ->createCollectionDataFromEntities($entities, $check_access);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityCreationTrait::modifyCreatedEntity protected function Modifies the created entity before it is saved. 2
EntityCreationTrait::processEntityCreation protected function Process the resource request.
EntityQueryResourceBase::$entityQueryExecutor private property The entity query executor utility.
EntityQueryResourceBase::getEntityQuery protected function Gets an entity query for the given entity type.
EntityQueryResourceBase::getPaginatorForRequest protected function Gets an entity query paginator for the current request.
EntityQueryResourceBase::loadResourceObjectDataFromEntityQuery protected function Finds entity resource object using an entity query.
EntityQueryResourceBase::loadResourceObjectsByEntityIds private function Loads and access checks entities loaded by ID as JSON:API resource objects.
EntityQueryResourceBase::setCacheabilityCapturingExecutor public function Sets the cacheability capturing entity query executor.
EntityResourceBase::$entityAccessChecker private property The JSON:API entity access checker.
EntityResourceBase::$entityTypeManager protected property The entity type manager.
EntityResourceBase::createCollectionDataFromEntities protected function Creates a JSON:API resource object from the given entity.
EntityResourceBase::createIndividualDataFromEntity protected function Creates a JSON:API resource object from the given entity.
EntityResourceBase::getResourceTypesByEntityTypeId protected function Get all resource types that represent variants of the given entity type ID.
EntityResourceBase::setEntityAccessChecker public function Sets the entity access checker.
EntityResourceBase::setEntityTypeManager public function Sets the entity type manager.
EntityValidationTrait::validate protected static function Verifies that an entity does not violate any validation constraints.
ResourceBase::$documentExtractor private property The document extractor.
ResourceBase::$resourceResponseFactory private property The resource response factory.
ResourceBase::$resourceTypeRepository protected property The resource type repository.
ResourceBase::createJsonapiResponse protected function Builds a response with the appropriate wrapped document.
ResourceBase::getDocumentFromRequest protected function Get the document from the request.
ResourceBase::getRouteResourceTypes public function 2
ResourceBase::setDocumentExtractor public function Sets the document extractor.
ResourceBase::setResourceResponseFactory public function Sets the resource response factory.
ResourceBase::setResourceTypeRepository public function Sets the resource type repository.
ResourceObjectToEntityMapperAwareTrait::$resourceObjectToEntityMapper private property The service which created an entity from a resource object.
ResourceObjectToEntityMapperAwareTrait::setResourceObjectToEntityMapper public function