1

I'd like to change the output of the search result page.

For example, I've got 3 different content types: foo, bar, etc. I'd like to group my search results by these content types, so that for every content type the first three or so results are shown followed by a link: "View more results for content-type foo" which will lead you to a page where all results of this specific content type are shown.

I've done some researching and couldn't find a module which does something like this. I guess i have to write my own module, but I don't know where to begin. Do you have any tips for me (whick hooks to implement, etc...)

2 Answers 2

1

If you only have 3 types and you don't plan on changing often, the easiest way to do that might be to create views of those three content types and use views_embed_view.

You could potentially override the search results in your theme using search-results.tpl.php. A more complex, and possibly better way would be to write a custom module that implements hook_menu_alter and redirects the default search callback to a custom view in your module.

I have done something like that before and it's not a trivial undertaking (take a look at the apachesolr_search module if you want a complete example).

Something like (untested)...

function mymodule_menu_alter(&$menu) {
  if (isset($menu['search/mymodulesearch/%menu_tail'])) {
    // redirect default search to this module's view
    $menu['search']['page callback'] = 'mymodule_search_view'; 
    // create a menu linkspecifically for this view
    $menu['search/mymodulesearch/%menu_tail']['page callback'] = 'mymodule_search_view';
    $menu['search/mymodulesearch/%menu_tail']['weight'] = -20;
  }
}

function mymodule_search_view() {
  $keys = search_get_keys();

  // do something with views_embed_view here
}
1

i think that correct code will be:

function custom_preprocess_search_results(&$variables) {
global $base_path;
drupal_add_library('system', 'ui.tabs');
drupal_add_js('jQuery(document).ready(function(){jQuery("#tabs").tabs();});', 'inline');
$variables['search_results'] = '';
// get a list of node types
$nodeTypes = node_type_get_types();
// loop through results, group by type
$resultTypes = array();
foreach ($variables['results'] as $result) {
    $resultTypes[$result['node']->type][] = $result;  
}

// create fieldsets for each type
    $output = '<div id="tabs">';
$i = 0;
$output .= '<ul>';

foreach ($resultTypes as $resultType => $resultTypeResults) {
    $i++;
    $value = "";
    // loop through entries
    foreach ($resultTypeResults as $result) {
        $value .= theme('search_result', array('result' => $result, 'module' => $variables['module']));
    }
    $output .=  '<li><a href="#tabs-'.$i.'">';
    $output .=  $nodeTypes[$resultType]->name.'</a></li>';

}
$output .= '</ul>';

$i = 0;
foreach ($resultTypes as $resultType => $resultTypeResults) {
    $i++;
    $value = "";
    // loop through entries
    foreach ($resultTypeResults as $result) {
        $value .= theme('search_result', array('result' => $result, 'module' => $variables['module']));
    }

    $output .=  '<div id="tabs-'.$i.'">';
    $output .=  $value;
    $output .=  '</div>';

}
    $output .= '</div>';

     $variables['search_results'] = $output;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.