Commit 06b36bd6 authored by catch's avatar catch

Issue #2521782 by paulmckibben, mparker17, MerryHamster, swentel,...

Issue #2521782 by paulmckibben, mparker17, MerryHamster, swentel, yogeshmpawar, caspervoogt, Nikolay Borisov, maebug, jkuma, Saviktor, Wim Leers, catch: HTML head has alternate hreflang links to unpublished translations

(cherry picked from commit db46062d)
parent d5a8ea01
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
use Drupal\content_translation\BundleTranslationSettingsInterface; use Drupal\content_translation\BundleTranslationSettingsInterface;
use Drupal\content_translation\ContentTranslationManager; use Drupal\content_translation\ContentTranslationManager;
use Drupal\Core\Access\AccessResult; use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\ContentEntityFormInterface; use Drupal\Core\Entity\ContentEntityFormInterface;
use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
...@@ -656,6 +657,7 @@ function content_translation_preprocess_language_content_settings_table(&$variab ...@@ -656,6 +657,7 @@ function content_translation_preprocess_language_content_settings_table(&$variab
* Implements hook_page_attachments(). * Implements hook_page_attachments().
*/ */
function content_translation_page_attachments(&$page) { function content_translation_page_attachments(&$page) {
$cache = CacheableMetadata::createFromRenderArray($page);
$route_match = \Drupal::routeMatch(); $route_match = \Drupal::routeMatch();
// If the current route has no parameters, return. // If the current route has no parameters, return.
...@@ -673,6 +675,13 @@ function content_translation_page_attachments(&$page) { ...@@ -673,6 +675,13 @@ function content_translation_page_attachments(&$page) {
if ($entity instanceof ContentEntityInterface && $entity->hasLinkTemplate('canonical')) { if ($entity instanceof ContentEntityInterface && $entity->hasLinkTemplate('canonical')) {
// 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());
$access = $translation->access('view', NULL, TRUE);
$cache->addCacheableDependency($access);
if (!$access->isAllowed()) {
continue;
}
$url = $entity->toUrl('canonical') $url = $entity->toUrl('canonical')
->setOption('language', $language) ->setOption('language', $language)
->setAbsolute() ->setAbsolute()
...@@ -688,6 +697,8 @@ function content_translation_page_attachments(&$page) { ...@@ -688,6 +697,8 @@ function content_translation_page_attachments(&$page) {
} }
} }
// Since entity was found, no need to iterate further. // Since entity was found, no need to iterate further.
return; break;
} }
// Apply updated caching information.
$cache->applyTo($page);
} }
...@@ -371,7 +371,7 @@ public function testTranslationRendering() { ...@@ -371,7 +371,7 @@ public function testTranslationRendering() {
$this->doTestTranslations('node/' . $node->id(), $values); $this->doTestTranslations('node/' . $node->id(), $values);
// 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->toUrl()); $this->doTestAlternateHreflangLinks($node);
} }
/** /**
...@@ -393,24 +393,35 @@ protected function doTestTranslations($path, array $values) { ...@@ -393,24 +393,35 @@ protected function doTestTranslations($path, array $values) {
/** /**
* Tests that the given path provides the correct alternate hreflang links. * Tests that the given path provides the 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.
*/ */
protected function doTestAlternateHreflangLinks(Url $url) { protected function doTestAlternateHreflangLinks(Node $node) {
$url = $node->toUrl();
$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) {
[':href' => $language_url->toString(), ':hreflang' => $alternate_langcode]); // Retrieve desired link elements from the HTML head.
$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()])); $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()]));
}
}
} }
} }
} }
......
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