Skip to content
This repository has been archived by the owner on Nov 16, 2018. It is now read-only.

Use new JSON API service to serialize a node #52

Open
wants to merge 1 commit into
base: 8.x-1.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 2 additions & 27 deletions modules/reservoir_ui/reservoir_ui.module
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,10 @@ function reservoir_ui_local_tasks_alter(&$local_tasks) {
function reservoir_ui_node_view_alter(array &$build, NodeInterface $entity, EntityViewDisplayInterface $display) {
// Retrieve JSON API representation of this node.
$jsonapi_url = _reservoir_ui_get_node_jsonapi_url($entity);
$response_body = _reservoir_ui_do_subrequest($jsonapi_url)->getContent();
$normalized = \Drupal::service('jsonapi.entity.to_jsonapi')->normalize($entity);

// Generate markup.
$json = json_encode(json_decode($response_body), JSON_PRETTY_PRINT);
$json = json_encode($normalized, JSON_PRETTY_PRINT);
$markup = <<<HTML
<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>
<p><a href="$jsonapi_url"><code>$jsonapi_url</code></a></p>
Expand Down Expand Up @@ -301,31 +301,6 @@ function _reservoir_ui_get_node_jsonapi_url(NodeInterface $node) {
])->toString(TRUE)->getGeneratedUrl();
}

/**
* Performs a subrequest.
*
* @param string $url
* The string to send a subrequest to.
*
* @return \Symfony\Component\HttpFoundation\Response
* The response.
*/
function _reservoir_ui_do_subrequest($url) {
// Create a subrequest.
$request = \Drupal::request();
$subrequest = Request::create($url, 'GET', $request->query->all(), $request->cookies->all(), [], $request->server->all());
if ($session = $request->getSession()) {
$subrequest->setSession($session);
}

// Execute the subrequest.
$response = \Drupal::service('http_kernel')->handle($subrequest, HttpKernelInterface::SUB_REQUEST);
// @todo Remove this when https://www.drupal.org/node/2613044 lands.
\Drupal::requestStack()->pop();

return $response;
}

/**
* Implements hook_entity_type_alter().
*/
Expand Down