search.views.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * @file
  4. * Provide views data and handlers for search.module.
  5. *
  6. * @ingroup views_module_handlers
  7. */
  8. /**
  9. * Implements hook_views_data().
  10. */
  11. function search_views_data() {
  12. // Basic table information.
  13. // Define the base group of this table. Fields that don't have a group
  14. // defined will go into this field by default.
  15. $data['search_index']['table']['group'] = t('Search');
  16. // For other base tables, explain how we join.
  17. $data['search_index']['table']['join'] = array(
  18. 'node' => array(
  19. 'left_field' => 'nid',
  20. 'field' => 'sid',
  21. ),
  22. );
  23. $data['search_total']['table']['join'] = array(
  24. 'node' => array(
  25. 'left_table' => 'search_index',
  26. 'left_field' => 'word',
  27. 'field' => 'word',
  28. ),
  29. 'users' => array(
  30. 'left_table' => 'search_index',
  31. 'left_field' => 'word',
  32. 'field' => 'word',
  33. ),
  34. );
  35. $data['search_dataset']['table']['join'] = array(
  36. 'node' => array(
  37. 'left_table' => 'search_index',
  38. 'left_field' => 'sid',
  39. 'field' => 'sid',
  40. 'extra' => array('search_index.type = search_dataset.type'),
  41. 'type' => 'INNER',
  42. ),
  43. 'users' => array(
  44. 'left_table' => 'search_index',
  45. 'left_field' => 'sid',
  46. 'field' => 'sid',
  47. 'extra' => array('search_index.type = search_dataset.type'),
  48. 'type' => 'INNER',
  49. ),
  50. );
  51. // ----------------------------------------------------------------
  52. // Fields.
  53. // Score.
  54. $data['search_index']['score'] = array(
  55. 'title' => t('Score'),
  56. 'help' => t('The score of the search item. This will not be used if the search filter is not also present.'),
  57. 'field' => array(
  58. 'handler' => 'views_handler_field_search_score',
  59. 'click sortable' => TRUE,
  60. 'float' => TRUE,
  61. 'no group by' => TRUE,
  62. ),
  63. // Information for sorting on a search score.
  64. 'sort' => array(
  65. 'handler' => 'views_handler_sort_search_score',
  66. 'no group by' => TRUE,
  67. ),
  68. );
  69. // Search node links: forward links.
  70. $data['search_node_links_from']['table']['group'] = t('Search');
  71. $data['search_node_links_from']['table']['join'] = array(
  72. 'node' => array(
  73. 'arguments' => array('search_node_links', 'node', 'nid', 'nid', NULL, 'INNER'),
  74. ),
  75. );
  76. $data['search_node_links_from']['sid'] = array(
  77. 'title' => t('Links from'),
  78. 'help' => t('Other nodes that are linked from the node.'),
  79. 'argument' => array(
  80. 'handler' => 'views_handler_argument_node_nid',
  81. ),
  82. 'filter' => array(
  83. 'handler' => 'views_handler_filter_equality',
  84. ),
  85. );
  86. // Search node links: backlinks.
  87. $data['search_node_links_to']['table']['group'] = t('Search');
  88. $data['search_node_links_to']['table']['join'] = array(
  89. 'node' => array(
  90. 'arguments' => array('search_node_links', 'node', 'nid', 'sid', NULL, 'INNER'),
  91. ),
  92. );
  93. $data['search_node_links_to']['nid'] = array(
  94. 'title' => t('Links to'),
  95. 'help' => t('Other nodes that link to the node.'),
  96. 'argument' => array(
  97. 'handler' => 'views_handler_argument_node_nid',
  98. ),
  99. 'filter' => array(
  100. 'handler' => 'views_handler_filter_equality',
  101. ),
  102. );
  103. // Search filter.
  104. $data['search_index']['keys'] = array(
  105. // The item it appears as on the UI,
  106. 'title' => t('Search Terms'),
  107. // The help that appears on the UI,
  108. 'help' => t('The terms to search for.'),
  109. // Information for searching terms using the full search syntax.
  110. 'filter' => array(
  111. 'handler' => 'views_handler_filter_search',
  112. 'no group by' => TRUE,
  113. ),
  114. 'argument' => array(
  115. 'handler' => 'views_handler_argument_search',
  116. 'no group by' => TRUE,
  117. ),
  118. );
  119. return $data;
  120. }
  121. /**
  122. * Implements hook_views_plugins().
  123. */
  124. function search_views_plugins() {
  125. return;
  126. // DISABLED. This currently doesn't work.
  127. // @todo Fix this.
  128. return array(
  129. 'module' => 'views',
  130. // This just tells our themes are elsewhere.
  131. 'row' => array(
  132. 'search' => array(
  133. 'title' => t('Search'),
  134. 'help' => t('Display the results with standard search view.'),
  135. 'handler' => 'views_plugin_row_search_view',
  136. 'theme' => 'views_view_row_search',
  137. 'path' => drupal_get_path('module', 'views') . '/modules/search',
  138. // Not necessary for most modules.
  139. 'base' => array('node'),
  140. // Only works with 'node' as base.
  141. 'type' => 'normal',
  142. ),
  143. 'views_handler_argument_search' => array(
  144. 'parent' => 'views_handler_argument',
  145. ),
  146. ),
  147. );
  148. }
  149. /**
  150. * Template helper for theme_views_view_row_search.
  151. */
  152. function template_preprocess_views_view_row_search(&$vars) {
  153. $vars['node'] = '';
  154. // Make sure var is defined.
  155. $nid = $vars['row']->nid;
  156. if (!is_numeric($nid)) {
  157. return;
  158. }
  159. // @todo Once the search row is fixed this node_load should be replace by a
  160. // node_load_multiple().
  161. $node = node_load($nid);
  162. if (empty($node)) {
  163. return;
  164. }
  165. // Build the node body.
  166. $node = node_build_content($node, FALSE, FALSE);
  167. $node->body = drupal_render($node->content);
  168. // Fetch comments for snippet.
  169. $node->body .= module_invoke('comment', 'nodeapi', $node, 'update index');
  170. // Fetch terms for snippet.
  171. $node->body .= module_invoke('taxonomy', 'nodeapi', $node, 'update index');
  172. $vars['url'] = url('node/' . $nid);
  173. $vars['title'] = check_plain($node->title);
  174. $info = array();
  175. $info['type'] = node_type_get_name($node);
  176. $info['user'] = theme('username', array('acccount' => $node));
  177. $info['date'] = format_date($node->changed, 'small');
  178. $extra = module_invoke_all('node_search_result', $node);
  179. if (isset($extra) && is_array($extra)) {
  180. $info = array_merge($info, $extra);
  181. }
  182. $vars['info_split'] = $info;
  183. $vars['info'] = implode(' - ', $info);
  184. $vars['node'] = $node;
  185. // @todo Where does the score come from?
  186. // $vars['score'] = $item->score;
  187. $vars['snippet'] = search_excerpt($vars['view']->value, $node->body);
  188. }