Commit 5a25b3fb authored by catch's avatar catch
Browse files

Issue #2521782 by paulmckibben, swentel: HTML head has alternate hreflang...

Issue #2521782 by paulmckibben, swentel: HTML head has alternate hreflang links to unpublished translations
parent edb618f5
......@@ -566,6 +566,11 @@ function content_translation_page_attachments(&$page) {
if ($entity instanceof ContentEntityInterface) {
// Current route represents a content entity. Build hreflang links.
foreach ($entity->getTranslationLanguages() as $language) {
// Skip any translation that cannot be viewed.
$translation = $entity->getTranslation($language->getId());
if (!$translation->access('view')) {
continue;
}
$url = $entity->urlInfo()
->setOption('language', $language)
->setAbsolute()
......
......@@ -352,8 +352,18 @@ public function testTranslationRendering() {
// Test that the node page displays the correct translations.
$this->doTestTranslations('node/' . $node->id(), $values);
// Unpublish one of the translations.
foreach ($node->getTranslationLanguages() as $lang) {
if ($lang->getId() != 'en') {
$translation = $node->getTranslation($lang->getId());
$translation->setPublished(NODE_NOT_PUBLISHED);
$translation->save();
break;
}
}
// Test that the node page has the correct alternate hreflang links.
$this->doTestAlternateHreflangLinks($node->urlInfo());
$this->doTestAlternateHreflangLinks($node);
}
/**
......@@ -373,26 +383,39 @@ protected function doTestTranslations($path, array $values) {
}
/**
* Tests that the given path provides the correct alternate hreflang links.
* Tests the given node's page view for correct alternate hreflang links.
*
* @param \Drupal\Core\Url $url
* The path to be tested.
* @param \Drupal\node\Entity\Node $node
* The node to be tested.
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
protected function doTestAlternateHreflangLinks(Url $url) {
protected function doTestAlternateHreflangLinks(Node $node) {
$url = $node->urlInfo();
$languages = $this->container->get('language_manager')->getLanguages();
$url->setAbsolute();
$urls = [];
$translations = [];
foreach ($this->langcodes as $langcode) {
$language_url = clone $url;
$urls[$langcode] = $language_url->setOption('language', $languages[$langcode]);
$translations[$langcode] = $node->getTranslation($langcode);
}
foreach ($this->langcodes as $langcode) {
$this->drupalGet($urls[$langcode]);
foreach ($urls as $alternate_langcode => $language_url) {
// Retrieve desired link elements from the HTML head.
$links = $this->xpath('head/link[@rel = "alternate" and @href = :href and @hreflang = :hreflang]',
array(':href' => $language_url->toString(), ':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->toString())));
// Skip unpublished translations.
if ($translations[$langcode]->isPublished()) {
$this->drupalGet($urls[$langcode]);
foreach ($urls as $alternate_langcode => $language_url) {
// Retrieve desired link elements from the HTML head.
$links = $this->xpath('head/link[@rel = "alternate" and @href = :href and @hreflang = :hreflang]',
array(':href' => $language_url->toString(), ':hreflang' => $alternate_langcode));
if ($translations[$alternate_langcode]->isPublished()) {
$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->toString())));
}
else {
$this->assertFalse(isset($links[0]), format_string('The %langcode node translation has an hreflang link for unpublished %alternate_langcode translation: %link.', array('%langcode' => $langcode, '%alternate_langcode' => $alternate_langcode, '%link' => $url->toString())));
}
}
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment