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

Commit f3db61e

Browse files
author
Mateu Aguiló Bosch
committed
fix: make sure we find the correct JSON API service
1 parent bd30c31 commit f3db61e

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

modules/contenta_enhancements/contenta_enhancements.module

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use Drupal\node\NodeInterface;
77
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
88
use Drupal\Core\Render\Markup;
99
use Drupal\Core\Menu\MenuTreeParameters;
10+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1011

1112
/**
1213
* Implements hook_help().
@@ -366,13 +367,19 @@ function contenta_enhancements_node_view_alter(array &$build, NodeInterface $ent
366367
return;
367368
}
368369
// Retrieve JSON API representation of this node.
369-
$normalized = \Drupal::service('jsonapi.entity.to_jsonapi')->normalize($entity);
370+
try {
371+
$entity_to_jsonapi = \Drupal::service('jsonapi_extras.entity.to_jsonapi');
372+
}
373+
catch (ServiceNotFoundException $e) {
374+
$entity_to_jsonapi = \Drupal::service('jsonapi.entity.to_jsonapi');
375+
}
376+
$normalized = $entity_to_jsonapi->normalize($entity);
370377

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

378385
// Add to render array.

0 commit comments

Comments
 (0)