I want to implement a node type specific search and want to format the search results different from the default. While there is a way to override the individual search results through theming or hook_search_item it is not possible to override the general formatting of search results.

For example, there is a hardcoded dl element around all search results.

In my specific case I want to return results as a table, similar to the search results presented by project.module.

Also, I would like to have a sortable table, so I would need to pass parameters to the pager query in do_search.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

killes@www.drop.org’s picture

Status: Active » Needs review

Ok, here is a patch that introducs hook_search_page analogous to hook_search_item and also adds another parameter to do_search.

We could debate whether we should always sort by score and just first order by additional parameters.

Tobias Maier’s picture

patch?

killes@www.drop.org’s picture

FileSize
3.36 KB

patch

Tobias Maier’s picture

i have a question regarding $result as argument for a theme_-function.

are we doing this often in core to hand over a $result from a sql_query?
I thought this is false so I decided to go another way in one of my patches:

+  $result = db_query('SELECT .....', $arguments);
+  while ($record = db_fetch_object($result)) {
+    $fields[] = $record;
+  }

I handed over this array of objects...

btw.
here is a list of other patches which try to replace hardcoded html with flexible theme_-functions:
refactor profile_browse()
aggregator: replace hardcoded html with theme-functions

second question is if we did this $sort_parameters = 'ORDER BY score DESC') anywhere else in core?

beside this patch looks good, no big change in code but very useful

killes@www.drop.org’s picture

$results in this case is an array, not a sql resource.

I don't understand your second question. It is just a default argument for a function parameter and we do that all the time.

killes@www.drop.org’s picture

FileSize
4.79 KB

Here is a new patch. I have changed do_search to also return the score of the results.

Steven’s picture

I'm not sure about the ordered clause... you can already change the SELECT clause for 'score' to alter the sorting order as you like (it's what node.module does). With this patch, the score that is returned in $item->score will not match the actual sorting order.

killes@www.drop.org’s picture

Hmm, how do you suggest I change the select clause to change the ordering? There is a hardcoded ORDER BY score DESC in the query. Do you suggest I change the score algorithm for each sorting order I might want to have? That sounds terribly complicated.

score will match the sorting order if sorted by score. If you should for example sort by date (within the returned results) then it obvioulsy won't.

Steven’s picture

FileSize
6.56 KB

Well I suppose an explicit ORDER BY is handier if you want a separate score measure.

I moved the hook_search_page() invokation out of theme_search_page(), it doesn't belong there. It also doesn't make sense to have hook_search_item() when there is hook_search_page(): it is up to the module to decide to invoke theme('search_item') or theme('mymodule_search_item').

New patch attached.

Steven’s picture

Status: Needs review » Fixed

Committed to HEAD after ok from killes.

Anonymous’s picture

Status: Fixed » Closed (fixed)