diff --git a/core/modules/search/search.pages.inc b/core/modules/search/search.pages.inc index 85923a8f09a92ec6dd331f5d880ebd1961204a86..8db7d159f2a876ed89f180c65dd7b3f6923e7847 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 7ef7a8a25a6915281852fa181dfe3f28405e0bd2..5b18be2774f1ad9a5f77728c5675165f0c86c5d4 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")]'); + } + }