addtemplates-1313676-16.patch 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. diff --git a/search-api-page-result.tpl.php b/search-api-page-result.tpl.php
  2. new file mode 100644
  3. index 0000000..bf56e48
  4. --- /dev/null
  5. +++ b/search-api-page-result.tpl.php
  6. @@ -0,0 +1,56 @@
  7. +<?php
  8. +/**
  9. + * @file
  10. + * Default theme implementation for displaying a single search result.
  11. + *
  12. + * This template renders a single search result and is collected into
  13. + * search_api_page-results.tpl.php. This and the parent template are
  14. + * dependent to one another sharing the markup for definition lists.
  15. + *
  16. + * View Mode is set in the Search page settings. If you select
  17. + * "Themed as search results", then the child template will be used for
  18. + * theming the individual result. Any other view mode will bypass the
  19. + * child template.
  20. + *
  21. + * Available variables:
  22. + * - $url: URL of the result.
  23. + * - $title: Title of the result.
  24. + * - $snippet: A small preview of the result. Does not apply to user searches.
  25. + * - $info: String of all the meta information ready for print. Does not apply
  26. + * to user searches.
  27. + * - $info_split: Contains same data as $info, split into a keyed array.
  28. + * - $list_classes: CSS classes for this list element.
  29. + *
  30. + * Default keys within $info_split:
  31. + * - $info_split['user']: Author of the node linked to users profile. Depends
  32. + * on permission.
  33. + * - $info_split['date']: Last update of the node. Short formatted.
  34. + *
  35. + * Since $info_split is keyed, a direct print of the item is possible.
  36. + * This array does not apply to user searches so it is recommended to check
  37. + * for its existence before printing. The default keys of 'user' and
  38. + * 'date' always exist for node searches.
  39. + *
  40. + * To check for all available data within $info_split, use the code below.
  41. + * @code
  42. + * <?php print '<pre>'. check_plain(print_r($info_split, 1)) .'</pre>'; ?>
  43. + * @endcode
  44. + *
  45. + * @see template_preprocess()
  46. + * @see template_preprocess_search_result()
  47. + * @see template_process()
  48. + */
  49. +?>
  50. +<li<?php print $list_classes; ?>>
  51. + <h3 class="title">
  52. + <?php print $url ? l($title, $url['path'], $url['options']) : check_plain($title); ?>
  53. + </h3>
  54. + <div class="search-snippet-info">
  55. + <?php if ($snippet) : ?>
  56. + <p class="search-snippet"><?php print $snippet; ?></p>
  57. + <?php endif; ?>
  58. + <?php if ($info) : ?>
  59. + <p class="search-info"><?php print $info; ?></p>
  60. + <?php endif; ?>
  61. + </div>
  62. +</li>
  63. diff --git a/search-api-page-results.tpl.php b/search-api-page-results.tpl.php
  64. new file mode 100644
  65. index 0000000..9bbf343
  66. --- /dev/null
  67. +++ b/search-api-page-results.tpl.php
  68. @@ -0,0 +1,53 @@
  69. +<?php
  70. +/**
  71. + * @file
  72. + * Default theme implementation for displaying search results.
  73. + *
  74. + * This template collects each invocation of theme_search_result(). This and
  75. + * the child template are dependent to one another sharing the markup for
  76. + * definition lists.
  77. + *
  78. + * Note that modules and themes may implement their own search type and theme
  79. + * function completely bypassing this template.
  80. + *
  81. + * Available variables:
  82. + * - $result_count: Number of results.
  83. + * - $spellcheck: Possible spelling suggestions from Search spellcheck module.
  84. + * - $search_results: All results rendered as list items in a single HTML string.
  85. + * - $items: All results as it is rendered through search-result.tpl.php.
  86. + * - $search_performance: The number of results and how long the query took.
  87. + * - $sec: The number of seconds it took to run the query.
  88. + * - $pager: Row of control buttons for navigating between pages of results.
  89. + * - $index:
  90. + * - $keys: The keywords of the executed search.
  91. + * - $page_machine_name: Machine name of the current Search API Page.
  92. + *
  93. + * View Mode is set in the Search page settings. If you select
  94. + * "Themed as search results", then the child template will be used for
  95. + * theming the individual result. Any other view mode will bypass the
  96. + * child template.
  97. + *
  98. + * @see template_preprocess_search_api_page_results()
  99. + */
  100. +
  101. +?>
  102. +<?php if (!empty($result_count)) : ?>
  103. + <div class="search-api-page <?php print 'search-api-page-' . $page_machine_name . ' view-mode-' . $variables['view_mode'];?>">
  104. + <?php if ($result_count) : ?>
  105. + <?php print render($search_performance); ?>
  106. + <?php print render($spellcheck); ?>
  107. + <h2><?php print t('Search results');?></h2>
  108. +
  109. + <?php if ($variables['view_mode'] == 'search_api_page_result') : // Uses child template. ?>
  110. + <?php print render($search_results); ?>
  111. + <? else : // All other view modes (Teaser, Full content, RSS and so forth). ?>
  112. + <div class="search-results">
  113. + <?php print render(entity_view($index->item_type, $items, $variables['view_mode'])); ?>
  114. + </div>
  115. + <?php endif; ?>
  116. + <?php print $pager; ?>
  117. + <?php else : ?>
  118. + <h2><?php print t('Your search yielded no results.');?></h2>
  119. + <?php endif; ?>
  120. + </div>
  121. +<?php endif; ?>
  122. diff --git a/search_api_page.info b/search_api_page.info
  123. index 148c481..263ce24 100644
  124. --- a/search_api_page.info
  125. +++ b/search_api_page.info
  126. @@ -4,5 +4,6 @@ description = "Create search pages using Search API indexes."
  127. dependencies[] = search_api
  128. core = 7.x
  129. package = Search
  130. +stylesheets[all][] = search_api_page.css
  131. configure = admin/config/search/search_api/page
  132. diff --git a/search_api_page.module b/search_api_page.module
  133. index a9a00cb..6e15c45 100755
  134. --- a/search_api_page.module
  135. +++ b/search_api_page.module
  136. @@ -60,8 +60,12 @@ function search_api_page_theme() {
  137. 'items' => array(),
  138. 'view_mode' => 'search_api_page_result',
  139. 'keys' => '',
  140. + 'page_machine_name' => NULL,
  141. + 'spellcheck' => NULL,
  142. + 'pager' => NULL,
  143. ),
  144. 'file' => 'search_api_page.pages.inc',
  145. + 'template' => 'search-api-page-results',
  146. );
  147. $themes['search_api_page_result'] = array(
  148. 'variables' => array(
  149. @@ -69,14 +73,99 @@ function search_api_page_theme() {
  150. 'result' => NULL,
  151. 'item' => NULL,
  152. 'keys' => '',
  153. + 'list_classes' => '',
  154. ),
  155. 'file' => 'search_api_page.pages.inc',
  156. + 'template' => 'search-api-page-result',
  157. + );
  158. + $themes['search_performance'] = array(
  159. + 'render element' => 'element',
  160. + );
  161. + $themes['search_results_list'] = array(
  162. + 'render element' => 'element',
  163. );
  164. return $themes;
  165. }
  166. /**
  167. + * Implements theme for rendering search-performance
  168. + */
  169. +function theme_search_performance($variables) {
  170. + $element = array_shift($variables);
  171. +
  172. + return $element['#markup'];
  173. +}
  174. +
  175. +/**
  176. + * Returns HTML for a list of search results.
  177. + * Taken from theme_item_list().
  178. + *
  179. + * @param $variables
  180. + * An associative array containing:
  181. + * - items: An array of items to be displayed in the list. If an item is a
  182. + * string, then it is used as is. If an item is an array, then the "data"
  183. + * element of the array is used as the contents of the list item. If an item
  184. + * is an array with a "children" element, those children are displayed in a
  185. + * nested list. All other elements are treated as attributes of the list
  186. + * item element.
  187. + * - type: The type of list to return (e.g. "ul", "ol").
  188. + * - attributes: The attributes applied to the list element.
  189. + */
  190. +function theme_search_results_list($variables) {
  191. + // Pull Element array from the $variables array.
  192. + $variables = $variables['element'];
  193. +
  194. + $items = $variables['items']; // Full data
  195. + $type = $variables['type'];
  196. +
  197. + // CSS classes for ul
  198. + $attributes = (!empty($variables['attributes'])) ? $variables['attributes'] : array();
  199. + $attributes['class'] = array_merge(
  200. + array('item-list', 'search-results-list'),
  201. + (isset($attributes['class'])) ? $attributes['class'] : array()
  202. + );
  203. +
  204. + // Render items within a list
  205. + if (!empty($items)) {
  206. + $output = "<$type" . drupal_attributes($attributes) . '>';
  207. + $num_items = count($items);
  208. +
  209. + // Parse search results as tokens to access items with full data.
  210. + $i = 0;
  211. + foreach ($variables['results'] as $result) {
  212. + // Set css classes.
  213. + $item_attributes = array();
  214. + if ($i == 0) {
  215. + $item_attributes['class'][] = 'first';
  216. + }
  217. + if ($i == $num_items - 1) {
  218. + $item_attributes['class'][] = 'last';
  219. + }
  220. + (($i+1)%2) ? $item_attributes['class'][] = 'odd': $item_attributes['class'][] = 'even';
  221. +
  222. + // Define render array.
  223. + $data = theme(
  224. + 'search_api_page_result', array(
  225. + 'index' => $variables['index'], // Use full results index.
  226. + 'result' => $result,
  227. + 'item' => isset($items[$result['id']]) ?
  228. + $items[$result['id']] :
  229. + NULL,
  230. + 'keys' => $variables['keys'],
  231. + 'list_classes' => drupal_attributes($item_attributes),
  232. + )
  233. + );
  234. + $output .= $data . "\n";
  235. + $i++;
  236. + }
  237. + $output .= "</$type>";
  238. +
  239. + return $output;
  240. + }
  241. +}
  242. +
  243. +/**
  244. * Implements hook_permission().
  245. */
  246. function search_api_page_permission() {
  247. diff --git a/search_api_page.pages.inc b/search_api_page.pages.inc
  248. index 7b72a0d..c514909 100644
  249. --- a/search_api_page.pages.inc
  250. +++ b/search_api_page.pages.inc
  251. @@ -33,14 +33,18 @@ function search_api_page_view($id, $keys = NULL) {
  252. watchdog('search_api_page', 'An error occurred while executing a search: !msg.', array('!msg' => $e->getMessage()), WATCHDOG_ERROR, l(t('search page'), $_GET['q']));
  253. }
  254. - // If spellcheck results are returned then add them to the render array.
  255. + // Load spellcheck.
  256. if (isset($results['search_api_spellcheck'])) {
  257. - $ret['search_api_spellcheck']['#theme'] = 'search_api_spellcheck';
  258. - $ret['search_api_spellcheck']['#spellcheck'] = $results['search_api_spellcheck'];
  259. + $ret['results']['#spellcheck'] = array(
  260. + '#theme' => 'search_api_spellcheck',
  261. + '#spellcheck' => $results['search_api_spellcheck'],
  262. // Let the theme function know where the key is stored by passing its arg
  263. // number. We can work this out from the number of args in the page path.
  264. - $ret['search_api_spellcheck']['#options'] = array(
  265. - 'arg' => array(count(arg(NULL, $page->path))),
  266. + '#options' => array(
  267. + 'arg' => array(count(arg(NULL, $page->path))),
  268. + ),
  269. + '#prefix' => '<p class="search-api-spellcheck suggestion">',
  270. + '#suffix' => '</p>',
  271. );
  272. }
  273. @@ -49,11 +53,12 @@ function search_api_page_view($id, $keys = NULL) {
  274. $ret['results']['#results'] = $results;
  275. $ret['results']['#view_mode'] = isset($page->options['view_mode']) ? $page->options['view_mode'] : 'search_api_page_result';
  276. $ret['results']['#keys'] = $keys;
  277. + $ret['results']['#page_machine_name'] = $page->machine_name;
  278. + // Load pager.
  279. if ($results['result count'] > $page->options['per_page']) {
  280. pager_default_initialize($results['result count'], $page->options['per_page']);
  281. - $ret['pager']['#theme'] = 'pager';
  282. - $ret['pager']['#quantity'] = 9;
  283. + $ret['results']['#pager'] = theme('pager');
  284. }
  285. if (!empty($results['ignored'])) {
  286. @@ -108,96 +113,66 @@ function search_api_page_search_execute(Entity $page, $keys) {
  287. /**
  288. * Function for preprocessing the variables for the search_api_page_results
  289. - * theme.
  290. + * template.
  291. *
  292. * @param array $variables
  293. * An associative array containing:
  294. - * - index: The index this search was executed on.
  295. - * - results: An array of search results, as returned by
  296. + * - $index: The index this search was executed on.
  297. + * - $results: An array of search results, as returned by
  298. * SearchApiQueryInterface::execute().
  299. - * - keys: The keywords of the executed search.
  300. - */
  301. -function template_preprocess_search_api_page_results(array &$variables) {
  302. - if (!empty($variables['results']['results'])) {
  303. - $variables['items'] = $variables['index']->loadItems(array_keys($variables['results']['results']));
  304. - }
  305. -}
  306. -
  307. -/**
  308. - * Theme function for displaying search results.
  309. + * - $keys: The keywords of the executed search.
  310. *
  311. - * @param array $variables
  312. - * An associative array containing:
  313. - * - index: The index this search was executed on.
  314. - * - results: An array of search results, as returned by
  315. - * SearchApiQueryInterface::execute().
  316. - * - items: The loaded items for all results, in an array keyed by ID.
  317. - * - view_mode: The view mode to use for displaying the individual results,
  318. - * or the special mode "search_api_page_result" to use the theme function
  319. - * of the same name.
  320. - * - keys: The keywords of the executed search.
  321. + * @see search_api_page-results.tpl.php
  322. */
  323. -function theme_search_api_page_results(array $variables) {
  324. - drupal_add_css(drupal_get_path('module', 'search_api_page') . '/search_api_page.css');
  325. -
  326. - $index = $variables['index'];
  327. +function template_preprocess_search_api_page_results(array &$variables) {
  328. $results = $variables['results'];
  329. - $items = $variables['items'];
  330. $keys = $variables['keys'];
  331. - $output = '<p class="search-performance">' . format_plural($results['result count'],
  332. - 'The search found 1 result in @sec seconds.',
  333. - 'The search found @count results in @sec seconds.',
  334. - array('@sec' => round($results['performance']['complete'], 3))) . '</p>';
  335. -
  336. - if (!$results['result count']) {
  337. - $output .= "\n<h2>" . t('Your search yielded no results') . "</h2>\n";
  338. - return $output;
  339. - }
  340. -
  341. - $output .= "\n<h2>" . t('Search results') . "</h2>\n";
  342. -
  343. - if ($variables['view_mode'] == 'search_api_page_result') {
  344. - $output .= '<ol class="search-results">';
  345. - foreach ($results['results'] as $item) {
  346. - $output .= '<li class="search-result">' . theme('search_api_page_result', array('index' => $index, 'result' => $item, 'item' => isset($items[$item['id']]) ? $items[$item['id']] : NULL, 'keys' => $keys)) . '</li>';
  347. - }
  348. - $output .= '</ol>';
  349. - }
  350. - else {
  351. - // This option can only be set when the items are entities.
  352. - $output .= '<div class="search-results">';
  353. - $render = entity_view($index->item_type, $items, $variables['view_mode']);
  354. - $output .= render($render);
  355. - $output .= '</div>';
  356. - }
  357. + $variables['items'] = $variables['index']->loadItems(array_keys($variables['results']['results']));
  358. + $variables['result_count'] = $results['result count'];
  359. + $variables['sec'] = round($results['performance']['complete'], 3);
  360. + $variables['search_performance'] = array(
  361. + '#theme' => 'search_performance',
  362. + '#markup' => format_plural(
  363. + $results['result count'],
  364. + 'The search found 1 result in @sec seconds.',
  365. + 'The search found @count results in @sec seconds.',
  366. + array('@sec' => $variables['sec'])
  367. + ),
  368. + '#prefix' => '<p class="search-performance">',
  369. + '#suffix' => '</p>',
  370. + );
  371. - return $output;
  372. + $variables['search_results'] = array(
  373. + '#theme' => 'search_results_list',
  374. + 'results' => $variables['results']['results'],
  375. + 'items' => $variables['items'],
  376. + 'index' => $variables['index'],
  377. + 'type' => 'ul',
  378. + );
  379. }
  380. /**
  381. - * Theme function for displaying search results.
  382. + * Process variables for search-result.tpl.php.
  383. *
  384. - * @param array $variables
  385. - * An associative array containing:
  386. - * - index: The index this search was executed on.
  387. - * - result: One item of the search results, an array containing the keys
  388. - * 'id' and 'score'.
  389. - * - item: The loaded item corresponding to the result.
  390. - * - keys: The keywords of the executed search.
  391. + * The $variables array contains the following arguments:
  392. + * - $result
  393. + *
  394. + * @see search_api_page-result.tpl.php
  395. */
  396. -function theme_search_api_page_result(array $variables) {
  397. +function template_preprocess_search_api_page_result(&$variables) {
  398. $index = $variables['index'];
  399. - $id = $variables['result']['id'];
  400. + $variables['id'] = $variables['result']['id'];
  401. + $variables['excerpt'] = $variables['result']['excerpt'];
  402. $item = $variables['item'];
  403. $wrapper = $index->entityWrapper($item, FALSE);
  404. - $url = $index->datasource()->getItemUrl($item);
  405. - $name = $index->datasource()->getItemLabel($item);
  406. + $variables['url'] = $index->datasource()->getItemUrl($item);
  407. + $variables['title'] = $index->datasource()->getItemLabel($item);
  408. - if (!empty($variables['result']['excerpt'])) {
  409. - $text = $variables['result']['excerpt'];
  410. + if (!empty($variables['excerpt'])) {
  411. + $text = $variables['excerpt'];
  412. }
  413. else {
  414. $fields = $index->options['fields'];
  415. @@ -239,10 +214,24 @@ function theme_search_api_page_result(array $variables) {
  416. }
  417. }
  418. - $output = '<h3>' . ($url ? l($name, $url['path'], $url['options']) : check_plain($name)) . "</h3>\n";
  419. - if ($text) {
  420. - $output .= $text;
  421. + // Check for existence. User search does not include snippets.
  422. + $variables['snippet'] = isset($text) ? $text : '';
  423. +
  424. + // Meta information
  425. + global $language;
  426. + if (isset($item->language) && $item->language != $language->language && $item->language != LANGUAGE_NONE) {
  427. + $variables['title_attributes_array']['xml:lang'] = $item->language;
  428. + $variables['content_attributes_array']['xml:lang'] = $item->language;
  429. }
  430. - return $output;
  431. + $info = array();
  432. + if (!empty($item->name)) {
  433. + $info['user'] = $item->name;
  434. + }
  435. + if (!empty($item->created)) {
  436. + $info['date'] = format_date($item->created, 'short');
  437. + }
  438. + // Provide separated and grouped meta information..
  439. + $variables['info_split'] = $info;
  440. + $variables['info'] = implode(' - ', $info);
  441. }