|
<?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; |
|
} |
|
|
|
} |