Issue #2521782 by paulmckibben, swentel: HTML head has alternate hreflang links to unpublished translations
parent
edb618f556
commit
5a25b3fba9
|
@ -566,6 +566,11 @@ function content_translation_page_attachments(&$page) {
|
||||||
if ($entity instanceof ContentEntityInterface) {
|
if ($entity instanceof ContentEntityInterface) {
|
||||||
// Current route represents a content entity. Build hreflang links.
|
// Current route represents a content entity. Build hreflang links.
|
||||||
foreach ($entity->getTranslationLanguages() as $language) {
|
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()
|
$url = $entity->urlInfo()
|
||||||
->setOption('language', $language)
|
->setOption('language', $language)
|
||||||
->setAbsolute()
|
->setAbsolute()
|
||||||
|
|
|
@ -352,8 +352,18 @@ class NodeTranslationUITest extends ContentTranslationUITestBase {
|
||||||
// Test that the node page displays the correct translations.
|
// Test that the node page displays the correct translations.
|
||||||
$this->doTestTranslations('node/' . $node->id(), $values);
|
$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.
|
// Test that the node page has the correct alternate hreflang links.
|
||||||
$this->doTestAlternateHreflangLinks($node->urlInfo());
|
$this->doTestAlternateHreflangLinks($node);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -373,26 +383,39 @@ class NodeTranslationUITest extends ContentTranslationUITestBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
* @param \Drupal\node\Entity\Node $node
|
||||||
* The path to be tested.
|
* 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();
|
$languages = $this->container->get('language_manager')->getLanguages();
|
||||||
$url->setAbsolute();
|
$url->setAbsolute();
|
||||||
$urls = [];
|
$urls = [];
|
||||||
|
$translations = [];
|
||||||
foreach ($this->langcodes as $langcode) {
|
foreach ($this->langcodes as $langcode) {
|
||||||
$language_url = clone $url;
|
$language_url = clone $url;
|
||||||
$urls[$langcode] = $language_url->setOption('language', $languages[$langcode]);
|
$urls[$langcode] = $language_url->setOption('language', $languages[$langcode]);
|
||||||
|
$translations[$langcode] = $node->getTranslation($langcode);
|
||||||
}
|
}
|
||||||
foreach ($this->langcodes as $langcode) {
|
foreach ($this->langcodes as $langcode) {
|
||||||
$this->drupalGet($urls[$langcode]);
|
// Skip unpublished translations.
|
||||||
foreach ($urls as $alternate_langcode => $language_url) {
|
if ($translations[$langcode]->isPublished()) {
|
||||||
// Retrieve desired link elements from the HTML head.
|
$this->drupalGet($urls[$langcode]);
|
||||||
$links = $this->xpath('head/link[@rel = "alternate" and @href = :href and @hreflang = :hreflang]',
|
foreach ($urls as $alternate_langcode => $language_url) {
|
||||||
array(':href' => $language_url->toString(), ':hreflang' => $alternate_langcode));
|
// Retrieve desired link elements from the HTML head.
|
||||||
$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())));
|
$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())));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue