Displaying recent searches -- is there a snippet?

dduane - September 8, 2007 - 13:15

I'd love to tuck in a block somewhere showing my users' most recent searches... say, the last five searches or so. Any thoughts on how this might be done? Is there perhaps a snippet already online somewhere?

Thanks!

Uhm, I think it's quite too

zsanmartin - September 8, 2007 - 14:12

Uhm, I think it's quite too complex for a snippet. I think it's better to write a simple module.

It's not too hard. You can do a nice job with few lines. Perhaps you could contribute it to Drupal, later.

First, look for a similar module here: http://drupal.org/project/Modules
I found this: http://drupal.org/project/search_keywords - but I don't think it is what you want.

Haven't you found nothing? Ok, let's start a custom module.

First of all, read the Module developer's guide: http://drupal.org/node/83055

Now, open api.drupal.org and start looking for hooks you can implement in your module. (Drupal works with hooks. When doing most of the things, such as saving or loading a node, performing a search or submitting a form, Drupal calls "hooks". Implementing a hook means writing a function in your module that will be called when that specific action is being performed)

I've found this: http://api.drupal.org/api/function/hook_search/5

Now, all what you have to do is write this down:

<?php
function yourmodulesname_search($op, $keys = null) {
  if (
$op == "search") {
   
//now, you get the keys from the variable $keys
    //and store them wherever you like.
    //it can be a custom table
    //or you can just do a variable_set()
 
}
}
?>

and then, create a function named yourmodulesname_show_keys which returns an array of last searched keys, and implements the hook yourmodulesname_block to create a block to show your keywords.

Hope it works for you.

José San Martin
http://www.verinco.com/

Zeitgeist

Richard Eriksson - February 15, 2008 - 03:02

http://drupal.org/project/zeitgeist might be what you're looking for.

subscribe

mrgoltra - February 19, 2008 - 20:38

adding

 
 

Drupal is a registered trademark of Dries Buytaert.