Commit c0d4ea28 authored by catch's avatar catch

Issue #2142987 by k4v: Multilingual node search bugs with title and language filtering.

parent 77dec69a
......@@ -94,7 +94,7 @@ class NodeSearch extends SearchPluginBase implements AccessibleInterface, Search
*/
protected $advanced = array(
'type' => array('column' => 'n.type'),
'langcode' => array('column' => 'n.langcode'),
'langcode' => array('column' => 'i.langcode'),
'author' => array('column' => 'n.uid'),
'term' => array('column' => 'ti.tid', 'join' => array('table' => 'taxonomy_index', 'alias' => 'ti', 'condition' => 'n.nid = ti.nid')),
);
......@@ -230,7 +230,7 @@ public function execute() {
foreach ($find as $item) {
// Render the node.
$node = $node_storage->load($item->sid);
$node = $node_storage->load($item->sid)->getTranslation($item->langcode);
$build = $node_render->view($node, 'search_result', $item->langcode);
unset($build['#theme']);
$node->rendered = drupal_render($build);
......@@ -249,7 +249,7 @@ public function execute() {
$results[] = array(
'link' => url($uri['path'], array_merge($uri['options'], array('absolute' => TRUE, 'language' => $language))),
'type' => check_plain($this->entityManager->getStorageController('node_type')->load($node->bundle())->label()),
'title' => $node->label($item->langcode),
'title' => $node->label(),
'user' => drupal_render($username),
'date' => $node->getChangedTime(),
'node' => $node,
......@@ -326,6 +326,7 @@ protected function indexNode(EntityInterface $node) {
$node_render = $this->entityManager->getViewBuilder('node');
foreach ($languages as $language) {
$node = $node->getTranslation($language->id);
// Render the node.
$build = $node_render->view($node, 'search_index', $language->id);
......@@ -558,5 +559,4 @@ public function submitConfigurationForm(array &$form, array &$form_state) {
}
}
}
}
......@@ -40,14 +40,16 @@ function setUp() {
'name' => 'Hungarian',
));
language_save($language);
$language = new Language(array(
'id' => 'sv',
'name' => 'Swedish',
));
language_save($language);
// Make the body field translatable.
// The parent class has already created the article and page content types.
// Make the body field translatable. The title is already translatable by
// definition. The parent class has already created the article and page
// content types.
$field = field_info_field('node', 'body');
$field->translatable = TRUE;
$field->save();
......@@ -56,16 +58,19 @@ function setUp() {
$default_format = filter_default_format();
$nodes = array(
array(
'title' => 'First node en',
'type' => 'page',
'body' => array(array('value' => $this->randomName(32), 'format' => $default_format)),
'langcode' => 'en',
),
array(
'title' => 'Second node this is the English title',
'type' => 'page',
'body' => array(array('value' => $this->randomName(32), 'format' => $default_format)),
'langcode' => 'en',
),
array(
'title' => 'Third node en',
'type' => 'page',
'body' => array(array('value' => $this->randomName(32), 'format' => $default_format)),
'langcode' => 'en',
......@@ -76,14 +81,15 @@ function setUp() {
$this->searchable_nodes[] = $this->drupalCreateNode($setting);
}
// Add a single translation to the second node.
$translation = $this->searchable_nodes[1]->getTranslation('hu');
$translation = $this->searchable_nodes[1]->addTranslation('hu', array('title' => 'Second node hu'));
$translation->body->value = $this->randomName(32);
$this->searchable_nodes[1]->save();
// Add two translations to the third node.
$translation = $this->searchable_nodes[2]->getTranslation('hu');
$translation = $this->searchable_nodes[2]->addTranslation('hu', array('title' => 'Third node this is the Hungarian title'));
$translation->body->value = $this->randomName(32);
$translation = $this->searchable_nodes[2]->getTranslation('sv');
$translation = $this->searchable_nodes[2]->addTranslation('sv', array('title' => 'Third node sv'));
$translation->body->value = $this->randomName(32);
$this->searchable_nodes[2]->save();
}
......@@ -118,16 +124,19 @@ function testSearchingMultilingualFieldValues() {
$plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
$plugin->updateIndex();
search_update_totals();
foreach ($this->searchable_nodes as $node) {
// Each searchable node that we created contains values in the body field
// in one or more languages. Let's pick the last language variant from the
// body array and execute a search using that as a search keyword.
$languages = $node->getTranslationLanguages();
$plugin->setSearch($node->getTranslation(end($languages)->id)->body->value, array(), array());
// Do the search and assert the results.
$search_result = $plugin->execute();
// See whether we get the same node as a result.
$this->assertEqual($search_result[0]['node']->id(), $node->id(), 'The search has resulted the correct node.');
}
// This should find two results for the second and third node.
$plugin->setSearch('English OR Hungarian', array(), array());
$search_result = $plugin->execute();
$this->assertEqual($search_result[0]['title'], 'Third node this is the Hungarian title', 'The search finds the correct Hungarian title.');
$this->assertEqual($search_result[1]['title'], 'Second node this is the English title', 'The search finds the correct English title.');
// Now filter for Hungarian results only.
$plugin->setSearch('English OR Hungarian', array('f' => array('langcode:hu')), array());
$search_result = $plugin->execute();
$this->assertEqual(count($search_result), 1, 'The search found only one result');
$this->assertEqual($search_result[0]['title'], 'Third node this is the Hungarian title', 'The search finds the correct Hungarian title.');
}
}
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