Skip to content
Snippets Groups Projects
Commit 5343e6d0 authored by Adam Bramley's avatar Adam Bramley
Browse files

Issue #3292366 by acbramley: Add JSON_PRETTY_PRINT flag

parent 900f2088
Branches
Tags 8.x-1.3
1 merge request!3Issue #3292366: Add JSON_PRETTY_PRINT flag
......@@ -8,6 +8,7 @@
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\json_ld_schema\JsonLdSchemaUtil;
/**
* Implements hook_page_bottom().
......@@ -62,8 +63,7 @@ function json_ld_schema_entity_view(array &$build, EntityInterface $entity, Enti
[
'#tag' => 'script',
'#attributes' => ['type' => 'application/ld+json'],
'#value' => json_encode($json_ld_entity->getData($entity, $view_mode)
->toArray(), JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT),
'#value' => JsonLdSchemaUtil::encodeJsonLdData($json_ld_entity->getData($entity, $view_mode)->toArray()),
],
sprintf('json_ld_%s_%s_%s_%s', $entity->getEntityTypeId(), $entity->id(), $view_mode, $definition['id']),
];
......
......@@ -4,6 +4,7 @@ namespace Drupal\json_ld_schema\Element;
use Drupal\Core\Render\Element\HtmlTag;
use Drupal\Core\Render\Element\RenderElement;
use Drupal\json_ld_schema\JsonLdSchemaUtil;
/**
* A JSON LD source element.
......@@ -35,7 +36,7 @@ class JsonLdSource extends RenderElement {
// that it is cached by render cache.
$source_manager = \Drupal::service('plugin.manager.json_ld_schema.source');
$plugin = $source_manager->createInstance($element['#plugin_id']);
$element['#value'] = json_encode($plugin->getData()->toArray(), JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
$element['#value'] = JsonLdSchemaUtil::encodeJsonLdData($plugin->getData()->toArray());
return $element;
}
......
<?php
declare(strict_types=1);
namespace Drupal\json_ld_schema;
/**
* Helper functions for the module.
*/
class JsonLdSchemaUtil {
/**
* Encode JSON:LD data.
*
* @param array $data
* An array of data to encode.
*
* @return string
* The encoded string.
*/
public static function encodeJsonLdData(array $data): string {
return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_PRETTY_PRINT);
}
}
......@@ -60,10 +60,10 @@ class JsonLdSourceTest extends BrowserTestBase {
// count changes.
$this->setNodeCommentCount(5);
$this->drupalGet($node->toUrl());
$this->assertStringContainsString('"commentCount":5', $this->getSession()->getPage()->getHtml());
$this->assertCommentCount(5);
$this->setNodeCommentCount(10);
$this->drupalGet($node->toUrl());
$this->assertStringContainsString('"commentCount":5', $this->getSession()->getPage()->getHtml());
$this->assertCommentCount(5);
// A second node will display the updated comment count.
$second_node = Node::create([
......@@ -72,7 +72,18 @@ class JsonLdSourceTest extends BrowserTestBase {
]);
$second_node->save();
$this->drupalGet($second_node->toUrl());
$this->assertStringContainsString('"commentCount":10', $this->getSession()->getPage()->getHtml());
$this->assertCommentCount(10);
}
/**
* Assert the comment count that appears on the page.
*
* @param int $count
* The count.
*/
protected function assertCommentCount(int $count): void {
$schema = $this->getSchemaValues('AboutPage');
$this->assertEquals($count, $schema['commentCount']);
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment