Issue #2303525 by paulmckibben, tim.plunkett: Provide link tags to alternate languages (hreflang) in HTML head.
parent
e07bf5fdd6
commit
2e7b455960
|
@ -718,3 +718,43 @@ function content_translation_preprocess_language_content_settings_table(&$variab
|
|||
module_load_include('inc', 'content_translation', 'content_translation.admin');
|
||||
_content_translation_preprocess_language_content_settings_table($variables);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_page_alter().
|
||||
*/
|
||||
function content_translation_page_alter(&$page) {
|
||||
$route_match = \Drupal::routeMatch();
|
||||
|
||||
// If the current route has no parameters, return.
|
||||
if (!($route = $route_match->getRouteObject()) || !($parameters = $route->getOption('parameters'))) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Determine if the current route represents an entity.
|
||||
foreach ($parameters as $name => $options) {
|
||||
if (!isset($options['type']) || strpos($options['type'], 'entity:') !== 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$entity = $route_match->getParameter($name);
|
||||
if ($entity instanceof ContentEntityInterface) {
|
||||
// Current route represents a content entity. Build hreflang links.
|
||||
foreach ($entity->getTranslationLanguages() as $language) {
|
||||
$url = $entity->urlInfo()
|
||||
->setOption('language', $language)
|
||||
->setAbsolute()
|
||||
->toString();
|
||||
$page['#attached']['drupal_add_html_head_link'][] = array(
|
||||
array(
|
||||
'rel' => 'alternate',
|
||||
'hreflang' => $language->id,
|
||||
'href' => $url,
|
||||
),
|
||||
TRUE,
|
||||
);
|
||||
}
|
||||
}
|
||||
// Since entity was found, no need to iterate further.
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -305,6 +305,9 @@ class NodeTranslationUITest extends ContentTranslationUITest {
|
|||
|
||||
// Test that the node page displays the correct translations.
|
||||
$this->doTestTranslations('node/' . $node->id(), $values);
|
||||
|
||||
// Test that the node page has the correct alternate hreflang links.
|
||||
$this->doTestAlternateHreflangLinks('node/' . $node->id());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -323,6 +326,28 @@ class NodeTranslationUITest extends ContentTranslationUITest {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the given path provides the correct alternate hreflang links.
|
||||
*
|
||||
* @param string $path
|
||||
* The path to be tested.
|
||||
*/
|
||||
protected function doTestAlternateHreflangLinks($path) {
|
||||
$languages = $this->container->get('language_manager')->getLanguages();
|
||||
foreach ($this->langcodes as $langcode) {
|
||||
$urls[$langcode] = url($path, array('absolute' => TRUE, 'language' => $languages[$langcode]));
|
||||
}
|
||||
foreach ($this->langcodes as $langcode) {
|
||||
$this->drupalGet($path, array('language' => $languages[$langcode]));
|
||||
foreach ($urls as $alternate_langcode => $url) {
|
||||
// Retrieve desired link elements from the HTML head.
|
||||
$links = $this->xpath('head/link[@rel = "alternate" and @href = :href and @hreflang = :hreflang]',
|
||||
array(':href' => $url, ':hreflang' => $alternate_langcode));
|
||||
$this->assert(isset($links[0]), format_string('The %langcode node translation has the correct alternate hreflang link for %alternate_langcode: %link.', array('%langcode' => $langcode, '%alternate_langcode' => $alternate_langcode, '%link' => $url)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue