From bd882e9b7b7e45ac6aecd58ff6c39d76aebefc12 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 30 Mar 2021 16:21:38 +0100
Subject: [PATCH] Issue #2992894 by mohit_aghera, adityasingh, adalbertov,
 raman.b, vsujeetkumar, mgifford, RenatoG, nishantghetiya, Kristen Pol,
 alexpott, penyaskito, andrewmacpherson, catch: Search results of
 multi-lingual pages fail Language of Parts

---
 core/modules/search/search.pages.inc          |  6 ++---
 .../src/Functional/SearchLanguageTest.php     | 23 +++++++++++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/core/modules/search/search.pages.inc b/core/modules/search/search.pages.inc
index 85923a8f09a9..8db7d159f2a8 100644
--- a/core/modules/search/search.pages.inc
+++ b/core/modules/search/search.pages.inc
@@ -30,9 +30,9 @@ function template_preprocess_search_result(&$variables) {
   $result = $variables['result'];
   $variables['url'] = UrlHelper::stripDangerousProtocols($result['link']);
   $variables['title'] = $result['title'];
-  if (isset($result['language']) && $result['language'] != $language_interface->getId() && $result['language'] != LanguageInterface::LANGCODE_NOT_SPECIFIED) {
-    $variables['title_attributes']['lang'] = $result['language'];
-    $variables['content_attributes']['lang'] = $result['language'];
+  if (isset($result['langcode']) && $result['langcode'] != $language_interface->getId() && $result['langcode'] != LanguageInterface::LANGCODE_NOT_SPECIFIED) {
+    $variables['title_attributes']['lang'] = $result['langcode'];
+    $variables['content_attributes']['lang'] = $result['langcode'];
   }
 
   $info = [];
diff --git a/core/modules/search/tests/src/Functional/SearchLanguageTest.php b/core/modules/search/tests/src/Functional/SearchLanguageTest.php
index 7ef7a8a25a69..5b18be2774f1 100644
--- a/core/modules/search/tests/src/Functional/SearchLanguageTest.php
+++ b/core/modules/search/tests/src/Functional/SearchLanguageTest.php
@@ -148,4 +148,27 @@ public function testLanguages() {
     $this->drupalPostForm('admin/config/regional/language/delete/en', [], 'Delete');
   }
 
+  /**
+   * Test language attribute "lang" for the search results.
+   */
+  public function testLanguageAttributes() {
+    $this->drupalGet('search/node');
+    $this->submitForm(['keys' => 'the Spanish title'], 'Search');
+
+    $node = $this->searchableNodes[1]->getTranslation('es');
+    $this->assertSession()->elementExists('xpath', '//div[@class="layout-content"]//ol/li/h3[contains(@lang, "es")]');
+    $result = $this->xpath('//div[@class="layout-content"]//ol/li/h3[contains(@lang, "es")]/a');
+    $this->assertEquals($node->getTitle(), $result[0]->getText());
+    $this->assertSession()->elementExists('xpath', '//div[@class="layout-content"]//ol/li/p[contains(@lang, "es")]');
+
+    // Visit the search form in Spanish language.
+    $this->drupalGet('es/search/node');
+    $this->submitForm(['keys' => 'First node'], 'Search');
+    $this->assertSession()->elementExists('xpath', '//div[@class="layout-content"]//ol/li/h3[contains(@lang, "en")]');
+    $node = $this->searchableNodes[0]->getTranslation('en');
+    $result = $this->xpath('//div[@class="layout-content"]//ol/li/h3[contains(@lang, "en")]/a');
+    $this->assertEquals($node->getTitle(), $result[0]->getText());
+    $this->assertSession()->elementExists('xpath', '//div[@class="layout-content"]//ol/li/p[contains(@lang, "en")]');
+  }
+
 }
-- 
GitLab