1

I customized the display of my search results so I can show custom fields in the search results (other question).

I have a custom field ('Department'). I want to show the search results grouped and ordered by that field.

1 Answer 1

1

Found an answer... hook_preprocess_search_results is exactly what I need.

function mymodule_preprocess_search_results(&$variables) {
    $variables['search_results'] = '';
    $output_results = '';
    $output = '';

    // loop through results, group by department
    $departments = array();
    foreach ($variables['results'] as $result) {
            $departments[$result['node']->field_department[$result['node']->language][0]['value']][] = $result;
    }
    ksort($departments);

    foreach ($departments as $department => $departmentResults) {
        $value = "";
        foreach ($departmentResults as $key => $result) {
            $value .= theme('search_result', array('result' => $result, 'module' => $variables['module']));
        }

        $output_results .=  '<div id="tabs"><h3>' . $department . '</h3>';
        $output_results .=  $value;
        $output_results .=  '</div>';
    }

    $output .= '</ul>';
    $output .= $output_results;
    $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.