diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 349aab5..ad24e8d 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -1582,7 +1582,7 @@ function node_search_execute($keys = NULL, $conditions = NULL) { 'node' => $node, 'extra' => $extra, 'score' => $item->calculated_score, - 'snippet' => search_excerpt($keys, $node->rendered), + 'snippet' => search_excerpt($keys, $node->rendered, $item->langcode), 'langcode' => $node->langcode, ); } diff --git a/core/modules/search/lib/Drupal/search/Tests/SearchPreprocessLangcodeTest.php b/core/modules/search/lib/Drupal/search/Tests/SearchPreprocessLangcodeTest.php new file mode 100644 index 0000000..f3c0f61 --- /dev/null +++ b/core/modules/search/lib/Drupal/search/Tests/SearchPreprocessLangcodeTest.php @@ -0,0 +1,59 @@ + 'Search preprocess langcode', + 'description' => 'Check that the hook_search_preprocess passes the correct langcode from the entity.', + 'group' => 'Search', + ); + } + + function setUp() { + parent::setUp(); + + $web_user = $this->drupalCreateUser(array('create page content', 'edit own page content', 'search content', 'use advanced search')); + $this->drupalLogin($web_user); + } + + /** + * Tests that search_simplify() does the right thing with punctuation. + */ + function testPreprocessLangcode() { + // Create a node. + $node = $this->drupalCreateNode(array('body' => array('en' => array(array())), 'langcode' => 'en')); + + // First update the index. This does the initial processing. + node_update_index(); + + // Then, run the shutdown function. Testing is a unique case where indexing + // and searching has to happen in the same request, so running the shutdown + // function manually is needed to finish the indexing process. + search_update_totals(); + + // Search for the title of the node with a POST query. + $edit = array('or' => $node->label()); + $this->drupalPost('search/node', $edit, t('Advanced search')); + + // Checks if the langcode has been passed by the Preprocess Hook. + $this->assertText('Langcode Preprocess Test: en'); + } +} diff --git a/core/modules/search/search.api.php b/core/modules/search/search.api.php index 4c24475..996acc3 100644 --- a/core/modules/search/search.api.php +++ b/core/modules/search/search.api.php @@ -246,7 +246,7 @@ function hook_search_execute($keys = NULL, $conditions = NULL) { 'node' => $node, 'extra' => $extra, 'score' => $item->calculated_score, - 'snippet' => search_excerpt($keys, $node->rendered), + 'snippet' => search_excerpt($keys, $node->rendered, $item->langcode), 'langcode' => $node->langcode, ); } @@ -305,6 +305,9 @@ function hook_search_page($results) { * from between two HTML tags or from the search query. It will not contain * any HTML entities or HTML tags. * + * @param $langcode + * The language code of the entitiy that has been found. + * * @return * The text after preprocessing. Note that if your module decides not to alter * the text, it should return the original text. Also, after preprocessing, @@ -312,7 +315,7 @@ function hook_search_page($results) { * * @ingroup search */ -function hook_search_preprocess($text) { +function hook_search_preprocess($text, $langcode = NULL) { // Do processing on $text return $text; } diff --git a/core/modules/search/search.module b/core/modules/search/search.module index 7613465..a1ed6c3 100644 --- a/core/modules/search/search.module +++ b/core/modules/search/search.module @@ -430,7 +430,7 @@ function search_update_totals() { * * @see hook_search_preprocess() */ -function search_simplify($text) { +function search_simplify($text, $langcode = NULL) { // Decode entities to UTF-8 $text = decode_entities($text); @@ -438,7 +438,7 @@ function search_simplify($text) { $text = drupal_strtolower($text); // Call an external processor for word handling. - search_invoke_preprocess($text); + search_invoke_preprocess($text, $langcode); // Simple CJK handling if (config('search.settings')->get('index.overlap_cjk')) { @@ -554,9 +554,9 @@ function _search_index_truncate(&$text) { /** * Invokes hook_search_preprocess() in modules. */ -function search_invoke_preprocess(&$text) { +function search_invoke_preprocess(&$text, $langcode = NULL) { foreach (module_implements('search_preprocess') as $module) { - $text = module_invoke($module, 'search_preprocess', $text); + $text = module_invoke($module, 'search_preprocess', $text, $langcode); } } @@ -1109,7 +1109,7 @@ function search_data($keys, $module, $conditions = NULL) { * @return * A string containing HTML for the excerpt. */ -function search_excerpt($keys, $text) { +function search_excerpt($keys, $text, $langcode = NULL) { // We highlight around non-indexable or CJK characters. $boundary = '(?:(?<=[' . PREG_CLASS_UNICODE_WORD_BOUNDARY . PREG_CLASS_CJK . '])|(?=[' . PREG_CLASS_UNICODE_WORD_BOUNDARY . PREG_CLASS_CJK . ']))'; @@ -1156,7 +1156,7 @@ function search_excerpt($keys, $text) { $p = $match[0][1]; } else { - $info = search_simplify_excerpt_match($key, $text, $included[$key], $boundary); + $info = search_simplify_excerpt_match($key, $text, $included[$key], $boundary, $langcode); if ($info['where']) { $p = $info['where']; if ($info['keyword']) { @@ -1273,10 +1273,10 @@ function _search_excerpt_replace(&$text) { * array with element 'where' giving the position of the match, and element * 'keyword' giving the actual word found in the text at that position. */ -function search_simplify_excerpt_match($key, $text, $offset, $boundary) { +function search_simplify_excerpt_match($key, $text, $offset, $boundary, $langcode = NULL) { $pos = NULL; - $simplified_key = search_simplify($key); - $simplified_text = search_simplify($text); + $simplified_key = search_simplify($key, $langcode); + $simplified_text = search_simplify($text, $langcode); // Return immediately if simplified key or text are empty. if (!$simplified_key || !$simplified_text) { diff --git a/core/modules/search/tests/modules/search_langcode_test/search_langcode_test.info b/core/modules/search/tests/modules/search_langcode_test/search_langcode_test.info new file mode 100644 index 0000000..4a9de30 --- /dev/null +++ b/core/modules/search/tests/modules/search_langcode_test/search_langcode_test.info @@ -0,0 +1,6 @@ +name = "Test search entity langcode" +description = "Support module for search module testing." +package = Testing +version = VERSION +core = 8.x +hidden = TRUE diff --git a/core/modules/search/tests/modules/search_langcode_test/search_langcode_test.module b/core/modules/search/tests/modules/search_langcode_test/search_langcode_test.module new file mode 100644 index 0000000..f2bcd47 --- /dev/null +++ b/core/modules/search/tests/modules/search_langcode_test/search_langcode_test.module @@ -0,0 +1,5 @@ +