Skip to content

Commit

Permalink
Issue #2521782 by paulmckibben, mparker17, MerryHamster, swentel, yog…
Browse files Browse the repository at this point in the history
…eshmpawar, caspervoogt, Nikolay Borisov, maebug, jkuma, Saviktor, Wim Leers, catch: HTML head has alternate hreflang links to unpublished translations

(cherry picked from commit db46062db906b7d72c9a8f3805e357eeafb9d5f2)
  • Loading branch information
catch committed Jul 13, 2020
1 parent a13dc2a commit 07c7df6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
13 changes: 12 additions & 1 deletion modules/content_translation/content_translation.module
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use Drupal\Core\Url;
use Drupal\content_translation\BundleTranslationSettingsInterface;
use Drupal\content_translation\ContentTranslationManager;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\ContentEntityFormInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
Expand Down Expand Up @@ -656,6 +657,7 @@ function content_translation_preprocess_language_content_settings_table(&$variab
* Implements hook_page_attachments().
*/
function content_translation_page_attachments(&$page) {
$cache = CacheableMetadata::createFromRenderArray($page);
$route_match = \Drupal::routeMatch();

// If the current route has no parameters, return.
Expand All @@ -673,6 +675,13 @@ function content_translation_page_attachments(&$page) {
if ($entity instanceof ContentEntityInterface && $entity->hasLinkTemplate('canonical')) {
// 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());
$access = $translation->access('view', NULL, TRUE);
$cache->addCacheableDependency($access);
if (!$access->isAllowed()) {
continue;
}
$url = $entity->toUrl('canonical')
->setOption('language', $language)
->setAbsolute()
Expand All @@ -688,6 +697,8 @@ function content_translation_page_attachments(&$page) {
}
}
// Since entity was found, no need to iterate further.
return;
break;
}
// Apply updated caching information.
$cache->applyTo($page);
}
31 changes: 21 additions & 10 deletions modules/node/tests/src/Functional/NodeTranslationUITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public function testTranslationRendering() {
$this->doTestTranslations('node/' . $node->id(), $values);

// Test that the node page has the correct alternate hreflang links.
$this->doTestAlternateHreflangLinks($node->toUrl());
$this->doTestAlternateHreflangLinks($node);
}

/**
Expand All @@ -393,24 +393,35 @@ protected function doTestTranslations($path, array $values) {
/**
* Tests that the given path provides the 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.
*/
protected function doTestAlternateHreflangLinks(Url $url) {
protected function doTestAlternateHreflangLinks(Node $node) {
$url = $node->toUrl();
$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]',
[':href' => $language_url->toString(), ':hreflang' => $alternate_langcode]);
$this->assert(isset($links[0]), new FormattableMarkup('The %langcode node translation has the correct alternate hreflang link for %alternate_langcode: %link.', ['%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]',
[':href' => $language_url->toString(), ':hreflang' => $alternate_langcode]);
if ($translations[$alternate_langcode]->isPublished()) {
$this->assert(isset($links[0]), new FormattableMarkup('The %langcode node translation has the correct alternate hreflang link for %alternate_langcode: %link.', ['%langcode' => $langcode, '%alternate_langcode' => $alternate_langcode, '%link' => $url->toString()]));
}
else {
$this->assertFalse(isset($links[0]), new FormattableMarkup('The %langcode node translation has an hreflang link for unpublished %alternate_langcode translation: %link.', ['%langcode' => $langcode, '%alternate_langcode' => $alternate_langcode, '%link' => $url->toString()]));
}
}
}
}
}
Expand Down

0 comments on commit 07c7df6

Please sign in to comment.