Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 296363

Allow to include a total count of items to collection responses

$
0
0

The #2942426: [DISCUSSION] Remove query counting and collection counts? explains that without a JSON:API Extras it's *almost* impossible to get a total number of items for a collection request within meta.count. This caused by the "static" code of Drupal\jsonapi\ResourceType\ResourceType::includeCount():

public function includeCount() {
  // By default, do not return counts in collection queries.
  return FALSE;
}

Proposed resolution

  1. Add string|null $totalCountMetaName to the Drupal\jsonapi\ResourceType\ResourceType.
  2. Add string|null $total_count_meta_name = NULL as the argument to Drupal\jsonapi\ResourceType\ResourceType::__construct() and assign it to $this->totalCountMetaName.
  3. Add logic to Drupal\jsonapi\ResourceType\ResourceType::includeCount() and return $this->totalCountMetaName !== NULL, so the total count is included for collection responses if the meta name is provided.
  4. Introduce Drupal\jsonapi\ResourceType\ResourceType::getTotalCountMetaName(): ?string that returns $this->totalCountMetaName or 'count' if the Drupal\jsonapi\ResourceType\ResourceType::includeCount() method is overridden.
  5. In the Drupal\jsonapi\Controller\EntityResource::respondWithCollection() use $meta[$resource_type->getTotalCountMetaName()] instead of $meta['count'].
  6. In the Drupal\jsonapi\ResourceType\ResourceTypeBuildEvent, introduce string|null $totalCountMetaName, setTotalCountMetaName(?string $name): void, and getTotalCountMetaName(): ?string that will allow changing the resource definition at a build time.
  7. In the Drupal\jsonapi\ResourceType\ResourceTypeRepository::createResourceType(), query $event->getTotalCountMetaName() for non-internal resource types and pass the value to new ResourceType.

Viewing all articles
Browse latest Browse all 296363

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>