Skip to content
Snippets Groups Projects
Unverified Commit b0160041 authored by Mateu Aguiló Bosch's avatar Mateu Aguiló Bosch Committed by Mateu Aguiló Bosch
Browse files

Issue #3049221 by e0ipso, logickal, Wim Leers: EntityToJsonapi service does...

Issue #3049221 by e0ipso, logickal, Wim Leers: EntityToJsonapi service does not respect overridden routes
parent 0603467b
No related branches found
No related tags found
No related merge requests found
...@@ -42,4 +42,6 @@ services: ...@@ -42,4 +42,6 @@ services:
jsonapi_extras.entity.to_jsonapi: jsonapi_extras.entity.to_jsonapi:
class: Drupal\jsonapi_extras\EntityToJsonApi class: Drupal\jsonapi_extras\EntityToJsonApi
arguments: ['@http_kernel'] arguments:
- '@http_kernel'
- '@jsonapi.resource_type.repository'
...@@ -5,6 +5,7 @@ namespace Drupal\jsonapi_extras; ...@@ -5,6 +5,7 @@ namespace Drupal\jsonapi_extras;
use Drupal\Component\Serialization\Json; use Drupal\Component\Serialization\Json;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\HttpKernelInterface;
...@@ -22,14 +23,27 @@ class EntityToJsonApi { ...@@ -22,14 +23,27 @@ class EntityToJsonApi {
*/ */
protected $httpKernel; protected $httpKernel;
/**
* The JSON:API Resource Type Repository.
*
* @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface
*/
protected $resourceTypeRepository;
/** /**
* EntityToJsonApi constructor. * EntityToJsonApi constructor.
* *
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
* The HTTP kernel. * The HTTP kernel.
* @param \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository
* The resource type repository.
*/ */
public function __construct(HttpKernelInterface $http_kernel) { public function __construct(
HttpKernelInterface $http_kernel,
ResourceTypeRepositoryInterface $resource_type_repository
) {
$this->httpKernel = $http_kernel; $this->httpKernel = $http_kernel;
$this->resourceTypeRepository = $resource_type_repository;
} }
/** /**
...@@ -42,10 +56,17 @@ class EntityToJsonApi { ...@@ -42,10 +56,17 @@ class EntityToJsonApi {
* *
* @return string * @return string
* The raw JSON string of the requested resource. * The raw JSON string of the requested resource.
*
* @throws \Exception
*/ */
public function serialize(EntityInterface $entity, array $includes = []) { public function serialize(EntityInterface $entity, array $includes = []) {
$route_name = sprintf('jsonapi.%s--%s.individual', $entity->getEntityTypeId(), $entity->bundle()); $resource_type_name = $this->resourceTypeRepository
$jsonapi_url = Url::fromRoute($route_name, ['entity' => $entity->uuid()])->toString(TRUE)->getGeneratedUrl(); ->get($entity->getEntityTypeId(), $entity->bundle())
->getTypeName();
$route_name = sprintf('jsonapi.%s.individual', $resource_type_name);
$jsonapi_url = Url::fromRoute($route_name, ['entity' => $entity->uuid()])
->toString(TRUE)
->getGeneratedUrl();
$query = []; $query = [];
if ($includes) { if ($includes) {
$query = ['include' => implode(',', $includes)]; $query = ['include' => implode(',', $includes)];
...@@ -65,6 +86,8 @@ class EntityToJsonApi { ...@@ -65,6 +86,8 @@ class EntityToJsonApi {
* *
* @return array * @return array
* The JSON structure of the requested resource. * The JSON structure of the requested resource.
*
* @throws \Exception
*/ */
public function normalize(EntityInterface $entity, array $includes = []) { public function normalize(EntityInterface $entity, array $includes = []) {
return Json::decode($this->serialize($entity, $includes)); return Json::decode($this->serialize($entity, $includes));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment