Skip to content

Commit

Permalink
Merge pull request #67 from City-of-Helsinki/UHF-9448-entity-id-metatag
Browse files Browse the repository at this point in the history
UHF-9448: Add entity id metatag
  • Loading branch information
hyrsky authored Dec 21, 2023
2 parents 8966aa2 + 0ab0ef3 commit cab652f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
24 changes: 15 additions & 9 deletions helfi_proxy.module
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion tests/src/Functional/CustomMetatagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<front>');
$this->assertSession()
->statusCodeEquals(200);

$this->assertSession()
->elementNotExists('css', 'meta[name="helfi_content_type"]');
$this->assertSession()
->elementNotExists('css', 'meta[name="helfi_content_id"]');
}

}

0 comments on commit cab652f

Please sign in to comment.