From 0ab0ef363990be66f1a23d5e18dd00b0f146e633 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Wed, 20 Dec 2023 14:44:34 +0200 Subject: [PATCH] Add entity id to custom metatag --- helfi_proxy.module | 24 ++++++++++++++-------- tests/src/Functional/CustomMetatagTest.php | 7 ++++++- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/helfi_proxy.module b/helfi_proxy.module index 3233c01..dd60724 100644 --- a/helfi_proxy.module +++ b/helfi_proxy.module @@ -71,16 +71,22 @@ function helfi_proxy_page_attachments_alter(array &$attachments) { $entity = !empty($entities) ? reset($entities) : NULL; if ($entity) { - $tag_name = 'helfi_content_type'; - - $helfi_content_type = [ - '#tag' => 'meta', - '#attributes' => [ - 'name' => $tag_name, - 'content' => $entity->bundle(), - ], + $tags = [ + 'helfi_content_type' => $entity->bundle(), + 'helfi_content_id' => $entity->id(), ]; - $attachments['#attached']['html_head'][] = [$helfi_content_type, $tag_name]; + + foreach ($tags as $tag_name => $content) { + $tag = [ + '#tag' => 'meta', + '#attributes' => [ + 'name' => $tag_name, + 'content' => $content, + ], + ]; + + $attachments['#attached']['html_head'][] = [$tag, $tag_name]; + } } } diff --git a/tests/src/Functional/CustomMetatagTest.php b/tests/src/Functional/CustomMetatagTest.php index 0091de3..3977977 100644 --- a/tests/src/Functional/CustomMetatagTest.php +++ b/tests/src/Functional/CustomMetatagTest.php @@ -50,19 +50,24 @@ public function setUp(): void { } /** - * Test that custom header metatag is set on page correctly. + * Test that custom header metatags are set correctly. */ public function testMetatag() : void { $this->drupalGet($this->node->toUrl('canonical')); $this->assertSession() ->elementAttributeContains('css', 'meta[name="helfi_content_type"]', 'content', 'page'); + $this->assertSession() + ->elementAttributeContains('css', 'meta[name="helfi_content_id"]', 'content', (string) $this->node->id()); + $this->drupalGet(''); $this->assertSession() ->statusCodeEquals(200); $this->assertSession() ->elementNotExists('css', 'meta[name="helfi_content_type"]'); + $this->assertSession() + ->elementNotExists('css', 'meta[name="helfi_content_id"]'); } }