I'm trying to make a custom JSON:API resource for flaggings using the JSON:API Resources module.
Right now, I simply want to return all the flaggings of a given bundle with an EntityQuery. (Later, I will customize the EntityQuery, which is why I am need to go beyond what JSON:API provides by default.)
My code is based on the module's example code.
MYMODULE/src/Resource/MyQueue.php
/**
* Processes a request for a collection containing a user's article nodes.
*
* @internal
*/
class MyQueue extends EntityQueryResourceBase {
/**
* Process the resource request.
*
* Code example:
* https://git.drupalcode.org/project/jsonapi_resources/tree/8.x-1.x/tests/modules/jsonapi_resources_test/src/Resource.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request.
*
* @return \Drupal\jsonapi\ResourceResponse
* The response.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function process(Request $request): ResourceResponse {
$cacheability = new CacheableMetadata();
$entity_type_id = 'flagging';
$entity_type_bundle = 'MY_FLAGGING_TYPE';
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
$entity_query = $this->getEntityQuery($entity_type_id)
->condition($entity_type->getKey('bundle'), $entity_type_bundle);
$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;
}
}
With this code, I'm getting the following error:
LogicException: This resource attempted to serve data that contains unsupported resource types.
I don't understand why I'm getting this error, or what it means.
- If I set
$entity_type_id
tonode
and$entity_type_bundle
topage
, everything works as expected. - If I set the
$entity_type_id
to 'custom_content_type`, one of the content types I configured in the admin UI, it doesn't work; I get the same error. - I tried to set
$entity_type_bundle
to all three flagging bundles on my site; none of them work. - The
$entity_query
is correct for the flaggings. I can do$entity_query->count->execute()
and I get the correct count. - I am logged in as an administrator with all permissions.
- I can access the jsonapi in my browser as an admin at
/jsonapi/flagging/MY_FLAGGING_TYPE
and I see the flaggings as I expect.