diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index a6230f2..bbbb01a 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1349,7 +1349,7 @@ function comment_node_predelete(Node $node) { /** * Implements hook_node_update_index(). */ -function comment_node_update_index(Node $node) { +function comment_node_update_index(Node $node, $langcode) { $index_comments = &drupal_static(__FUNCTION__); if ($index_comments === NULL) { @@ -1377,7 +1377,7 @@ function comment_node_update_index(Node $node) { if ($node->comment && $cids = comment_get_thread($node, $mode, $comments_per_page)) { $comments = comment_load_multiple($cids); comment_prepare_thread($comments); - $build = comment_view_multiple($comments, $node); + $build = comment_view_multiple($comments, $node, $langcode); return drupal_render($build); } } diff --git a/core/modules/node/node.api.php b/core/modules/node/node.api.php index 9719fc0..8fd7fa8 100644 --- a/core/modules/node/node.api.php +++ b/core/modules/node/node.api.php @@ -658,6 +658,8 @@ function hook_node_prepare(Drupal\node\Node $node) { * * @param Drupal\node\Node $node * The node being displayed in a search result. + * @param $langcode + * Language code of result being displayed. * * @return array * Extra information to be displayed with search result. This information @@ -670,7 +672,7 @@ function hook_node_prepare(Drupal\node\Node $node) { * * @ingroup node_api_hooks */ -function hook_node_search_result(Drupal\node\Node $node) { +function hook_node_search_result(Drupal\node\Node $node, $langcode) { $comments = db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = :nid', array('nid' => $node->nid))->fetchField(); return array('comment' => format_plural($comments, '1 comment', '@count comments')); } @@ -722,13 +724,15 @@ function hook_node_update(Drupal\node\Node $node) { * * @param Drupal\node\Node $node * The node being indexed. + * @param $langcode + * Language code of the variant of the node being indexed. * * @return string * Additional node information to be indexed. * * @ingroup node_api_hooks */ -function hook_node_update_index(Drupal\node\Node $node) { +function hook_node_update_index(Drupal\node\Node $node, $langcode) { $text = ''; $comments = db_query('SELECT subject, comment, format FROM {comment} WHERE nid = :nid AND status = :status', array(':nid' => $node->nid, ':status' => COMMENT_PUBLISHED)); foreach ($comments as $comment) { diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 42d968d..b1ab281 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -1598,22 +1598,23 @@ function node_search_execute($keys = NULL, $conditions = NULL) { foreach ($find as $item) { // Render the node. $node = node_load($item->sid); - $build = node_view($node, 'search_result'); + $build = node_view($node, 'search_result', $item->langcode); unset($build['#theme']); $node->rendered = drupal_render($build); // Fetch comments for snippet. - $node->rendered .= ' ' . module_invoke('comment', 'node_update_index', $node); + $node->rendered .= ' ' . module_invoke('comment', 'node_update_index', $node, $item->langcode); - $extra = module_invoke_all('node_search_result', $node); + $extra = module_invoke_all('node_search_result', $node, $item->langcode); + $language = language_load($item->langcode); $uri = entity_uri('node', $node); $results[] = array( - 'link' => url($uri['path'], array_merge($uri['options'], array('absolute' => TRUE))), + 'link' => url($uri['path'], array_merge($uri['options'], array('absolute' => TRUE, 'language' => $language))), 'type' => check_plain(node_type_get_name($node)), - 'title' => $node->title, + 'title' => $node->label($item->langcode), 'user' => theme('username', array('account' => $node)), - 'date' => $node->changed, + 'date' => $node->get('changed', $item->langcode), 'node' => $node, 'extra' => $extra, 'score' => $item->calculated_score, @@ -2630,21 +2631,23 @@ function _node_index_node(Node $node) { // results half-life calculation. variable_set('node_cron_last', $node->changed); - // Render the node. - $build = node_view($node, 'search_index'); - unset($build['#theme']); - $node->rendered = drupal_render($build); + foreach ($node->translations() as $language) { + // Render the node in this language. + $build = node_view($node, 'search_index', $language->langcode); + unset($build['#theme']); + $node->rendered = drupal_render($build); - $text = '

' . check_plain($node->title) . '

' . $node->rendered; + $text = '

' . check_plain($node->label($language->langcode)) . '

' . $node->rendered; - // Fetch extra data normally not visible - $extra = module_invoke_all('node_update_index', $node); - foreach ($extra as $t) { - $text .= $t; - } + // Fetch extra data normally not visible. + $extra = module_invoke_all('node_update_index', $node, $language->langcode); + foreach ($extra as $t) { + $text .= $t; + } - // Update index - search_index($node->nid, 'node', $text); + // Update index. + search_index($node->nid, 'node', $text, $langcode); + } } /** diff --git a/core/modules/search/search.api.php b/core/modules/search/search.api.php index 0f43460..58a6b35 100644 --- a/core/modules/search/search.api.php +++ b/core/modules/search/search.api.php @@ -206,7 +206,7 @@ function hook_search_execute($keys = NULL, $conditions = NULL) { // Insert special keywords. $query->setOption('type', 'n.type'); - $query->setOption('language', 'n.language'); + $query->setOption('langcode', 'n.langcode'); if ($query->setOption('term', 'ti.tid')) { $query->join('taxonomy_index', 'ti', 'n.nid = ti.nid'); } @@ -224,28 +224,30 @@ function hook_search_execute($keys = NULL, $conditions = NULL) { ->execute(); $results = array(); foreach ($find as $item) { - // Build the node body. + // Render the node. $node = node_load($item->sid); - node_build_content($node, 'search_result'); - $node->body = drupal_render($node->content); + $build = node_view($node, 'search_result', $item->langcode); + unset($build['#theme']); + $node->rendered = drupal_render($build); // Fetch comments for snippet. - $node->rendered .= ' ' . module_invoke('comment', 'node_update_index', $node); - // Fetch terms for snippet. - $node->rendered .= ' ' . module_invoke('taxonomy', 'node_update_index', $node); + $node->rendered .= ' ' . module_invoke('comment', 'node_update_index', $node, $item->langcode); - $extra = module_invoke_all('node_search_result', $node); + $extra = module_invoke_all('node_search_result', $node, $item->langcode); + $language = language_load($item->langcode); + $uri = entity_uri('node', $node); $results[] = array( - 'link' => url('node/' . $item->sid, array('absolute' => TRUE)), + 'link' => url($uri['path'], array_merge($uri['options'], array('absolute' => TRUE, 'language' => $language))), 'type' => check_plain(node_type_get_name($node)), - 'title' => $node->title, + 'title' => $node->label($item->langcode), 'user' => theme('username', array('account' => $node)), - 'date' => $node->changed, + 'date' => $node->get('changed', $item->langcode), 'node' => $node, 'extra' => $extra, 'score' => $item->calculated_score, - 'snippet' => search_excerpt($keys, $node->body), + 'snippet' => search_excerpt($keys, $node->rendered), + 'langcode' => $node->langcode, ); } return $results; diff --git a/core/modules/search/search.install b/core/modules/search/search.install index c450f05..803f5bd 100644 --- a/core/modules/search/search.install +++ b/core/modules/search/search.install @@ -28,6 +28,12 @@ function search_schema() { 'default' => 0, 'description' => 'Search item ID, e.g. node ID for nodes.', ), + 'langcode' => array( + 'type' => 'varchar', + 'length' => '12', + 'not null' => TRUE, + 'description' => 'The {languages}.langcode of the item variant.', + ), 'type' => array( 'type' => 'varchar', 'length' => 16, @@ -48,7 +54,7 @@ function search_schema() { 'description' => 'Set to force node reindexing.', ), ), - 'primary key' => array('sid', 'type'), + 'primary key' => array('sid', 'langcode', 'type'), ); $schema['search_index'] = array( @@ -68,6 +74,12 @@ function search_schema() { 'default' => 0, 'description' => 'The {search_dataset}.sid of the searchable item to which the word belongs.', ), + 'langcode' => array( + 'type' => 'varchar', + 'length' => '12', + 'not null' => TRUE, + 'description' => 'The {languages}.langcode of the item variant.', + ), 'type' => array( 'type' => 'varchar', 'length' => 16, @@ -81,7 +93,7 @@ function search_schema() { ), ), 'indexes' => array( - 'sid_type' => array('sid', 'type'), + 'sid_type' => array('sid', 'langcode', 'type'), ), 'foreign keys' => array( 'search_dataset' => array( @@ -92,7 +104,7 @@ function search_schema() { ), ), ), - 'primary key' => array('word', 'sid', 'type'), + 'primary key' => array('word', 'sid', 'langcode', 'type'), ); $schema['search_total'] = array( diff --git a/core/modules/search/search.module b/core/modules/search/search.module index f567dae..72f84a2 100644 --- a/core/modules/search/search.module +++ b/core/modules/search/search.module @@ -315,20 +315,33 @@ function _search_menu_access($name) { * @param $module * (optional) The machine-readable name of the module for the item to remove * from the search index. + * @param $reindex + * (optional) Boolean to specify whether reindexing happens. + * @param $langcode + * (optional) Language code for the operation. If not provided, all + * index records for the $sid will be deleted. */ -function search_reindex($sid = NULL, $module = NULL, $reindex = FALSE) { +function search_reindex($sid = NULL, $module = NULL, $reindex = FALSE, $langcode = NULL) { if ($module == NULL && $sid == NULL) { module_invoke_all('search_reset'); } else { - db_delete('search_dataset') + $query = db_delete('search_dataset') ->condition('sid', $sid) - ->condition('type', $module) - ->execute(); - db_delete('search_index') + ->condition('type', $module); + if (!empty($langcode)) { + $query->condition('langcode', $langcode); + } + $query->execute(); + + $query = db_delete('search_index') ->condition('sid', $sid) - ->condition('type', $module) - ->execute(); + ->condition('type', $module); + if (!empty($langcode)) { + $query->condition('langcode', $langcode); + } + $query->execute(); + // Don't remove links if re-indexing. if (!$reindex) { db_delete('search_node_links') @@ -557,10 +570,12 @@ function search_invoke_preprocess(&$text) { * that implements hook_search_info()). * @param $text * The content of this item. Must be a piece of HTML or plain text. + * @param $langcode + * Language code for text being indexed. * * @ingroup search */ -function search_index($sid, $module, $text) { +function search_index($sid, $module, $text, $langcode) { $minimum_word_size = variable_get('minimum_word_size', 3); // Link matching @@ -701,12 +716,13 @@ function search_index($sid, $module, $text) { $tag = !$tag; } - search_reindex($sid, $module, TRUE); + search_reindex($sid, $module, TRUE, $langcode); // Insert cleaned up data into dataset db_insert('search_dataset') ->fields(array( 'sid' => $sid, + 'langcode' => $langcode, 'type' => $module, 'data' => $accum, 'reindex' => 0, @@ -722,6 +738,7 @@ function search_index($sid, $module, $text) { ->key(array( 'word' => $word, 'sid' => $sid, + 'langcode' => $langcode, 'type' => $module, )) ->fields(array('score' => $score))