Created
June 1, 2020 02:36
-
-
Save acbramley/14e0193c110adecdc36be3bca77fff61 to your computer and use it in GitHub Desktop.
Limit bundles in jsonapi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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