diff --git a/json_ld_schema.module b/json_ld_schema.module index d23f7c0ad47e88d33bc9628a3a3c3c0ec18460ab..1673d66e17851db686e50cad4f7c0a64e2305134 100644 --- a/json_ld_schema.module +++ b/json_ld_schema.module @@ -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']), ]; diff --git a/src/Element/JsonLdSource.php b/src/Element/JsonLdSource.php index c50d4fff64fc62992d5a1c0822a4364df735e510..cc7b0f2c864bf0fc03851a24f2bfce01e550c130 100644 --- a/src/Element/JsonLdSource.php +++ b/src/Element/JsonLdSource.php @@ -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; } diff --git a/src/JsonLdSchemaUtil.php b/src/JsonLdSchemaUtil.php new file mode 100644 index 0000000000000000000000000000000000000000..862cd40df5b6306d4512a305d7a9083fb57b1ba5 --- /dev/null +++ b/src/JsonLdSchemaUtil.php @@ -0,0 +1,25 @@ +<?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); + } + +} diff --git a/tests/src/Functional/JsonLdSourceTest.php b/tests/src/Functional/JsonLdSourceTest.php index 81795f9e2474f39768891eefe96d35202c924cbc..d4917a6f8af7afb8835747d2ca919f8a58a13b94 100644 --- a/tests/src/Functional/JsonLdSourceTest.php +++ b/tests/src/Functional/JsonLdSourceTest.php @@ -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']); } /**