Skip to content
Snippets Groups Projects
Commit 396e8cc4 authored by Jürgen Haas's avatar Jürgen Haas
Browse files

Issue #3330178 by jurgenhaas: Code style cleanup

parent a3fc9220
No related branches found
No related tags found
No related merge requests found
......@@ -3,10 +3,13 @@
namespace Drupal\json_api_book\EventSubscriber;
use Drupal\book\BookManagerInterface;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\jsonapi\Routing\Routes;
use Drupal\node\Entity\Node;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
......@@ -51,14 +54,29 @@ class ResponseSubscriber implements EventSubscriberInterface {
*/
protected array $loadedUrls = [];
/**
* The node storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface|null
*/
protected ?EntityStorageInterface $nodeStorage = NULL;
/**
* ResponseSubscriber constructor.
*
* @param \Drupal\book\BookManagerInterface $bookManager
* The book manager service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
*/
public function __construct(BookManagerInterface $bookManager) {
public function __construct(BookManagerInterface $bookManager, EntityTypeManagerInterface $entityTypeManager) {
$this->bookManager = $bookManager;
try {
$this->nodeStorage = $entityTypeManager->getStorage('node');
}
catch (InvalidPluginDefinitionException | PluginNotFoundException $e) {
// This is just theoretical and should never happen.
}
}
/**
......@@ -89,15 +107,17 @@ class ResponseSubscriber implements EventSubscriberInterface {
*
* @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
* The filter event.
*
* @throws \JsonException
*/
public function onResponse(ResponseEvent $event): void {
if (!$this->routeMatch->getRouteObject()) {
if ($this->nodeStorage === NULL || !$this->routeMatch->getRouteObject()) {
return;
}
if ($this->routeMatch->getRouteName() === 'jsonapi.resource_list' || Routes::isJsonApiRequest($this->routeMatch->getRouteObject()->getDefaults())) {
$response = $event->getResponse();
$content = $response->getContent();
$jsonapi_response = json_decode($content, TRUE);
$jsonapi_response = json_decode($content, TRUE, 512, JSON_THROW_ON_ERROR);
if (!is_array($jsonapi_response)) {
return;
}
......@@ -111,7 +131,7 @@ class ResponseSubscriber implements EventSubscriberInterface {
$this->findBookDefinition($jsonapi_response['data'][$key]['attributes']);
}
}
$response->setContent(json_encode($jsonapi_response));
$response->setContent(json_encode($jsonapi_response, JSON_THROW_ON_ERROR));
}
}
......@@ -149,9 +169,9 @@ class ResponseSubscriber implements EventSubscriberInterface {
foreach ($references as $reference) {
$refId = $this->loadedBookDefinitions[$nid][$reference];
if (!isset($this->loadedUUIDs[$refId])) {
/** @var \Drupal\node\NodeInterface $node */
$node = Node::load($refId);
if (!$node) {
/** @var \Drupal\node\NodeInterface|null $node */
$node = $this->nodeStorage->load($refId);
if ($node === NULL) {
continue;
}
$this->loadedUUIDs[$refId] = $node->uuid();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment