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..743a106 --- /dev/null +++ b/core/modules/search/lib/Drupal/search/Tests/SearchPreprocessLangcodeTest.php @@ -0,0 +1,92 @@ + '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 hook_search_preprocess() returns the correct langcode. + */ + 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'); + } + + /** + * Small stemming test for hook_search_preprocess(). + */ + function testPreprocessStemming() { + // Create a node. + $node = $this->drupalCreateNode(array('title' => 'we are testing', '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' => 'testing'); + $this->drupalPost('search/node', $edit, t('Advanced search')); + + // Check if the node has been found. + $this->assertText('Search results'); + $this->assertText('we are testing'); + + // Search for the same node using a different query. + $edit = array('or' => 'test'); + $this->drupalPost('search/node', $edit, t('Advanced search')); + + // Check if the node has been found. + $this->assertText('Search results'); + $this->assertText('we are testing'); + + } +} diff --git a/core/modules/search/search.api.php b/core/modules/search/search.api.php index 4c24475..6148385 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,8 +315,16 @@ function hook_search_page($results) { * * @ingroup search */ -function hook_search_preprocess($text) { +function hook_search_preprocess($text, $langcode = NULL) { // Do processing on $text + + // $langcode example: + // If the langcode is set and is 'en' then add 'en_text' to each + // $text. Using this everything with $langcode 'en' will also + // be found by 'en_text'. + if (isset($langcode) && $langcode == 'en') { + $text .= 'en_text'; + } return $text; } diff --git a/core/modules/search/search.module b/core/modules/search/search.module index 7613465..315ae7a 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')) { @@ -523,7 +523,7 @@ function search_expand_cjk($matches) { /** * Simplifies and splits a string into tokens for indexing. */ -function search_index_split($text) { +function search_index_split($text, $langcode = NULL) { $last = &drupal_static(__FUNCTION__); $lastsplit = &drupal_static(__FUNCTION__ . ':lastsplit'); @@ -531,7 +531,7 @@ function search_index_split($text) { return $lastsplit; } // Process words - $text = search_simplify($text); + $text = search_simplify($text, $langcode); $words = explode(' ', $text); // Save last keyword result @@ -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); } } @@ -668,7 +668,7 @@ function search_index($sid, $module, $text, $langcode) { $value = $linktitle; } } - $words = search_index_split($value); + $words = search_index_split($value, $langcode); foreach ($words as $word) { // Add word to accumulator $accum .= $word . ' '; @@ -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..97d9aee --- /dev/null +++ b/core/modules/search/tests/modules/search_langcode_test/search_langcode_test.module @@ -0,0 +1,16 @@ +