Commit 11118a34 authored by catch's avatar catch
Browse files

Issue #2796399 by Krzysztof Domański, mayurjadhav, pritish.kumar, heikki,...

Issue #2796399 by Krzysztof Domański, mayurjadhav, pritish.kumar, heikki, sarvjeetsingh, User Advocate, caspervoogt, Kristen Pol, abhisekmazumdar: Problematic hreflang links when using entity as front page
parent df4b4043
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -723,6 +723,7 @@ horizontalrule
hosters
hostnames
hreflang
hreflangs
hrefs
htaccess
htkey
+16 −4
Original line number Diff line number Diff line
@@ -674,6 +674,7 @@ function content_translation_page_attachments(&$page) {
  if (!($route = $route_match->getRouteObject()) || !($parameters = $route->getOption('parameters'))) {
    return;
  }
  $is_front = \Drupal::service('path.matcher')->isFrontPage();

  // Determine if the current route represents an entity.
  foreach ($parameters as $name => $options) {
@@ -692,10 +693,21 @@ function content_translation_page_attachments(&$page) {
        if (!$access->isAllowed()) {
          continue;
        }
        if ($is_front) {
          // If the current page is front page, do not create hreflang links
          // from the entity route, just add the languages to root path.
          $url = Url::fromRoute('<front>', [], [
            'absolute' => TRUE,
            'language' => $language,
          ])->toString();
        }
        // Create links for the entity path.
        else {
          $url = $entity->toUrl('canonical')
            ->setOption('language', $language)
            ->setAbsolute()
            ->toString();
        }
        $page['#attached']['html_head_link'][] = [
          [
            'rel' => 'alternate',
+22 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\Tests\content_translation\Functional;

use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\entity_test\Entity\EntityTestMul;
@@ -121,6 +122,27 @@ function ($langcode) use ($url_base, $languageManager) {
        $this->assertTrue(isset($links[0]), $message);
      }
    }

    // Configure entity path as a front page.
    $entity_canonical = '/entity_test_mul/manage/' . $entity->id();
    $this->config('system.site')->set('page.front', $entity_canonical)->save();

    // Tests hreflangs when using entities as a front page.
    foreach ($urls as $langcode => $url) {
      $this->drupalGet($url);
      foreach ($entity->getTranslationLanguages() as $language) {
        $frontpage_path = Url::fromRoute('<front>', [], [
          'absolute' => TRUE,
          'language' => $language,
        ])->toString();
        $args = [
          ':href' => $frontpage_path,
          ':hreflang' => $language->getId(),
        ];
        $links = $this->xpath('head/link[@rel = "alternate" and @href = :href and @hreflang = :hreflang]', $args);
        $this->assertArrayHasKey(0, $links);
      }
    }
  }

  /**