search.views.inc

  1. 3.x modules/search.views.inc
  2. 2.x modules/search.views.inc

Provide views data and handlers for search.module

File

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