Commit ebaab15c authored by webchick's avatar webchick

Issue #511594 by sxnc, jhodgdon, Gábor Hojtsy: Added hook_search_preprocess()...

Issue #511594 by sxnc, jhodgdon, Gábor Hojtsy: Added hook_search_preprocess() needs to be language-specific.
parent 86d2fc3e
......@@ -1584,7 +1584,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,
);
}
......
<?php
/**
* @file
* Definition of Drupal\search\Tests\SearchPreprocessLangcodeTest.
*/
namespace Drupal\search\Tests;
/**
* Test search_simplify() on every Unicode character, and some other cases.
*/
class SearchPreprocessLangcodeTest extends SearchTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('search_langcode_test');
public static function getInfo() {
return array(
'name' => '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 hook_search_preprocess().
$this->assertText('Langcode Preprocess Test: en');
}
/**
* Tests stemming 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');
}
}
......@@ -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) {
// Do processing on $text
function hook_search_preprocess($text, $langcode = NULL) {
// If the langcode is set to 'en' then add variations of the word "testing"
// which can also be found during English language searches.
if (isset($langcode) && $langcode == 'en') {
// Add the alternate verb forms for the word "testing".
if ($text == 'we are testing') {
$text .= ' test tested';
}
}
return $text;
}
......
......@@ -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) {
......
name = "Test search entity langcode"
description = "Support module for search module testing."
package = Testing
version = VERSION
core = 8.x
hidden = TRUE
<?php
/**
* @file
* Test module setting up two tests, one for checking if the entity $langcode is
* being passed on and another one sets up the alternate verb forms for the
* stemming test.
*/
/**
* Implements hook_search_preprocess().
*/
function search_langcode_test_search_preprocess($text, $langcode = NULL) {
if (isset($langcode) && $langcode == 'en') {
// Add the alternate verb forms for the word "testing".
if ($text == 'we are testing') {
$text .= ' test tested';
}
// Prints the langcode for testPreprocessLangcode().
else {
drupal_set_message('Langcode Preprocess Test: ' . $langcode);
}
}
// Prints the langcode for testPreprocessLangcode().
elseif (isset($langcode)) {
drupal_set_message('Langcode Preprocess Test: ' . $langcode);
}
return $text;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment