- Patch #164032 by Crell, Chris Kennedy, dmitrig01, WimLeers, dvessel et al: tpl-ified the search module.

6.x
Dries Buytaert 2007-10-31 18:06:38 +00:00
parent 9a9ea37403
commit 6c85fdc6ab
7 changed files with 252 additions and 64 deletions

View File

@ -0,0 +1,37 @@
<?php
// $Id$
/**
* @file search-block-form.tpl.php
* Default theme implementation for displaying a search form within a block region.
*
* Available variables:
* - $search_form: The complete search form ready for print.
* - $search: Array of keyed search elements. Can be used to print each form
* element separately.
*
* Default keys within $search:
* - $search['search_block_form']: Text input area wrapped in a div.
* - $search['submit']: Form submit button.
* - $search['hidden']: Hidden form elements. Used to validate forms when submitted.
*
* Since $search is keyed, a direct print of the form element is possible.
* Modules can add to the search form so it is recommended to check for their
* existance before printing. The default keys will always exist.
*
* <?php if (isset($search['extra_field'])): ?>
* <div class="extra-field">
* <?php print $search['extra_field']; ?>
* </div>
* <?php endif; ?>
*
* To check for all available data within $search, use the code below.
*
* <?php print '<pre>'. check_plain(print_r($search, 1)) .'</pre>'; ?>
*
* @see template_preprocess_search_block_form()
*/
?>
<div class="container-inline">
<?php print $search_form; ?>
</div>

View File

@ -0,0 +1,59 @@
<?php
// $Id$
/**
* @file search-result.tpl.php
* Default theme implementation for displaying a single search result.
*
* This template renders a single search result and is collected into
* search-results.tpl.php. This and the parent template are
* dependent to one another sharing the markup for definition lists.
*
* Available variables:
* - $url: URL of the result.
* - $title: Title of the result.
* - $snippet: A small preview of the result. Does not apply to user searches.
* - $info: String of all the meta information ready for print. Does not apply
* to user searches.
* - $info_split: Contains same data as $info split into a keyed array.
* - $type: The type of search, e.g., "node" or "user".
*
* Default keys within $info_split:
* - $info_split['type']: Node type.
* - $info_split['user']: Author of the node linked to users profile. Depends
* on permission.
* - $info_split['date']: Last update of the node. Short formatted.
* - $info_split['comment']: Number of comments output as "% comments", %
* being the count. Depends on comment.module.
* - $info_split['upload']: Number of attachments output as "% attachments", %
* being the count. Depends on upload.module.
*
* Since $info_split is keyed, a direct print of the item is possible.
* This array does not apply to user searches so it is recommended to check
* for their existance before printing. The default keys of 'type', 'user' and
* 'date' always exist for node searches. Modules may provide other data.
*
* <?php if (isset($info_split['comment'])) : ?>
* <span class="info-comment">
* <?php print $info_split['comment']; ?>
* </span>
* <?php endif; ?>
*
* To check for all available data within $info_split, use the code below.
*
* <?php print '<pre>'. check_plain(print_r($info_split, 1)) .'</pre>'; ?>
*
* @see template_preprocess_search_result()
*/
?>
<dt class="title">
<a href="<?php print $url; ?>"><?php print $title; ?></a>
</dt>
<dd>
<?php if ($snippet) : ?>
<p class="search-snippet"><?php print $snippet; ?></p>
<?php endif; ?>
<?php if ($info) : ?>
<p class="search-info"><?php print $info; ?></p>
<?php endif; ?>
</dd>

View File

@ -0,0 +1,27 @@
<?php
// $Id$
/**
* @file search-results.tpl.php
* Default theme implementation for displaying search results.
*
* This template collects each invocation of theme_search_result(). This and
* the child template are dependant to one another sharing the markup for
* definition lists.
*
* Note that modules may implement their own search type and theme function
* completely bypassing this template.
*
* Available variables:
* - $search_results: All results as it is rendered through
* search-result.tpl.php
* - $type: The type of search, e.g., "node" or "user".
*
*
* @see template_preprocess_search_results()
*/
?>
<dl class="search-results <?php print $type; ?>-results">
<?php print $search_results; ?>
</dl>
<?php print $pager; ?>

View File

@ -0,0 +1,38 @@
<?php
// $Id$
/**
* @file search-theme-form.tpl.php
* Default theme implementation for displaying a search form directly into the
* theme layout. Not to be confused with the search block or the search page.
*
* Available variables:
* - $search_form: The complete search form ready for print.
* - $search: Array of keyed search elements. Can be used to print each form
* element separately.
*
* Default keys within $search:
* - $search['search_theme_form']: Text input area wrapped in a div.
* - $search['submit']: Form submit button.
* - $search['hidden']: Hidden form elements. Used to validate forms when submitted.
*
* Since $search is keyed, a direct print of the form element is possible.
* Modules can add to the search form so it is recommended to check for their
* existance before printing. The default keys will always exist.
*
* <?php if (isset($search['extra_field'])): ?>
* <div class="extra-field">
* <?php print $search['extra_field']; ?>
* </div>
* <?php endif; ?>
*
* To check for all available data within $search, use the code below.
*
* <?php print '<pre>'. check_plain(print_r($search, 1)) .'</pre>'; ?>
*
* @see template_preprocess_search_theme_form()
*/
?>
<div id="search" class="container-inline">
<?php print $search_form; ?>
</div>

View File

@ -3,12 +3,6 @@
.search-form { .search-form {
margin-bottom: 1em; margin-bottom: 1em;
} }
.search-form p {
margin-top: 0;
margin-bottom: 0.2em;
padding-top: 0;
padding-bottom: 0;
}
.search-form input { .search-form input {
margin-top: 0; margin-top: 0;
margin-bottom: 0; margin-bottom: 0;

View File

@ -118,17 +118,21 @@ function search_theme() {
return array( return array(
'search_theme_form' => array( 'search_theme_form' => array(
'arguments' => array('form' => NULL), 'arguments' => array('form' => NULL),
'template' => 'search-theme-form',
), ),
'search_block_form' => array( 'search_block_form' => array(
'arguments' => array('form' => NULL), 'arguments' => array('form' => NULL),
'template' => 'search-block-form',
), ),
'search_item' => array( 'search_result' => array(
'arguments' => array('item' => NULL, 'type' => NULL), 'arguments' => array('result' => NULL, 'type' => NULL),
'file' => 'search.pages.inc', 'file' => 'search.pages.inc',
'template' => 'search-result',
), ),
'search_page' => array( 'search_results' => array(
'arguments' => array('results' => NULL, 'type' => NULL), 'arguments' => array('results' => NULL, 'type' => NULL),
'file' => 'search.pages.inc', 'file' => 'search.pages.inc',
'template' => 'search-results',
), ),
); );
} }
@ -940,8 +944,7 @@ function search_form(&$form_state, $action = '', $keys = '', $type = NULL, $prom
* @see theme_search_box_form(). * @see theme_search_box_form().
*/ */
function search_box(&$form_state, $form_id) { function search_box(&$form_state, $form_id) {
// Use search_keys instead of keys to avoid ID conflicts with the search block. $form[$form_id] = array(
$form[$form_id .'_keys'] = array(
'#title' => t('Search this site'), '#title' => t('Search this site'),
'#type' => 'textfield', '#type' => 'textfield',
'#size' => 15, '#size' => 15,
@ -960,25 +963,61 @@ function search_box(&$form_state, $form_id) {
*/ */
function search_box_form_submit($form, &$form_state) { function search_box_form_submit($form, &$form_state) {
$form_id = $form['form_id']['#value']; $form_id = $form['form_id']['#value'];
$form_state['redirect'] = 'search/node/'. trim($form_state['values'][$form_id .'_keys']); $form_state['redirect'] = 'search/node/'. trim($form_state['values'][$form_id]);
} }
/** /**
* Theme the theme search form. * Process variables for search-theme-form.tpl.php.
* *
* @ingroup themeable * The $variables array contains the following arguments:
* - $form
*
* @see search-theme-form.tpl.php
*/ */
function theme_search_theme_form($form) { function template_preprocess_search_theme_form(&$variables) {
return '<div id="search" class="container-inline">'. drupal_render($form) .'</div>'; $variables['search'] = array();
$hidden = array();
// Provide variables named after form keys so themers can print each element independently.
foreach (element_children($variables['form']) as $key) {
$type = $variables['form'][$key]['#type'];
if ($type == 'hidden' || $type == 'token') {
$hidden[] = drupal_render($variables['form'][$key]);
}
else {
$variables['search'][$key] = drupal_render($variables['form'][$key]);
}
}
// Hidden form elements have no value to themers. No need for separation.
$variables['search']['hidden'] = implode($hidden);
// Collect all form elements to make it easier to print the whole form.
$variables['search_form'] = implode($variables['search']);
} }
/** /**
* Theme the block search form. * Process variables for search-block-form.tpl.php.
* *
* @ingroup themeable * The $variables array contains the following arguments:
* - $form
*
* @see search-block-form.tpl.php
*/ */
function theme_search_block_form($form) { function template_preprocess_search_block_form(&$variables) {
return '<div class="container-inline">'. drupal_render($form) .'</div>'; $variables['search'] = array();
$hidden = array();
// Provide variables named after form keys so themers can print each element independently.
foreach (element_children($variables['form']) as $key) {
$type = $variables['form'][$key]['#type'];
if ($type == 'hidden' || $type == 'token') {
$hidden[] = drupal_render($variables['form'][$key]);
}
else {
$variables['search'][$key] = drupal_render($variables['form'][$key]);
}
}
// Hidden form elements have no value to themers. No need for separation.
$variables['search']['hidden'] = implode($hidden);
// Collect all form elements to make it easier to print the whole form.
$variables['search_form'] = implode($variables['search']);
} }
/** /**
@ -994,7 +1033,7 @@ function search_data($keys = NULL, $type = 'node') {
return module_invoke($type, 'search_page', $results); return module_invoke($type, 'search_page', $results);
} }
else { else {
return theme('search_page', $results, $type); return theme('search_results', $results, $type);
} }
} }
} }

View File

@ -50,63 +50,58 @@ function search_view($type = 'node') {
} }
/** /**
* Format the result page of a search query. * Process variables for search-results.tpl.php.
* *
* Modules may implement hook_search_page() in order to override this default * The $variables array contains the following arguments:
* function to display search results. In that case it is expected they provide * - $results
* their own themeable functions. * - $type
* *
* @param $results * @see search-results.tpl.php
* All search result as returned by hook_search().
* @param $type
* The type of item found, such as "user" or "node".
*
* @ingroup themeable
*/ */
function theme_search_page($results, $type) { function template_preprocess_search_results(&$variables) {
$output = '<dl class="search-results">'; $variables['search_results'] = '';
foreach ($variables['results'] as $result) {
foreach ($results as $entry) { $variables['search_results'] .= theme('search_result', $result, $variables['type']);
$output .= theme('search_item', $entry, $type);
} }
$output .= '</dl>'; $variables['pager'] = theme('pager', NULL, 10, 0);
$output .= theme('pager', NULL, 10, 0); // Provide alternate search results template.
$variables['template_files'][] = 'search-results-'. $variables['type'];
return $output;
} }
/** /**
* Format a single result entry of a search query. This function is normally * Process variables for search-result.tpl.php.
* called by theme_search_page() or hook_search_page().
* *
* @param $item * The $variables array contains the following arguments:
* A single search result as returned by hook_search(). The result should be * - $result
* an array with keys "link", "title", "type", "user", "date", and "snippet". * - $type
* Optionally, "extra" can be an array of extra info to show along with the
* result.
* @param $type
* The type of item found, such as "user" or "node".
* *
* @ingroup themeable * @see search-result.tpl.php
*/ */
function theme_search_item($item, $type) { function template_preprocess_search_result(&$variables) {
$output = ' <dt class="title"><a href="'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></dt>'; $result = $variables['result'];
$variables['url'] = check_url($result['link']);
$variables['title'] = check_plain($result['title']);
$info = array(); $info = array();
if (!empty($item['type'])) { if (!empty($result['type'])) {
$info[] = $item['type']; $info['type'] = $result['type'];
} }
if (!empty($item['user'])) { if (!empty($result['user'])) {
$info[] = $item['user']; $info['user'] = $result['user'];
} }
if (!empty($item['date'])) { if (!empty($result['date'])) {
$info[] = format_date($item['date'], 'small'); $info['date'] = format_date($result['date'], 'small');
} }
if (isset($item['extra']) && is_array($item['extra'])) { if (isset($result['extra']) && is_array($result['extra'])) {
$info = array_merge($info, $item['extra']); $info = array_merge($info, $result['extra']);
} }
$output .= ' <dd>'. (!empty($item['snippet']) ? '<p>'. $item['snippet'] .'</p>' : '') .'<p class="search-info">'. implode(' - ', $info) .'</p></dd>'; // Check for existence. User search does not include snippets.
return $output; $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
// Provide separated and grouped meta information..
$variables['info_split'] = $info;
$variables['info'] = implode(' - ', $info);
// Provide alternate search result template.
$variables['template_files'][] = 'search-result-'. $variables['type'];
} }
/** /**
@ -133,4 +128,3 @@ function search_form_submit($form, &$form_state) {
$form_state['redirect'] = 'search/'. $type .'/'. $keys; $form_state['redirect'] = 'search/'. $type .'/'. $keys;
return; return;
} }