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

Commit

Permalink
fix: pretty print the JSON in the node view
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateu Aguiló Bosch committed Nov 13, 2018
1 parent eb20e07 commit 17dd080
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ contenta_enhancements:
css:
component:
css/contenta_enhancements.css: {}

contenta_enhancements.node-preview:
version: '1.0'
js:
https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?skin=sunburst: { type: external, minified: true }
20 changes: 14 additions & 6 deletions modules/contenta_enhancements/contenta_enhancements.module
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,12 @@ function contenta_enhancements_node_view_alter(array &$build, NodeInterface $ent
$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"><code class="language-js">$json</code></pre>
HTML;
$flags = JSON_HEX_TAG
| JSON_HEX_APOS
| JSON_HEX_AMP
| JSON_HEX_QUOT
| JSON_PRETTY_PRINT;
$json = json_encode($normalized, $flags);

// Add to render array.
$build['jsonapi_prefix'] = [
Expand All @@ -389,7 +390,14 @@ HTML;
];
$build['jsonapi'] = [
'#weight' => 100000,
'#prefix' => '</div><div class="col s12 m8"><div class="node-jsonapi-preview">' . Markup::create($markup) . '</div></div></div>',
'#prefix' => '</div><div class="col s12 m8"><div class="node-jsonapi-preview"><pre class="prettyprint"><code class="language-js">',
'#suffix' => '</code></pre></div></div></div>',
'#markup' => $json,
'#attached' => [
'library' => [
'contenta_enhancements/contenta_enhancements.node-preview',
],
],
];
}

Expand Down

0 comments on commit 17dd080

Please sign in to comment.