Loading CHANGELOG.txt +2 −0 Original line number Diff line number Diff line Loading @@ -17,6 +17,8 @@ Metatag 8.x-1.x-dev, xxxx-xx-xx source plugins report incorrect number of items. #3136075 by s_leu, fjgarlin, mirom, DamienMcKenna: D9 Compatibility - pre_render should be method implementing TrustedCallbackInterface. #3153911 by alexpott, mrinalini9: Improve logic in metatag_page_attachments() and make code comments more clear. Metatag 8.x-1.13, 2020-04-21 Loading metatag.module +14 −19 Original line number Diff line number Diff line Loading @@ -144,6 +144,13 @@ function metatag_page_attachments(array &$attachments) { $head_links = []; foreach ($metatag_attachments['#attached']['html_head'] as $item) { // Do not attach a title meta tag as this unnecessarily duplicates the // title tag. // @see metatag_preprocess_html() if ($item[1] == 'title') { continue; } $attachments['#attached']['html_head'][] = $item; // Also add a HTTP header "Link:" for canonical URLs and shortlinks. Loading @@ -170,18 +177,6 @@ function metatag_page_attachments(array &$attachments) { ]; } } // Remove the erroneous "title" meta tag as the page title has already been // overridden. // @see metatag_preprocess_html() if (!empty($attachments['#attached']['html_head'])) { foreach ($attachments['#attached']['html_head'] as $ctr => $data) { if (!empty($data[1]) && $data[1] == 'title') { unset($attachments['#attached']['html_head'][$ctr]); break; } } } } /** Loading Loading @@ -427,20 +422,20 @@ function metatag_preprocess_html(&$variables) { return NULL; } $attachments = &drupal_static('metatag_attachments'); if (is_null($attachments)) { $attachments = metatag_get_tags_from_route(); $metatag_attachments = &drupal_static('metatag_attachments'); if (is_null($metatag_attachments)) { $metatag_attachments = metatag_get_tags_from_route(); } if (!$attachments) { if (!$metatag_attachments) { return NULL; } // Copy the "title" meta tag into the regular <title> tag. The redundant meta // tag will be removed elsewhere. // tag is never added to $variables['#attached']. // @see metatag_page_attachments() if (!empty($attachments['#attached']['html_head'])) { foreach ($attachments['#attached']['html_head'] as $attachment) { if (!empty($metatag_attachments['#attached']['html_head'])) { foreach ($metatag_attachments['#attached']['html_head'] as $attachment) { if (!empty($attachment[1]) && $attachment[1] == 'title') { // Empty head_title to avoid the site name and slogan to be appended to // the meta title. Loading tests/modules/metatag_test_integration/metatag_test_integration.info.yml 0 → 100644 +6 −0 Original line number Diff line number Diff line name: "Metatag: Test Integration" type: module description: Support module for integration with metatag module. package: Testing dependencies: - metatag:metatag tests/modules/metatag_test_integration/metatag_test_integration.module 0 → 100644 +72 −0 Original line number Diff line number Diff line <?php /** * @file * Contains metatag_test_integration.module. */ /** * Implements hook_metatags_attachments_alter(). */ function metatag_test_integration_metatags_attachments_alter(array &$attachments) { $title = "This is the title I want | [site:name] | Yeah!"; if (isset($title)) { _metatag_test_integration_replace_tag('title', \Drupal::token()->replace($title), $attachments); } } /** * Replaces meta tag in html head with given content. * * @param string $name * The name of the tag to replace. * @param string $content * The content to use. * @param array $attachments * The array of attachments to act on. */ function _metatag_test_integration_replace_tag($name, $content, array &$attachments) { if (empty($attachments['#attached'])) { $attachments['#attached'] = []; } if (empty($attachments['#attached']['html_head'])) { $attachments['#attached']['html_head'] = []; } $index = _metatag_test_integration_find_tag($name, $attachments); if ($index > -1) { $attachments['#attached']['html_head'][$index][0]['#attributes']['content'] = $content; } else { $attachments['#attached']['html_head'][] = [ 0 => [ '#attributes' => ['name' => $name, 'content' => $content], '#tag' => 'meta', ], 1 => 'description', ]; } } /** * Finds the index of a meta tag in the html head. * * @param string $name * The name of the tag to find. * @param array $attachments * The array of attachments to search. * * @return int * The position in the attachment array that the tag is found in. -1 if the * tag is not set. */ function _metatag_test_integration_find_tag($name, array $attachments) { foreach ($attachments['#attached']['html_head'] as $index => $attachment) { if ($attachment[1] == $name) { return $index; } } return -1; } tests/src/Functional/MetatagIntegrationTest.php 0 → 100644 +41 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\metatag\Functional; use Drupal\Tests\BrowserTestBase; /** * Ensures that the Metatag hook_metatags_attachments_alter() works. * * @group metatag */ class MetatagIntegrationTest extends BrowserTestBase { /** * {@inheritdoc} */ protected static $modules = [ // The base module. 'metatag', // Implements metatag_test_integration_metatags_attachments_alter(). 'metatag_test_integration', ]; /** * {@inheritdoc} */ protected $defaultTheme = 'stark'; /** * Tests hook_metatags_attachments_alter() and title altering. * * @see metatag_test_integration_metatags_attachments_alter() */ public function testHookMetatagsAttachmentsAlter() { // Get the front page and assert the page title. $this->drupalGet(''); $this->assertSession()->titleEquals('This is the title I want | Drupal | Yeah!'); } } Loading
CHANGELOG.txt +2 −0 Original line number Diff line number Diff line Loading @@ -17,6 +17,8 @@ Metatag 8.x-1.x-dev, xxxx-xx-xx source plugins report incorrect number of items. #3136075 by s_leu, fjgarlin, mirom, DamienMcKenna: D9 Compatibility - pre_render should be method implementing TrustedCallbackInterface. #3153911 by alexpott, mrinalini9: Improve logic in metatag_page_attachments() and make code comments more clear. Metatag 8.x-1.13, 2020-04-21 Loading
metatag.module +14 −19 Original line number Diff line number Diff line Loading @@ -144,6 +144,13 @@ function metatag_page_attachments(array &$attachments) { $head_links = []; foreach ($metatag_attachments['#attached']['html_head'] as $item) { // Do not attach a title meta tag as this unnecessarily duplicates the // title tag. // @see metatag_preprocess_html() if ($item[1] == 'title') { continue; } $attachments['#attached']['html_head'][] = $item; // Also add a HTTP header "Link:" for canonical URLs and shortlinks. Loading @@ -170,18 +177,6 @@ function metatag_page_attachments(array &$attachments) { ]; } } // Remove the erroneous "title" meta tag as the page title has already been // overridden. // @see metatag_preprocess_html() if (!empty($attachments['#attached']['html_head'])) { foreach ($attachments['#attached']['html_head'] as $ctr => $data) { if (!empty($data[1]) && $data[1] == 'title') { unset($attachments['#attached']['html_head'][$ctr]); break; } } } } /** Loading Loading @@ -427,20 +422,20 @@ function metatag_preprocess_html(&$variables) { return NULL; } $attachments = &drupal_static('metatag_attachments'); if (is_null($attachments)) { $attachments = metatag_get_tags_from_route(); $metatag_attachments = &drupal_static('metatag_attachments'); if (is_null($metatag_attachments)) { $metatag_attachments = metatag_get_tags_from_route(); } if (!$attachments) { if (!$metatag_attachments) { return NULL; } // Copy the "title" meta tag into the regular <title> tag. The redundant meta // tag will be removed elsewhere. // tag is never added to $variables['#attached']. // @see metatag_page_attachments() if (!empty($attachments['#attached']['html_head'])) { foreach ($attachments['#attached']['html_head'] as $attachment) { if (!empty($metatag_attachments['#attached']['html_head'])) { foreach ($metatag_attachments['#attached']['html_head'] as $attachment) { if (!empty($attachment[1]) && $attachment[1] == 'title') { // Empty head_title to avoid the site name and slogan to be appended to // the meta title. Loading
tests/modules/metatag_test_integration/metatag_test_integration.info.yml 0 → 100644 +6 −0 Original line number Diff line number Diff line name: "Metatag: Test Integration" type: module description: Support module for integration with metatag module. package: Testing dependencies: - metatag:metatag
tests/modules/metatag_test_integration/metatag_test_integration.module 0 → 100644 +72 −0 Original line number Diff line number Diff line <?php /** * @file * Contains metatag_test_integration.module. */ /** * Implements hook_metatags_attachments_alter(). */ function metatag_test_integration_metatags_attachments_alter(array &$attachments) { $title = "This is the title I want | [site:name] | Yeah!"; if (isset($title)) { _metatag_test_integration_replace_tag('title', \Drupal::token()->replace($title), $attachments); } } /** * Replaces meta tag in html head with given content. * * @param string $name * The name of the tag to replace. * @param string $content * The content to use. * @param array $attachments * The array of attachments to act on. */ function _metatag_test_integration_replace_tag($name, $content, array &$attachments) { if (empty($attachments['#attached'])) { $attachments['#attached'] = []; } if (empty($attachments['#attached']['html_head'])) { $attachments['#attached']['html_head'] = []; } $index = _metatag_test_integration_find_tag($name, $attachments); if ($index > -1) { $attachments['#attached']['html_head'][$index][0]['#attributes']['content'] = $content; } else { $attachments['#attached']['html_head'][] = [ 0 => [ '#attributes' => ['name' => $name, 'content' => $content], '#tag' => 'meta', ], 1 => 'description', ]; } } /** * Finds the index of a meta tag in the html head. * * @param string $name * The name of the tag to find. * @param array $attachments * The array of attachments to search. * * @return int * The position in the attachment array that the tag is found in. -1 if the * tag is not set. */ function _metatag_test_integration_find_tag($name, array $attachments) { foreach ($attachments['#attached']['html_head'] as $index => $attachment) { if ($attachment[1] == $name) { return $index; } } return -1; }
tests/src/Functional/MetatagIntegrationTest.php 0 → 100644 +41 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\metatag\Functional; use Drupal\Tests\BrowserTestBase; /** * Ensures that the Metatag hook_metatags_attachments_alter() works. * * @group metatag */ class MetatagIntegrationTest extends BrowserTestBase { /** * {@inheritdoc} */ protected static $modules = [ // The base module. 'metatag', // Implements metatag_test_integration_metatags_attachments_alter(). 'metatag_test_integration', ]; /** * {@inheritdoc} */ protected $defaultTheme = 'stark'; /** * Tests hook_metatags_attachments_alter() and title altering. * * @see metatag_test_integration_metatags_attachments_alter() */ public function testHookMetatagsAttachmentsAlter() { // Get the front page and assert the page title. $this->drupalGet(''); $this->assertSession()->titleEquals('This is the title I want | Drupal | Yeah!'); } }