ds_search.module

  1. cis7 sites/all/modules/ulmus/ds/modules/ds_search/ds_search.module
  2. cle7 sites/all/modules/ulmus/ds/modules/ds_search/ds_search.module
  3. ecd7 sites/all/modules/ulmus/ds/modules/ds_search/ds_search.module
  4. elmsmedia7 sites/all/modules/ulmus/ds/modules/ds_search/ds_search.module
  5. harmony7 sites/all/modules/ulmus/ds/modules/ds_search/ds_search.module
  6. icor7 sites/all/modules/ulmus/ds/modules/ds_search/ds_search.module
  7. meedjum_blog7 sites/all/modules/ulmus/ds/modules/ds_search/ds_search.module
  8. mooc7 sites/all/modules/ulmus/ds/modules/ds_search/ds_search.module

Display Suite search.

Functions

Namesort descending Description
ds_build_shared_page_variables Build shared page variables.
ds_entity_view_fallback DS entity view callback.
ds_search_apachesolr_index_document_build Implements hook_apachesolr_index_document_build().
ds_search_apachesolr_query_alter Implements hook_apachesolr_query_alter().
ds_search_apachesolr_search_page_alter Implements hook_apachesolr_search_page_alter(&$build, $search_page).
ds_search_ds_fields_info Implements hook_ds_fields_info().
ds_search_extra_info Returns the info field, just like default search.
ds_search_extra_variables Return the extra variables.
ds_search_form_apachesolr_search_custom_page_search_form_alter Implements hook_form_FORM_ID_alter().
ds_search_form_search_form_alter Implements hook_form_FORM_ID_alter().
ds_search_get_keys Save or get the search keys.
ds_search_help Implements hook_help().
ds_search_invoke_node_search Invoke a given search hook on the node module.
ds_search_menu Implements hook_menu().
ds_search_process_results Process results on behalf of Apache Solr.
ds_search_search_execute Implements hook_search_execute().
ds_search_search_info Implements hook_search_info().
ds_search_search_page Implements hook_search_page().
ds_search_search_status Implements hook_search_status().
ds_search_snippet Returns the snippet field.
ds_search_theme Implements hook_theme().
ds_search_update_index Implements hook_node_update_index().
node_ds_search_execute Search on behalf of Drupal Core.
theme_ds_search_page Search page theming.
_ds_search_group_by_type Helper function to group by type.
_ds_search_group_by_type_multisearch Helper function to perform grouping on Apache Solr multisearch.

File

sites/all/modules/ulmus/ds/modules/ds_search/ds_search.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Display Suite search.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function ds_search_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/structure/ds/list/search':
  12. $output = '<dl>';
  13. $output .= '<dt>' . t('Display Suite defines its own search type for search. You need to enable it at !url when you are going to use Drupal core search. You do not have to enable and use it when using the Apachesolr module. Search results will be themed on the default Apachesolr pages.', array('!url' => l('search settings', 'admin/config/search/settings'))) . '</dt>';
  14. $output .= '</dl>';
  15. return $output;
  16. }
  17. }
  18. /**
  19. * Implements hook_menu().
  20. */
  21. function ds_search_menu() {
  22. $items = array();
  23. $items['admin/structure/ds/list/search'] = array(
  24. 'title' => 'Search',
  25. 'description' => 'Configure search settings.',
  26. 'page callback' => 'drupal_get_form',
  27. 'page arguments' => array('ds_search_settings'),
  28. 'access arguments' => array('admin_display_suite'),
  29. 'file' => 'includes/ds_search.admin.inc',
  30. 'type' => MENU_LOCAL_TASK,
  31. );
  32. return $items;
  33. }
  34. /**
  35. * Implements hook_theme().
  36. */
  37. function ds_search_theme() {
  38. return array(
  39. 'ds_search_page' => array(),
  40. 'ds_search_group_by_type_settings' => array(
  41. 'render element' => 'element',
  42. 'file' => 'includes/ds_search.admin.inc',
  43. ),
  44. );
  45. }
  46. /**
  47. * Search page theming.
  48. */
  49. function theme_ds_search_page($build) {
  50. // Check on empty search results.
  51. if (empty($build['search_results'])) {
  52. // Alter the title and extra variables.
  53. if (!empty($build['search_title'])) {
  54. $build['search_title']['#markup'] = '<h2>' . t('Your search yielded no results') . '</h2>';
  55. unset($build['search_extra']);
  56. }
  57. $build['search_empty'] = array('#markup' => search_help('search#noresults', drupal_help_arg()));
  58. }
  59. $build['search_results']['#sorted'] = TRUE;
  60. return $build;
  61. }
  62. /**
  63. * Implements hook_ds_fields_info().
  64. */
  65. function ds_search_ds_fields_info($entity_type) {
  66. $fields = array();
  67. if ($entity_type == 'node') {
  68. $fields['node']['search_snippet'] = array(
  69. 'title' => t('Search snippet'),
  70. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  71. 'function' => 'ds_search_snippet',
  72. 'ui_limit' => array('*|' . variable_get('ds_search_view_mode', 'search_result')),
  73. );
  74. $fields['node']['search_info'] = array(
  75. 'title' => t('Search info'),
  76. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  77. 'function' => 'ds_search_extra_info',
  78. 'ui_limit' => array('*|' . variable_get('ds_search_view_mode', 'search_result')),
  79. );
  80. }
  81. if (isset($fields[$entity_type])) {
  82. return array($entity_type => $fields[$entity_type]);
  83. }
  84. return;
  85. }
  86. /**
  87. * Returns the snippet field.
  88. */
  89. function ds_search_snippet($field) {
  90. // Apache Solr
  91. if (isset($field['entity']->search_snippet)) {
  92. return $field['entity']->search_snippet;
  93. }
  94. // Original node snippet
  95. elseif (isset($field['entity']->snippet)) {
  96. return $field['entity']->snippet;
  97. }
  98. }
  99. /**
  100. * Returns the info field, just like default search.
  101. */
  102. function ds_search_extra_info($field) {
  103. $info = array();
  104. $info['user'] = theme('username', array('account' => $field['entity']));
  105. $info['date'] = format_date($field['entity']->changed, 'short');
  106. if (isset($field['entity']->search_extra) && is_array($field['entity']->search_extra)) {
  107. $info = array_merge($info, $field['entity']->search_extra);
  108. }
  109. return implode(' - ', $info);
  110. }
  111. /**
  112. * Implements hook_search_info().
  113. */
  114. function ds_search_search_info() {
  115. return array(
  116. 'title' => 'Content',
  117. 'path' => variable_get('ds_search_path', 'content'),
  118. );
  119. }
  120. /**
  121. * Implements hook_node_update_index().
  122. */
  123. function ds_search_update_index() {
  124. ds_search_invoke_node_search('update_index');
  125. }
  126. /**
  127. * Implements hook_search_status().
  128. */
  129. function ds_search_search_status() {
  130. return ds_search_invoke_node_search('search_status');
  131. }
  132. /**
  133. * Implements hook_search_execute().
  134. */
  135. function ds_search_search_execute($keys = NULL, $conditions = NULL) {
  136. // Save the keys in case we need them later on.
  137. ds_search_get_keys($keys);
  138. // We will call an extra function which handles the actual search.
  139. // In some cases, we simply copied a lot from the original hook,
  140. // because some modules already called drupal_render and were unsetting
  141. // the #theme key. By using our own search info type, we can call
  142. // hook_search_page ourselves and be as flexible as we need to be.
  143. $ds_search_type = variable_get('ds_search_type', 'node') . '_ds_search_execute';
  144. // Make sure the function exists.
  145. if (function_exists($ds_search_type)) {
  146. return $ds_search_type($keys, $conditions);
  147. }
  148. }
  149. /**
  150. * Save or get the search keys.
  151. */
  152. function ds_search_get_keys($keys = NULL) {
  153. static $run, $saved_keys = FALSE;
  154. if (!$run) {
  155. $run = TRUE;
  156. $saved_keys = $keys;
  157. }
  158. else {
  159. return $saved_keys;
  160. }
  161. }
  162. /**
  163. * Invoke a given search hook on the node module.
  164. *
  165. * @param $hook
  166. * Hook to invoke.
  167. */
  168. function ds_search_invoke_node_search($hook) {
  169. $enabled_search_modules = variable_get('search_active_modules', array());
  170. // If node search is enabled, core is invoking it.
  171. if (isset($enabled_search_modules['node']) && $enabled_search_modules['node'] === 'node') {
  172. return;
  173. }
  174. else {
  175. $ds_search_type = variable_get('ds_search_type', 'node');
  176. if ($ds_search_type != 'node') {
  177. return;
  178. }
  179. }
  180. return module_invoke('node', $hook);
  181. }
  182. /**
  183. * DS entity view callback.
  184. *
  185. * Straight copy from Entity API module with fallback to Drupal core
  186. * view callbacks for nodes, files and maybe others later.
  187. */
  188. function ds_entity_view_fallback($entity_type, $entities, $view_mode = 'full', $langcode = NULL, $page = NULL) {
  189. // Use the entity module in case it's enabled.
  190. if (module_exists('entity')) {
  191. return entity_view($entity_type, $entities, $view_mode, $langcode, $page);
  192. }
  193. else {
  194. if ($entity_type == 'node') {
  195. return node_view_multiple($entities, $view_mode);
  196. }
  197. elseif ($entity_type == 'file' && function_exists('file_view_multiple')) {
  198. return file_view_multiple($entities, $view_mode);
  199. }
  200. }
  201. }
  202. /**
  203. * Implements hook_search_page().
  204. */
  205. function ds_search_search_page($results) {
  206. // Build shared variables.
  207. $build = array('#type' => 'node');
  208. ds_build_shared_page_variables($build);
  209. $i = 0;
  210. // Multi site Apache Solr support.
  211. if (variable_get('ds_search_apachesolr_multisite') && variable_get('ds_search_type', 'node') == 'apachesolr_search') {
  212. $build['search_results'] = $results;
  213. }
  214. else {
  215. foreach ($results as $id => $result) {
  216. // Use default search result theming for file in case it's configured.
  217. if ($result->entity_type == 'file' && variable_get('ds_search_file_render', FALSE)) {
  218. // Get the file type from the file entity module. We'll overwrite
  219. // the bundle here then as that makes more sense as a suggestion.
  220. if (function_exists('file_get_type')) {
  221. $type = file_get_type($result);
  222. $result->original_result['bundle'] = $type;
  223. }
  224. $build['search_results'][] = array(
  225. '#weight' => $i++,
  226. '#markup' => theme('search_result', array('result' => $result->original_result, 'module' => 'apachesolr_search')),
  227. );
  228. continue;
  229. }
  230. $entity_type = isset($result->entity_type) ? $result->entity_type : 'node';
  231. $data = ds_entity_view_fallback($entity_type, array($result->entity_id => $result), variable_get('ds_search_view_mode', 'search_result'));
  232. // Check that we got an actual result back.
  233. if ($data) {
  234. $data = reset($data);
  235. $data[$result->entity_id]['#weight'] = $i++;
  236. $build['search_results'][] = $data[$result->entity_id];
  237. }
  238. }
  239. }
  240. // Group by type.
  241. if (variable_get('ds_search_group_by_type') && variable_get('ds_search_group_by_type_settings') && !empty($build['search_results'])) {
  242. _ds_search_group_by_type($build);
  243. }
  244. else {
  245. // Provide zebra striping for results that are not grouped.
  246. $parity = 'odd';
  247. foreach ($build['search_results'] as $id => $result) {
  248. // We need to check on the entity type, as the container
  249. // where the object is stored in doesn't necessarily reflect
  250. // the name of the entity type.
  251. if (!empty($build['search_results'][$id]['#entity_type'])) {
  252. switch ($build['search_results'][$id]['#entity_type']) {
  253. case 'taxonomy_term':
  254. $key = '#term';
  255. break;
  256. default:
  257. $key = '#' . $build['search_results'][$id]['#entity_type'];
  258. break;
  259. }
  260. $build['search_results'][$id][$key]->ds_search_zebra = $parity;
  261. }
  262. // Let parity change always.
  263. $parity = $parity == 'odd' ? 'even' : 'odd';
  264. }
  265. }
  266. // Apache Solr multisearch grouping.
  267. if (variable_get('ds_search_apachesolr_multisite') && variable_get('ds_search_apachesolr_multisite_group') && variable_get('ds_search_type', 'node') == 'apachesolr_search') {
  268. _ds_search_group_by_type_multisearch($build);
  269. }
  270. return theme('ds_search_page', $build);
  271. }
  272. /**
  273. * Helper function to group by type.
  274. */
  275. function _ds_search_group_by_type(&$build) {
  276. $settings = variable_get('ds_search_group_by_type_settings');
  277. foreach ($build['search_results'] as $id => $result) {
  278. if ($settings[$result['#bundle']]['status']) {
  279. // Type group.
  280. if (!isset($build['search_results'][$result['#bundle']])) {
  281. $type = $settings[$result['#bundle']]['wrapper'];
  282. $title = check_plain(t($settings[$result['#bundle']]['label']));
  283. $class = 'group-result group-result-' . strtr($result['#bundle'], '_', '-');
  284. $parity[$result['#bundle']] = 'odd';
  285. $build['search_results'][$result['#bundle']] = array(
  286. '#type' => $type,
  287. '#title' => $title,
  288. '#weight' => $settings[$result['#bundle']]['weight'],
  289. '#attributes' => array(
  290. 'class' => array($class),
  291. ),
  292. );
  293. if ($type == 'markup') {
  294. $build['search_results'][$result['#bundle']]['#prefix'] = '<div class="' . $class . '">' . ((!empty($title)) ? ' <h2>' . $title . '</h2>' : '');
  295. $build['search_results'][$result['#bundle']]['#suffix'] = '</div>';
  296. }
  297. }
  298. // Move result into the wrapper of its type and unset previous.
  299. $build['search_results'][$result['#bundle']][$id] = $result;
  300. unset($build['search_results'][$id]);
  301. // Add the parity to the result to enable correct zebra striping.
  302. $build['search_results'][$result['#bundle']][$id]['#node']->ds_search_zebra = $parity[$result['#bundle']];
  303. $parity[$result['#bundle']] = $parity[$result['#bundle']] == 'odd' ? 'even' : 'odd';
  304. }
  305. else {
  306. // Other group.
  307. if (!isset($build['search_results']['ds-other'])) {
  308. $title = check_plain(t(variable_get('ds_search_group_by_type_other', 'Other')));
  309. $type = variable_get('ds_search_group_by_type_other_wrapper', 'fieldset');
  310. $class = 'group-result group-result-other';
  311. $parity['ds-other'] = 'odd';
  312. $build['search_results']['ds-other'] = array(
  313. '#type' => $type,
  314. '#title' => $title,
  315. '#weight' => 100,
  316. '#attributes' => array(
  317. 'class' => array($class),
  318. ),
  319. );
  320. if ($type == 'markup') {
  321. $build['search_results']['ds-other']['#prefix'] = '<div class="' . $class . '">' . ((!empty($title)) ? '<h2>' . $title . '</h2>' : '');
  322. $build['search_results']['ds-other']['#suffix'] = '</div>';
  323. }
  324. }
  325. // Move result into other wrapper and unset previous.
  326. $build['search_results']['ds-other'][$id] = $result;
  327. unset($build['search_results'][$id]);
  328. // Add the parity to the result to enable correct zebra striping.
  329. $build['search_results']['ds-other'][$id]['#node']->ds_search_parity = $parity['ds-other'];
  330. $parity['ds-other'] = $parity['ds-other'] == 'odd' ? 'even' : 'odd';
  331. }
  332. }
  333. }
  334. /**
  335. * Helper function to perform grouping on Apache Solr multisearch.
  336. */
  337. function _ds_search_group_by_type_multisearch(&$build) {
  338. $site_counter = array();
  339. $conf_array = array();
  340. $config = explode("\n", variable_get('ds_search_apachesolr_multisite_group_config'));
  341. foreach ($config as $weight => $conf) {
  342. $conf = trim($conf);
  343. if (empty($conf)) {
  344. continue;
  345. }
  346. $site_conf = explode('|', $conf);
  347. $conf_array[$site_conf[0]] = array(
  348. 'label' => $site_conf[1],
  349. 'wrapper' => $site_conf[2],
  350. 'weight' => $weight,
  351. );
  352. }
  353. // Iterate over results.
  354. foreach ($build['search_results'] as $id => $result) {
  355. if (!isset($build['search_results'][$result['#site_hash']])) {
  356. $class = 'group-result group-result-' . strtr($result['#site_hash'], '_', '-');
  357. $build['search_results'][$result['#site_hash']] = array(
  358. '#type' => 'fieldset',
  359. '#weight' => $conf_array[$result['#site_hash']]['weight'],
  360. '#attributes' => array(
  361. 'class' => array($class),
  362. ),
  363. );
  364. // Create site counter.
  365. $site_counter[$result['#site_hash']] = array(
  366. 'counter' => 0,
  367. 'title' => $conf_array[$result['#site_hash']]['label'],
  368. 'type' => $conf_array[$result['#site_hash']]['wrapper'],
  369. 'class' => $class,
  370. );
  371. }
  372. // Move result into other wrapper and unset previous. Also count for
  373. // every site so we can populate @total_per_site later on.
  374. $site_counter[$result['#site_hash']]['counter']++;
  375. $build['search_results'][$result['#site_hash']][$id] = $result;
  376. unset($build['search_results'][$id]);
  377. }
  378. // Site counter.
  379. foreach ($site_counter as $hash => $values) {
  380. $title = check_plain(t($values['title'], array('!total_per_site' => format_plural($values['counter'], '1 result', '@count results'))));
  381. if ($values['type'] == 'div') {
  382. $build['search_results'][$hash]['#prefix'] = '<div class="' . $values['class'] . '">' . ((!empty($title)) ? '<h2>' . $title . '</h2>' : '');
  383. $build['search_results'][$hash]['#suffix'] = '</div>';
  384. }
  385. else {
  386. $build['search_results'][$hash]['#title'] = $title;
  387. }
  388. }
  389. }
  390. /**
  391. * Search on behalf of Drupal Core.
  392. */
  393. function node_ds_search_execute($keys = NULL, $conditions = NULL) {
  394. // Build matching conditions
  395. $query = db_select('search_index', 'i', array('target' => 'slave'))->extend('SearchQuery')->extend('PagerDefault');
  396. $query->join('node', 'n', 'n.nid = i.sid');
  397. $query
  398. ->condition('n.status', 1)
  399. ->addTag('node_access')
  400. ->searchExpression($keys, 'node');
  401. // Language.
  402. if (variable_get('ds_search_language', FALSE)) {
  403. global $language;
  404. $query->condition('n.language', $language->language);
  405. }
  406. // Insert special keywords.
  407. $query->setOption('type', 'n.type');
  408. $query->setOption('language', 'n.language');
  409. if ($query->setOption('term', 'ti.tid')) {
  410. $query->join('taxonomy_index', 'ti', 'n.nid = ti.nid');
  411. }
  412. // Only continue if the first pass query matches.
  413. if (!$query->executeFirstPass()) {
  414. return array();
  415. }
  416. // Add the ranking expressions.
  417. _node_rankings($query);
  418. $limit = variable_get('ds_search_node_limit', 10);
  419. $query->limit($limit);
  420. // Load results.
  421. $find = $query->execute();
  422. $results = array();
  423. foreach ($find as $item) {
  424. $node = node_load($item->sid);
  425. $node->entity_type = 'node';
  426. $node->entity_id = $item->sid;
  427. $node->search_extra = module_invoke_all('node_search_result', $node);
  428. // Only build a node search snippet if this field is actually being used.
  429. $fields = ds_get_field_settings($node->entity_type, $node->type, 'search_result');
  430. if (!empty($fields) && isset($fields['search_snippet'])) {
  431. // Because the 'search_result' display is being built right now (and because it is being overridden by Display Suite),
  432. // it is necessary to use the 'search_index' display for rendered field content.
  433. $build = node_view($node, 'search_index');
  434. unset($build['#theme']);
  435. // Render the node.
  436. $rendered = drupal_render($build);
  437. // Attach extra information to the rendered output.
  438. $rendered .= ' ' . $node->search_extra;
  439. // Generate the snippet based on rendered content.
  440. $node->snippet = search_excerpt($keys, $rendered);
  441. }
  442. $results[$item->sid] = $node;
  443. }
  444. return $results;
  445. }
  446. /**
  447. * Override search results page for users.
  448. */
  449. if (variable_get('ds_user_override_search_page', FALSE)) {
  450. function user_search_page($results) {
  451. $build = array('#type' => 'user');
  452. global $base_url;
  453. ds_build_shared_page_variables($build);
  454. $uids = array();
  455. foreach ($results as $key => $result) {
  456. $uid = FALSE;
  457. // Try to get the uid from the $result['link'];
  458. $path = explode('/', $result['link']);
  459. $uid = end($path);
  460. // Lookup drupal path, we are most likely having an alias.
  461. if (!is_numeric($uid)) {
  462. $path = str_replace($base_url . '/', '', $result['link']);
  463. $alias = drupal_get_normal_path($path);
  464. $path = explode('/', $alias);
  465. $uid = end($path);
  466. }
  467. if (is_numeric($uid)) {
  468. $uids[] = $uid;
  469. }
  470. // Return all uids.
  471. if (!empty($uids)) {
  472. $accounts = user_load_multiple($uids);
  473. foreach ($accounts as $account) {
  474. $build['search_results'][$account->uid] = user_view($account, variable_get('ds_search_view_mode', 'search_result'));
  475. }
  476. }
  477. }
  478. // Return output.
  479. return theme('ds_search_page', $build);
  480. }
  481. }
  482. /**
  483. * Build shared page variables.
  484. *
  485. * @param $build
  486. * The build array.
  487. */
  488. function ds_build_shared_page_variables(&$build) {
  489. // Search results title.
  490. if (variable_get('ds_search_show_title', FALSE)) {
  491. $build['search_title'] = array('#markup' => '<h2>' . t('Search results') . '</h2>');
  492. }
  493. // Extra variables.
  494. if (variable_get('ds_search_variables', 'none') != 'none') {
  495. $build['search_extra'] = array('#markup' => '<div class="ds-search-extra">' . ds_search_extra_variables(arg(2)) . '</div>');
  496. }
  497. // Search results.
  498. $build['search_results'] = array();
  499. // Pager.
  500. $build['search_pager'] = array('#markup' => theme('pager', array('tags' => NULL)));
  501. // CSS and JS.
  502. if (variable_get('ds_search_highlight', FALSE)) {
  503. drupal_add_css(drupal_get_path('module', 'ds_search') . '/css/ds_search.theme.css');
  504. drupal_add_js(drupal_get_path('module', 'ds_search') . '/js/ds_search.js');
  505. drupal_add_js(array(
  506. 'ds_search' => array(
  507. 'selector' => check_plain(variable_get('ds_search_highlight_selector', '.view-mode-search_result')),
  508. 'search' => check_plain(arg(2)),
  509. ),
  510. ), 'setting');
  511. }
  512. }
  513. /**
  514. * Return the extra variables.
  515. */
  516. function ds_search_extra_variables($arg_keys = NULL) {
  517. $type = variable_get('ds_search_variables', 'none');
  518. // Define the number of results being shown on a page.
  519. // We rely on the apache solr rows for now.
  520. $items_per_page = variable_get('apachesolr_rows', 10);
  521. // Get the current page.
  522. $current_page = isset($_REQUEST['page']) ? $_REQUEST['page']+1 : 1;
  523. // Get the total number of results from the $GLOBALS.
  524. $total = isset($GLOBALS['pager_total_items'][0]) ? $GLOBALS['pager_total_items'][0] : 0;
  525. // Perform calculation
  526. $start = $items_per_page * $current_page - ($items_per_page - 1);
  527. $end = $items_per_page * $current_page;
  528. if ($end > $total) $end = $total;
  529. // Get the search keys.
  530. $keys = empty($arg_keys) ? trim(ds_search_get_keys()) : $arg_keys;
  531. // Send the right extra variable.
  532. switch ($type) {
  533. case 'search_totals':
  534. return format_plural($total, 'One result', 'Total results: @total.', array('@total' => $total));
  535. break;
  536. case 'search_totals_plus_keywords':
  537. return format_plural($total, 'Your search for "<strong>@search</strong>" gave back 1 result.',
  538. 'Your search for "<strong>@search</strong>" gave back @count results.',
  539. array('@search' => $keys));
  540. break;
  541. case 'search_totals_from_to_end':
  542. return format_plural($total, 'Displaying @start - @end of 1 result.',
  543. 'Displaying @start - @end of @count results.',
  544. array('@start' => $start, '@end' => $end));
  545. break;
  546. }
  547. }
  548. /**
  549. * Implements hook_form_FORM_ID_alter().
  550. */
  551. function ds_search_form_search_form_alter(&$form, $form_state) {
  552. if (variable_get('ds_search_type', 'node') == 'node' && isset($form['module']) && $form['module']['#value'] == 'ds_search') {
  553. if (variable_get('ds_search_node_form_alter', FALSE)) {
  554. $form['module']['#value'] = 'node';
  555. node_form_search_form_alter($form, $form_state);
  556. }
  557. }
  558. }
  559. /**
  560. * Implements hook_form_FORM_ID_alter().
  561. */
  562. function ds_search_form_apachesolr_search_custom_page_search_form_alter(&$form, $form_state) {
  563. if (variable_get('ds_search_apachesolr_hide_current_filters', FALSE)) {
  564. $form['basic']['retain-filters']['#type'] = 'value';
  565. $form['basic']['retain-filters']['#value'] = variable_get('ds_search_apachesolr_current_filters_default', FALSE);
  566. }
  567. }
  568. /**
  569. * Implements hook_apachesolr_index_document_build().
  570. */
  571. function ds_search_apachesolr_index_document_build(ApacheSolrDocument $document, $entity) {
  572. // Apache Solr multisite support. Render the node already here.
  573. if (variable_get('ds_search_apachesolr_multisite')) {
  574. ob_start();
  575. $element = node_view($entity, variable_get('ds_search_view_mode', 'search_result'));
  576. print drupal_render($element);
  577. $output = ob_get_contents();
  578. ob_end_clean();
  579. $document->addField('tm_ds_search_result', $output);
  580. }
  581. }
  582. /**
  583. * Implements hook_apachesolr_query_alter().
  584. */
  585. function ds_search_apachesolr_query_alter($query) {
  586. // Apache Solr multisite support.
  587. if (variable_get('ds_search_apachesolr_multisite') && variable_get('ds_search_type', 'node') == 'apachesolr_search') {
  588. // Site hash.
  589. $query->addParam('fl', 'hash');
  590. // Rendered search result.
  591. $query->addParam('fl', 'tm_ds_search_result');
  592. // Make sure this site's search results are first.
  593. if (variable_get('ds_search_apachesolr_multisite_boost')) {
  594. $hash = apachesolr_site_hash();
  595. $query->addParam('bq', 'hash:' . $hash . '^' . variable_get('ds_search_apachesolr_multisite_boost_nr', 100));
  596. }
  597. }
  598. // Search per language.
  599. if (variable_get('ds_search_language', FALSE)) {
  600. global $language;
  601. $query->addFilter('ss_language', $language->language);
  602. }
  603. }
  604. /**
  605. * Process results on behalf of Apache Solr.
  606. */
  607. function ds_search_process_results($results) {
  608. $processed_results = array();
  609. if (is_array($results) && !empty($results)) {
  610. foreach ($results as $result) {
  611. $load = entity_load($result['fields']['entity_type'], array($result['fields']['entity_id']));
  612. $entity = reset($load);
  613. // Add the snippet, url and extra info on the object.
  614. $entity->search_snippet = $result['snippet'];
  615. $entity->search_extra = $result['extra'];
  616. $entity->search_as_url = $result['fields']['url'];
  617. $entity->entity_type = $result['fields']['entity_type'];
  618. $entity->entity_id = $result['fields']['entity_id'];
  619. // Add the original result on the entity too in case this is a file
  620. // entity. Attachments have brittle support as the file entity only
  621. // exists in media 1.x or file entity 2.x. Because of that, we're
  622. // most likely will render files through theme('search_result').
  623. if ($result['fields']['entity_type'] == 'file') {
  624. $entity->original_result = $result;
  625. }
  626. // Apache Solr multisite support.
  627. if (variable_get('ds_search_apachesolr_multisite')) {
  628. // Pass along the uri path in case some people want to
  629. // do cool stuff themselves.
  630. $entity->uri['path'] = $entity->search_as_url;
  631. $entity->uri['options'] = array();
  632. // Prefix with site hash so we don't override same id's.
  633. $markup = $result['fields']['tm_ds_search_result'][0];
  634. $processed_results[$result['fields']['id'] . '-' . $result['fields']['entity_id']] = array(
  635. '#markup' => $markup,
  636. '#site_hash' => $result['fields']['hash'],
  637. );
  638. }
  639. else {
  640. $processed_results[$result['fields']['id'] . '-' . $result['fields']['entity_id']] = $entity;
  641. }
  642. }
  643. }
  644. return $processed_results;
  645. }
  646. /**
  647. * Implements hook_apachesolr_search_page_alter(&$build, $search_page).
  648. */
  649. function ds_search_apachesolr_search_page_alter(&$build, $search_page) {
  650. if (!empty($build['search_results']['#results'])) {
  651. $results = ds_search_process_results($build['search_results']['#results']);
  652. $build['search_results'] = ds_search_search_page($results);
  653. }
  654. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.