From eeeba75a5b10420688f08093738dd8e28060d69e Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Wed, 18 Aug 2010 18:40:50 +0000 Subject: - Patch #839524 by jhodgdon, pwolanin: search results are themed too early. --- modules/search/search-result.tpl.php | 3 ++- modules/search/search-results.tpl.php | 5 ++-- modules/search/search.api.php | 23 ++++++++++------- modules/search/search.module | 14 +++++++---- modules/search/search.pages.inc | 22 +++++++++------- modules/search/search.test | 36 ++++++++++++++++++++++++++- modules/search/tests/search_extra_type.module | 23 +++++++++++++++++ 7 files changed, 99 insertions(+), 27 deletions(-) (limited to 'modules/search') diff --git a/modules/search/search-result.tpl.php b/modules/search/search-result.tpl.php index e05aeb046..625133fb7 100644 --- a/modules/search/search-result.tpl.php +++ b/modules/search/search-result.tpl.php @@ -16,7 +16,8 @@ * - $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". + * - $module: The machine-readable name of the module (tab) being searched, such + * as "node" or "user". * * Default keys within $info_split: * - $info_split['type']: Node type. diff --git a/modules/search/search-results.tpl.php b/modules/search/search-results.tpl.php index 8d2d9527c..ec0f394f5 100644 --- a/modules/search/search-results.tpl.php +++ b/modules/search/search-results.tpl.php @@ -15,7 +15,8 @@ * 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". + * - $module: The machine-readable name of the module (tab) being searched, such + * as "node" or "user". * * * @see template_preprocess_search_results() @@ -23,7 +24,7 @@ ?>

-
    +
    diff --git a/modules/search/search.api.php b/modules/search/search.api.php index da3b7b839..25b446ebb 100644 --- a/modules/search/search.api.php +++ b/modules/search/search.api.php @@ -25,9 +25,11 @@ * hook_update_index(). If your search type has settings, you can implement * hook_search_admin() to add them to the search settings page. You can also * alter the display of your module's search results by implementing - * hook_search_page(). And you can use hook_form_FORM_ID_alter(), with - * FORM_ID set to 'search', to add fields to the search form. See - * node_form_search_form_alter() for an example. + * hook_search_page(). You can use hook_form_FORM_ID_alter(), with + * FORM_ID set to 'search', to add fields to the search form (see + * node_form_search_form_alter() for an example). You can use + * hook_search_access() to limit access to searching, and hook_search_page() to + * override how search results are displayed. * * @return * Array with optional keys: @@ -248,7 +250,7 @@ function hook_search_execute($keys = NULL, $conditions = NULL) { /** * Override the rendering of search results. * - * A module that implements hook_search() to define a type of search + * A module that implements hook_search_info() to define a type of search * may implement this hook in order to override the default theming of * its search results, which is otherwise themed using theme('search_results'). * @@ -262,17 +264,20 @@ function hook_search_execute($keys = NULL, $conditions = NULL) { * An array of search results. * * @return - * An HTML string containing the formatted search results, with + * A renderable array, which will render the formatted search results with * a pager included. */ function hook_search_page($results) { - $output = '
      '; + $output['prefix']['#markup'] = '
        '; foreach ($results as $entry) { - $output .= theme('search_result', $entry, $type); + $output[] = array( + '#theme' => 'search_result', + '#result' => $entry, + '#module' => 'my_module_name', + ); } - $output .= '
      '; - $output .= theme('pager', NULL); + $output['suffix']['#markup'] = '
    ' . theme('pager'); return $output; } diff --git a/modules/search/search.module b/modules/search/search.module index 7f0402c42..b5a135c2b 100644 --- a/modules/search/search.module +++ b/modules/search/search.module @@ -114,12 +114,12 @@ function search_theme() { 'template' => 'search-block-form', ), 'search_result' => array( - 'variables' => array('result' => NULL, 'type' => NULL), + 'variables' => array('result' => NULL, 'module' => NULL), 'file' => 'search.pages.inc', 'template' => 'search-result', ), 'search_results' => array( - 'variables' => array('results' => NULL, 'type' => NULL), + 'variables' => array('results' => NULL, 'module' => NULL), 'file' => 'search.pages.inc', 'template' => 'search-results', ), @@ -1074,8 +1074,8 @@ function template_preprocess_search_block_form(&$variables) { * Optional array of additional search conditions. * * @return - * Formatted search results. No return value if $keys are not supplied or - * if the given search module is not active. + * Renderable array of search results. No return value if $keys are not + * supplied or if the given search module is not active. */ function search_data($keys, $module, $conditions = NULL) { if (module_hook($module, 'search_execute')) { @@ -1084,7 +1084,11 @@ function search_data($keys, $module, $conditions = NULL) { return module_invoke($module, 'search_page', $results); } else { - return theme('search_results', array('results' => $results, 'type' => $module)); + return array( + '#theme' => 'search_results', + '#results' => $results, + '#module' => $module, + ); } } } diff --git a/modules/search/search.pages.inc b/modules/search/search.pages.inc index a5eca2d21..607afa97f 100644 --- a/modules/search/search.pages.inc +++ b/modules/search/search.pages.inc @@ -44,7 +44,8 @@ function search_view($module = NULL, $keys = '') { drupal_goto($path); } - $results = ''; + // Default results output is an empty string. + $results = array('#markup' => ''); // Process the search form. Note that if there is $_POST data, // search_form_submit() will cause a redirect to search/[module path]/[keys], // which will get us back to this page callback. In other words, the search @@ -67,7 +68,7 @@ function search_view($module = NULL, $keys = '') { } // The form may be altered based on whether the search was run. $build['search_form'] = drupal_get_form('search_form', NULL, $keys, $info['module']); - $build['search_results'] = array('#markup' => $results); + $build['search_results'] = $results; return $build; } @@ -77,17 +78,20 @@ function search_view($module = NULL, $keys = '') { * * The $variables array contains the following arguments: * - $results - * - $type + * - $module * * @see search-results.tpl.php */ function template_preprocess_search_results(&$variables) { $variables['search_results'] = ''; + if (!empty($variables['module'])) { + $variables['module'] = check_plain($variables['module']); + } foreach ($variables['results'] as $result) { - $variables['search_results'] .= theme('search_result', array('result' => $result, 'type' => $variables['type'])); + $variables['search_results'] .= theme('search_result', array('result' => $result, 'module' => $variables['module'])); } $variables['pager'] = theme('pager', array('tags' => NULL)); - $variables['theme_hook_suggestions'][] = 'search_results__' . $variables['type']; + $variables['theme_hook_suggestions'][] = 'search_results__' . $variables['module']; } /** @@ -95,7 +99,7 @@ function template_preprocess_search_results(&$variables) { * * The $variables array contains the following arguments: * - $result - * - $type + * - $module * * @see search-result.tpl.php */ @@ -105,8 +109,8 @@ function template_preprocess_search_result(&$variables) { $variables['title'] = check_plain($result['title']); $info = array(); - if (!empty($result['type'])) { - $info['type'] = check_plain($result['type']); + if (!empty($result['module'])) { + $info['module'] = check_plain($result['module']); } if (!empty($result['user'])) { $info['user'] = $result['user']; @@ -122,7 +126,7 @@ function template_preprocess_search_result(&$variables) { // Provide separated and grouped meta information.. $variables['info_split'] = $info; $variables['info'] = implode(' - ', $info); - $variables['theme_hook_suggestions'][] = 'search_result__' . $variables['type']; + $variables['theme_hook_suggestions'][] = 'search_result__' . $variables['module']; } /** diff --git a/modules/search/search.test b/modules/search/search.test index df76e4a23..b4c38312c 100644 --- a/modules/search/search.test +++ b/modules/search/search.test @@ -1050,7 +1050,7 @@ class SearchSimplifyTestCase extends DrupalWebTestCase { /** - * Test config page. + * Tests keywords and conditions. */ class SearchKeywordsConditions extends DrupalWebTestCase { @@ -1523,3 +1523,37 @@ class SearchEmbedForm extends DrupalWebTestCase { $this->submit_count = $count; } } + +/** + * Tests that hook_search_page runs. + */ +class SearchPageOverride extends DrupalWebTestCase { + public $search_user; + + public static function getInfo() { + return array( + 'name' => 'Search page override', + 'description' => 'Verify that hook_search_page can override search page display.', + 'group' => 'Search', + ); + } + + function setUp() { + parent::setUp('search', 'search_extra_type'); + + // Login as a user that can create and search content. + $this->search_user = $this->drupalCreateUser(array('search content', 'administer search')); + $this->drupalLogin($this->search_user); + + // Enable the extra type module for searching. + variable_set('search_active_modules', array('node' => 'node', 'user' => 'user', 'search_extra_type' => 'search_extra_type')); + menu_rebuild(); + } + + function testSearchPageHook() { + $keys = 'bike shed ' . $this->randomName(); + $this->drupalGet("search/dummy_path/{$keys}"); + $this->assertText('Dummy search snippet', 'Dummy search snippet is shown'); + $this->assertText('Test page text is here', 'Page override is working'); + } +} diff --git a/modules/search/tests/search_extra_type.module b/modules/search/tests/search_extra_type.module index 1d22c46c8..8dcab4f6b 100644 --- a/modules/search/tests/search_extra_type.module +++ b/modules/search/tests/search_extra_type.module @@ -17,6 +17,9 @@ function search_extra_type_search_info() { ); } +/** + * Test conditions callback for hook_search_info(). + */ function search_extra_type_conditions() { $conditions = array(); @@ -45,3 +48,23 @@ function search_extra_type_search_execute($keys = NULL, $conditions = NULL) { ), ); } + +/** + * Implements hook_search_page(). + * + * Adds some text to the search page so we can verify that it runs. + */ +function search_extra_type_search_page($results) { + $output['prefix']['#markup'] = '

    Test page text is here

      '; + + foreach ($results as $entry) { + $output[] = array( + '#theme' => 'search_result', + '#result' => $entry, + '#module' => 'search_extra_type', + ); + } + $output['suffix']['#markup'] = '
    ' . theme('pager'); + + return $output; +} -- cgit v1.2.3