Skip to content

Instantly share code, notes, and snippets.

@WengerK
Created November 7, 2022 14:08
Show Gist options
  • Save WengerK/99fdb2880ee0198ed4fe7d490f88bf7b to your computer and use it in GitHub Desktop.
Save WengerK/99fdb2880ee0198ed4fe7d490f88bf7b to your computer and use it in GitHub Desktop.
How to create a custom endpoint using Drupal JSON:API

Article Ressources - How to create a custom endpoint using Drupal JSON:API

This is the Gist repository for my article How to create a custom endpoint using Drupal JSON:API.

Be aware that this article has been wrote for the Blog of Antistatique — Web Agency in Lausanne, Switzerland. A place where I work as Backend Web Developer.

Feel free to read it the full article on Medium or check it out on Antistatique.

<?php
namespace Drupal\my_custom_resource\Resource;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\jsonapi\ResourceResponse;
use Drupal\jsonapi_resources\Resource\EntityQueryResourceBase;
use Drupal\node\NodeInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Processes a request for a collection containing featured articles nodes.
*/
class FeaturedArticlesResource extends EntityQueryResourceBase {
/**
* Process the resource request.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*
* @return \Drupal\jsonapi\ResourceResponse
* The response.
*/
public function process(Request $request): ResourceResponse {
$cacheability = new CacheableMetadata();
/** @var \Drupal\Core\Entity\ContentEntityType $entity_type */
$entity_type = $this->entityTypeManager->getDefinition('node');
/** @var string $bundle_field */
$bundle_field = $entity_type->getKey('bundle');
/** @var string $status_field */
$status_field = $entity_type->getKey('status');
$entity_query = $this->getEntityQuery('node')
->condition($bundle_field, 'article')
->condition('promote', TRUE)
->condition($status_field, NodeInterface::PUBLISHED);
$cacheability->addCacheContexts(['url']);
$paginator = $this->getPaginatorForRequest($request);
$paginator->applyToQuery($entity_query, $cacheability);
$data = $this->loadResourceObjectDataFromEntityQuery($entity_query, $cacheability);
$pagination_links = $paginator->getPaginationLinks($entity_query, $cacheability, TRUE);
/** @var \Drupal\jsonapi\CacheableResourceResponse $response */
$response = $this->createJsonapiResponse($data, $request, 200, [], $pagination_links);
$response->addCacheableDependency($cacheability);
return $response;
}
}
# Defines a route for a collection containing featured articles nodes.
my_custom_resource.jsonapi_resources.featured_articles:
# %jsonapi% is a placeholder for the JSON:API base path, which can be
# configured in a site's services.yml file.
path: '/%jsonapi%/articles/featured'
defaults:
# Every JSON:API resource route must declare a _jsonapi_resource. The
# value can either be a class or a service ID. Unlike the _controller
# route default, it is not possible to declare a method name to be called.
_jsonapi_resource: Drupal\my_custom_resource\Resource\FeaturedArticlesResource
_jsonapi_resource_types: ['node--article']
requirements:
_permission: 'access content'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment