FeaturedNodes.php in JSON:API Resources 8
File
tests/modules/jsonapi_resources_test/src/Resource/FeaturedNodes.php
View source
<?php
namespace Drupal\jsonapi_resources_test\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;
use Symfony\Component\Routing\Route;
final class FeaturedNodes extends EntityQueryResourceBase {
public function process(Request $request) : ResourceResponse {
$query = $this
->getEntityQuery('node')
->condition('status', NodeInterface::PUBLISHED)
->condition('promote', NodeInterface::PROMOTED);
$cacheability = new CacheableMetadata();
$paginator = $this
->getPaginatorForRequest($request);
$paginator
->applyToQuery($query, $cacheability);
$data = $this
->loadResourceObjectDataFromEntityQuery($query, $cacheability);
$pagination_links = $paginator
->getPaginationLinks($query, $cacheability);
return $this
->createJsonapiResponse($data, $request, 200, [], $pagination_links);
}
public function getRouteResourceTypes(Route $route, string $route_name) : array {
return $this
->getResourceTypesByEntityTypeId('node');
}
}
Classes
Name |
Description |
FeaturedNodes |
Processes a request for a collection of featured nodes. |