Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
fix: make sure we find the correct JSON API service
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateu Aguiló Bosch committed Nov 13, 2018
1 parent bd30c31 commit f3db61e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions modules/contenta_enhancements/contenta_enhancements.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use Drupal\node\NodeInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Menu\MenuTreeParameters;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

/**
* Implements hook_help().
Expand Down Expand Up @@ -366,13 +367,19 @@ function contenta_enhancements_node_view_alter(array &$build, NodeInterface $ent
return;
}
// Retrieve JSON API representation of this node.
$normalized = \Drupal::service('jsonapi.entity.to_jsonapi')->normalize($entity);
try {
$entity_to_jsonapi = \Drupal::service('jsonapi_extras.entity.to_jsonapi');
}
catch (ServiceNotFoundException $e) {
$entity_to_jsonapi = \Drupal::service('jsonapi.entity.to_jsonapi');
}
$normalized = $entity_to_jsonapi->normalize($entity);

// Generate markup.
$json = json_encode($normalized, JSON_PRETTY_PRINT);
$markup = <<<HTML
<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>
<pre class="prettyprint lang-js">$json</pre>
<pre class="prettyprint"><code class="language-js">$json</code></pre>
HTML;

// Add to render array.
Expand Down

0 comments on commit f3db61e

Please sign in to comment.