Unverified Commit b0160041 authored by e0ipso's avatar e0ipso Committed by e0ipso

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
......@@ -42,4 +42,6 @@ services:
jsonapi_extras.entity.to_jsonapi:
class: Drupal\jsonapi_extras\EntityToJsonApi
arguments: ['@http_kernel']
arguments:
- '@http_kernel'
- '@jsonapi.resource_type.repository'
......@@ -5,6 +5,7 @@ namespace Drupal\jsonapi_extras;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
......@@ -22,14 +23,27 @@ class EntityToJsonApi {
*/
protected $httpKernel;
/**
* The JSON:API Resource Type Repository.
*
* @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface
*/
protected $resourceTypeRepository;
/**
* EntityToJsonApi constructor.
*
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $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->resourceTypeRepository = $resource_type_repository;
}
/**
......@@ -42,10 +56,17 @@ class EntityToJsonApi {
*
* @return string
* The raw JSON string of the requested resource.
*
* @throws \Exception
*/
public function serialize(EntityInterface $entity, array $includes = []) {
$route_name = sprintf('jsonapi.%s--%s.individual', $entity->getEntityTypeId(), $entity->bundle());
$jsonapi_url = Url::fromRoute($route_name, ['entity' => $entity->uuid()])->toString(TRUE)->getGeneratedUrl();
$resource_type_name = $this->resourceTypeRepository
->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 = [];
if ($includes) {
$query = ['include' => implode(',', $includes)];
......@@ -65,6 +86,8 @@ class EntityToJsonApi {
*
* @return array
* The JSON structure of the requested resource.
*
* @throws \Exception
*/
public function normalize(EntityInterface $entity, array $includes = []) {
return Json::decode($this->serialize($entity, $includes));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment