Issue #511594 by sxnc, jhodgdon, Gábor Hojtsy: Added hook_search_preprocess() needs to be language-specific.
parent
86d2fc3ed7
commit
ebaab15cf0
|
@ -1584,7 +1584,7 @@ function node_search_execute($keys = NULL, $conditions = NULL) {
|
||||||
'node' => $node,
|
'node' => $node,
|
||||||
'extra' => $extra,
|
'extra' => $extra,
|
||||||
'score' => $item->calculated_score,
|
'score' => $item->calculated_score,
|
||||||
'snippet' => search_excerpt($keys, $node->rendered),
|
'snippet' => search_excerpt($keys, $node->rendered, $item->langcode),
|
||||||
'langcode' => $node->langcode,
|
'langcode' => $node->langcode,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,99 @@
|
||||||
|
<?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,
|
'node' => $node,
|
||||||
'extra' => $extra,
|
'extra' => $extra,
|
||||||
'score' => $item->calculated_score,
|
'score' => $item->calculated_score,
|
||||||
'snippet' => search_excerpt($keys, $node->rendered),
|
'snippet' => search_excerpt($keys, $node->rendered, $item->langcode),
|
||||||
'langcode' => $node->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
|
* from between two HTML tags or from the search query. It will not contain
|
||||||
* any HTML entities or HTML tags.
|
* any HTML entities or HTML tags.
|
||||||
*
|
*
|
||||||
|
* @param $langcode
|
||||||
|
* The language code of the entitiy that has been found.
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
* The text after preprocessing. Note that if your module decides not to alter
|
* The text after preprocessing. Note that if your module decides not to alter
|
||||||
* the text, it should return the original text. Also, after preprocessing,
|
* the text, it should return the original text. Also, after preprocessing,
|
||||||
|
@ -312,8 +315,16 @@ function hook_search_page($results) {
|
||||||
*
|
*
|
||||||
* @ingroup search
|
* @ingroup search
|
||||||
*/
|
*/
|
||||||
function hook_search_preprocess($text) {
|
function hook_search_preprocess($text, $langcode = NULL) {
|
||||||
// Do processing on $text
|
// 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;
|
return $text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -430,7 +430,7 @@ function search_update_totals() {
|
||||||
*
|
*
|
||||||
* @see hook_search_preprocess()
|
* @see hook_search_preprocess()
|
||||||
*/
|
*/
|
||||||
function search_simplify($text) {
|
function search_simplify($text, $langcode = NULL) {
|
||||||
// Decode entities to UTF-8
|
// Decode entities to UTF-8
|
||||||
$text = decode_entities($text);
|
$text = decode_entities($text);
|
||||||
|
|
||||||
|
@ -438,7 +438,7 @@ function search_simplify($text) {
|
||||||
$text = drupal_strtolower($text);
|
$text = drupal_strtolower($text);
|
||||||
|
|
||||||
// Call an external processor for word handling.
|
// Call an external processor for word handling.
|
||||||
search_invoke_preprocess($text);
|
search_invoke_preprocess($text, $langcode);
|
||||||
|
|
||||||
// Simple CJK handling
|
// Simple CJK handling
|
||||||
if (config('search.settings')->get('index.overlap_cjk')) {
|
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.
|
* 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__);
|
$last = &drupal_static(__FUNCTION__);
|
||||||
$lastsplit = &drupal_static(__FUNCTION__ . ':lastsplit');
|
$lastsplit = &drupal_static(__FUNCTION__ . ':lastsplit');
|
||||||
|
|
||||||
|
@ -531,7 +531,7 @@ function search_index_split($text) {
|
||||||
return $lastsplit;
|
return $lastsplit;
|
||||||
}
|
}
|
||||||
// Process words
|
// Process words
|
||||||
$text = search_simplify($text);
|
$text = search_simplify($text, $langcode);
|
||||||
$words = explode(' ', $text);
|
$words = explode(' ', $text);
|
||||||
|
|
||||||
// Save last keyword result
|
// Save last keyword result
|
||||||
|
@ -554,9 +554,9 @@ function _search_index_truncate(&$text) {
|
||||||
/**
|
/**
|
||||||
* Invokes hook_search_preprocess() in modules.
|
* 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) {
|
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;
|
$value = $linktitle;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$words = search_index_split($value);
|
$words = search_index_split($value, $langcode);
|
||||||
foreach ($words as $word) {
|
foreach ($words as $word) {
|
||||||
// Add word to accumulator
|
// Add word to accumulator
|
||||||
$accum .= $word . ' ';
|
$accum .= $word . ' ';
|
||||||
|
@ -1109,7 +1109,7 @@ function search_data($keys, $module, $conditions = NULL) {
|
||||||
* @return
|
* @return
|
||||||
* A string containing HTML for the excerpt.
|
* 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.
|
// 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 . ']))';
|
$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];
|
$p = $match[0][1];
|
||||||
}
|
}
|
||||||
else {
|
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']) {
|
if ($info['where']) {
|
||||||
$p = $info['where'];
|
$p = $info['where'];
|
||||||
if ($info['keyword']) {
|
if ($info['keyword']) {
|
||||||
|
@ -1273,10 +1273,10 @@ function _search_excerpt_replace(&$text) {
|
||||||
* array with element 'where' giving the position of the match, and element
|
* array with element 'where' giving the position of the match, and element
|
||||||
* 'keyword' giving the actual word found in the text at that position.
|
* '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;
|
$pos = NULL;
|
||||||
$simplified_key = search_simplify($key);
|
$simplified_key = search_simplify($key, $langcode);
|
||||||
$simplified_text = search_simplify($text);
|
$simplified_text = search_simplify($text, $langcode);
|
||||||
|
|
||||||
// Return immediately if simplified key or text are empty.
|
// Return immediately if simplified key or text are empty.
|
||||||
if (!$simplified_key || !$simplified_text) {
|
if (!$simplified_key || !$simplified_text) {
|
||||||
|
|
|
@ -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
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?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;
|
||||||
|
}
|
Loading…
Reference in New Issue