Customizing Resources
Some sites may not wish to expose certain JSON:API resource types or fields or may wish to rename fields to provide a cleaner interface.
To allow these alterations, JSON:API dispatches a ResourceTypeBuildEvent
event object using the event name ResourceTypeBuildEvents::BUILD
. Please refer to subscribe to and dispatch events on how to use these events.
Subscribers can call the following methods:
ResourceTypeBuildEvent::disableResourceType()
to prevent a resource type from being accessed in any way via JSON:API.ResourceTypeBuildEvent::disableField()
to prevent a field from being accessed in any way via JSON:API.ResourceTypeBuildEvent::setPublicFieldName()
to set a field alias so that JSON:API does not expose the internal Drupal field name.ResourceTypeBuildEvent::setResourceTypeName()
to set a resource alias so that JSON:API does not expose the internal Drupal resource name.
Any module, custom or contrib, can implement a subscriber for this event. That means a module for a specific site can disable resource types and also that modules providing their own entity types can disable their own JSON:API representations.
Example
Below is an example EventSubscriber
that shows all the possible actions you can take.
<?php
namespace Drupal\modules\custom\resource_types\EventSubscriber;
use Drupal\jsonapi\ResourceType\ResourceTypeBuildEvents;
use Drupal\jsonapi\ResourceType\ResourceTypeBuildEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Event subscriber to change some resource types.
*/
class ResourceTypeBuildEventSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
ResourceTypeBuildEvents::BUILD => [
['disableResourceType'],
['aliasResourceTypeFields'],
['disableResourceTypeFields'],
['renameResourceType'],
],
];
}
/**
* Disables node/page resource type.
*
* @param \Drupal\jsonapi\ResourceType\ResourceTypeBuildEvent $event
* The build event.
*/
public function disableResourceType(ResourceTypeBuildEvent $event) {
if ($event->getResourceTypeName() === 'node--page') {
$event->disableResourceType();
}
}
/**
* Aliases the body field to content.
*
* @param \Drupal\jsonapi\ResourceType\ResourceTypeBuildEvent $event
* The build event.
*/
public function aliasResourceTypeFields(ResourceTypeBuildEvent $event) {
if ($event->getResourceTypeName() === 'node--article') {
foreach ($event->getFields() as $field) {
if ($field->getInternalName() === 'body') {
$event->setPublicFieldName($field, 'content');
}
}
}
}
/**
* Disables the sticky field on node--article.
*
* @param \Drupal\jsonapi\ResourceType\ResourceTypeBuildEvent $event
* The build event.
*/
public function disableResourceTypeFields(ResourceTypeBuildEvent $event) {
if ($event->getResourceTypeName() === 'node--article') {
foreach ($event->getFields() as $field) {
if ($field->getInternalName() === 'sticky') {
$event->disableField($field);
}
}
}
}
/**
* Renames node--article to article, exposing the resource as /jsonapi/article
*
* @param \Drupal\jsonapi\ResourceType\ResourceTypeBuildEvent $event
* The build event.
*/
public function renameResourceType(ResourceTypeBuildEvent $event) {
if ($event->getResourceTypeName() === 'node--article') {
$event->setResourceTypeName('article');
}
}
}
JSON:API Extras
Some of this functionality is also supplied in JSON:API Extras. This module tries to use the core functionality where possible but supplies an interface and some extra features like:
- Altering the basepath to the API.
- Add enhancers to fields.
- Disable resources by default.
After enabling the module you can visit /admin/config/services/jsonapi to see a list of all configuration and content exposed by JSON:API.
Article from Drupal Documentation.