public function AuthorArticles::process in JSON:API Resources 8
Process the resource request.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The request.
\Drupal\user\UserInterface $user: The user.
Return value
\Drupal\jsonapi\ResourceResponse The response.
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
File
- tests/
modules/ jsonapi_resources_test/ src/ Resource/ AuthorArticles.php, line 33
Class
- AuthorArticles
- Processes a request for a collection containing a user's article nodes.
Namespace
Drupal\jsonapi_resources_test\ResourceCode
public function process(Request $request, UserInterface $user) : ResourceResponse {
// Force the author to be included.
$include = $request->query
->get('include');
$request->query
->set('include', $include . (empty($include) ? '' : ',') . 'uid');
$cacheability = new CacheableMetadata();
$entity_type = $this->entityTypeManager
->getDefinition('node');
$entity_query = $this
->getEntityQuery('node')
->condition($entity_type
->getKey('bundle'), 'article')
->condition($entity_type
->getKey('status'), NodeInterface::PUBLISHED)
->condition($entity_type
->getKey('uid'), $user
->id());
$cacheability
->addCacheContexts([
'url.path',
]);
$paginator = $this
->getPaginatorForRequest($request);
$paginator
->applyToQuery($entity_query, $cacheability);
$data = $this
->loadResourceObjectDataFromEntityQuery($entity_query, $cacheability);
$pagination_links = $paginator
->getPaginationLinks($entity_query, $cacheability, TRUE);
$response = $this
->createJsonapiResponse($data, $request, 200, [], $pagination_links);
$response
->addCacheableDependency($cacheability);
return $response;
}