Issue #2959727 by mfb, mcdruid, ejanus, msankhala, Fabianx: drupal_add_html_head_link() needs to allow multiple hreflang tags to point to one URL

merge-requests/1304/head
mcdruid 2021-11-08 17:06:56 +00:00
parent f49e7f9395
commit a45aec7747
3 changed files with 77 additions and 4 deletions

View File

@ -2944,12 +2944,12 @@ function base_path() {
}
/**
* Adds a LINK tag with a distinct 'rel' attribute to the page's HEAD.
* Adds a LINK tag with distinct attributes to the page's HEAD.
*
* This function can be called as long the HTML header hasn't been sent, which
* on normal pages is up through the preprocess step of theme('html'). Adding
* a link will overwrite a prior link with the exact same 'rel' and 'href'
* attributes.
* a link will overwrite a prior link with the exact same 'rel', 'href' and
* 'hreflang' attributes.
*
* @param $attributes
* Associative array of element attributes including 'href' and 'rel'.
@ -2970,7 +2970,7 @@ function drupal_add_html_head_link($attributes, $header = FALSE) {
$element['#attached']['drupal_add_http_header'][] = array('Link', $href . drupal_http_header_attributes($attributes), TRUE);
}
drupal_add_html_head($element, 'drupal_add_html_head_link:' . $attributes['rel'] . ':' . $href);
drupal_add_html_head($element, 'drupal_add_html_head_link:' . $attributes['rel'] . ':' . (isset($attributes['hreflang']) ? "{$attributes['hreflang']}:" : '') . $href);
}
/**

View File

@ -3297,3 +3297,45 @@ class BlockInterestCohortTest extends DrupalWebTestCase {
}
}
/**
* Test for drupal_add_html_head_link().
*/
class DrupalAddHtmlHeadLinkTest extends DrupalWebTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Add HTML head link',
'description' => 'Test for drupal_add_html_head_link().',
'group' => 'System',
);
}
/**
* {@inheritdoc}
*/
function setUp() {
parent::setUp('common_test');
}
/**
* Tests drupal_add_html_head_link().
*/
function testDrupalAddHtmlHeadLink() {
$this->drupalGet('common-test/html_head_link');
$expected_link_header = implode(',', array(
'</foo?bar=baz>; rel="alternate"',
'</foo/bar>; hreflang="nl"; rel="alternate"',
'</foo/bar>; hreflang="de"; rel="alternate"',
));
$this->assertEqual($this->drupalGetHeader('Link'), $expected_link_header);
// Check that duplicate alternate URLs with different hreflangs are allowed.
$test_link = $this->xpath('//head/link[@rel="alternate"][@href="/foo/bar"]');
$this->assertEqual(count($test_link), 2, 'Duplicate alternate URLs are allowed.');
}
}

View File

@ -64,6 +64,12 @@ function common_test_menu() {
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['common-test/html_head_link'] = array(
'title' => 'Test HTML head link',
'page callback' => 'common_test_html_head_link',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
@ -314,3 +320,28 @@ function existing_permissions_policy_header() {
drupal_add_http_header('Permissions-Policy', 'geolocation=()');
print __FUNCTION__;
}
/**
* Page callback.
*/
function common_test_html_head_link() {
drupal_add_html_head_link(array(
'href' => '/foo?bar=baz',
'rel' => 'alternate',
), TRUE);
drupal_add_html_head_link(array(
'href' => '/not-added-to-http-headers',
'rel' => 'alternate',
), FALSE);
drupal_add_html_head_link(array(
'href' => '/foo/bar',
'hreflang' => 'nl',
'rel' => 'alternate',
), TRUE);
drupal_add_html_head_link(array(
'href' => '/foo/bar',
'hreflang' => 'de',
'rel' => 'alternate',
), TRUE);
return '';
}