diff --git a/README.md b/README.md index 24bfdf3..76c9c2d 100644 --- a/README.md +++ b/README.md @@ -34,9 +34,9 @@ The list of endpoints then looks like the following: It is also possible to obtain the JSON representation of a supported entity: // For a given $entity object. - $json_output = \Drupal::service('jsonapi.entity.to_json', ['resource' => $entity])->json(); + $json_output = \Drupal::service('jsonapi.entity.to_json')->json($entity); Should it be needed, the raw string itself can be obtained: // For a given $entity object. - $json_output = \Drupal::service('jsonapi.entity.to_json', ['resource' => $entity])->raw(); + $raw_output = \Drupal::service('jsonapi.entity.to_json')->raw($entity); diff --git a/src/Controller/ToJson.php b/src/Controller/ToJson.php index 6ea207f..a41c275 100644 --- a/src/Controller/ToJson.php +++ b/src/Controller/ToJson.php @@ -2,50 +2,61 @@ namespace Drupal\jsonapi\Controller; -use Drupal\Component\Serialization\Json; -use Drupal\Core\Cache\CacheableMetadata; -use Drupal\Core\Controller\ControllerBase; -use Drupal\Core\Entity\EntityInterface; +use \Drupal\Component\Serialization\Json; +use \Drupal\Core\Controller\ControllerBase; +use \Drupal\Core\Entity\EntityInterface; +use \Symfony\Component\HttpFoundation\Request; +use \Symfony\Component\HttpKernel\HttpKernelInterface; /** * Provide JSON output for a given object. */ class ToJson extends ControllerBase { - /** @var object */ - // protected $entity; - - /** - * {@inheritDoc} - */ - // public function __construct(EntityInterface $entity) { - // $this->entity = $entity; - // } - /** * Return the requested entity as a JSON object. * - * @return Drupal\Component\Serialization\Json + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity to generate the JSON from. + * @param array $options + * Optional arguments to pass to the HTTP Kernel request. + * + * @return \Drupal\Component\Serialization\Json * The requested resource in JSON form. */ - public function json($entity) { - return Json::decode($this->raw($entity)); + public function json(EntityInterface $entity, array $options = []) { + return Json::decode($this->raw($entity, $options)); } /** * Return the requested entity as a raw string. * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity to generate the JSON from. + * @param array $options + * Optional arguments to pass to the HTTP Kernel request. + * * @return string * The raw JSON string of the requested resource. */ - public function raw($entity) { - $format = 'api_json'; - $context = [ - 'account' => \Drupal::currentUser(), - 'cacheable_metadata' => new CacheableMetadata(), - ]; - return \Drupal::service('serializer.normalizer.entity.jsonapi') - ->normalize($entity, $format, $context) - ->rasterizeValue(); + public function raw(EntityInterface $entity, array $options = []) { + $resource = "{$entity->getEntityTypeId()}/{$entity->bundle()}/{$entity->uuid()}"; + + /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */ + $kernel = \Drupal::service('http_kernel.basic'); + + $request = Request::create('/jsonapi/' . $resource, 'GET', $options + ['_format' => 'api_json']); + + // @todo Drop this hack when https://www.drupal.org/node/2613044 is done. + $previous_request = \Drupal::requestStack()->getCurrentRequest(); + + // @todo Find a nicer implementation with maybe less layers involved. + $response = $kernel->handle($request, HttpKernelInterface::SUB_REQUEST, TRUE); + + \Drupal::requestStack()->push($previous_request); + + // @todo How to take into account cacheability metadata. + return $response->getContent(); } + } diff --git a/tests/src/Functional/JsonApiFunctionalToJsonTest.php b/tests/src/Functional/JsonApiFunctionalToJsonTest.php index 47d7b3b..b783091 100644 --- a/tests/src/Functional/JsonApiFunctionalToJsonTest.php +++ b/tests/src/Functional/JsonApiFunctionalToJsonTest.php @@ -30,10 +30,11 @@ protected function setUp() { parent::setUp(); drupal_flush_all_caches(); + $this->drupalGet(''); } /** - * Check the output of the system service. + * Check the output of the system service. */ public function testService() { $node = $this->createNode();