Skip to content

Instantly share code, notes, and snippets.

@acbramley
Created June 1, 2020 02:36
Show Gist options
  • Save acbramley/14e0193c110adecdc36be3bca77fff61 to your computer and use it in GitHub Desktop.
Save acbramley/14e0193c110adecdc36be3bca77fff61 to your computer and use it in GitHub Desktop.
Limit bundles in jsonapi
<?php
namespace Drupal\my_module\EventSubscriber;
use Drupal\jsonapi\ResourceType\ResourceTypeBuildEvents;
use Drupal\jsonapi\ResourceType\ResourceTypeBuildEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Event subscriber to disable all resource types except what we need.
*/
class ResourceTypeBuildEventSubscriber implements EventSubscriberInterface {
/**
* The list of allowed resources.
*
* @var array
*/
protected $allowedResources = [
'node--basic_page',
'node--article',
];
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
ResourceTypeBuildEvents::BUILD => [
['disableResourceType'],
],
];
}
/**
* Disables any resource types that aren't explicitly allowed.
*
* @param \Drupal\jsonapi\ResourceType\ResourceTypeBuildEvent $event
* The build event.
*/
public function disableResourceType(ResourceTypeBuildEvent $event) {
if (!in_array($event->getResourceTypeName(), $this->allowedResources, TRUE)) {
$event->disableResourceType();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment