Create a module that adds autocomplete functionality to the search module. This is to be done by fetching the statistics module's "top search phrases" and plugging it into the search form through use of hook_form_alter on the textfield, adding the '#autocomplete_path' property. It should depend on both the search and statistics modules. It should be written for Drupal 6.x.

Comments

solipsist’s picture

Possible source of ideas: http://drupal.org/project/livesearch

birdmanx35’s picture

Title: Create a module that adds autocomplete functionality to the search module. » DROP Task: Create a module that adds autocomplete functionality to the search module.

Being obnoxious.

dawehner’s picture

StatusFileSize
new907 bytes

my current state:
it works

another module name
autocomplete_search ??

update:
- only search string strlen > 3
- code style

cwgordon7’s picture

Apart from coding standards issues, this is pretty much there.

Several major issues:

<?php
strlen($last_string)  >= 3
?>

Search.module already refuses to index words below a certain user-configurable string length. This is unnecessary.

The hook_form_alter implementation is screaming out for a switch ($form_id) { statement.

drupal_explode_tags() is used... but I cannot determine the purpose, as we do not enter a comma-separated list into the search box. A simple explode(' ', $string) should work fine.

This does not use the statistic module's tracking of top search phrases; instead it uses the top words collected by the search module. I appreciate the value in this—perhaps it would be best to combine the two by letting the user decide the behavior through the admin page?

Now, the picky coding standards stuff...

1. I agree that autocomplete_search would be a better name.
2. The .info file, line 0: there should be a ; $Id$ CVS tag.
3. The .info file, line 1: name = ajax_search is a problem. This is going to be presented to the user— we want something more like "Ajax search".
4. The .info file, line 2: description = adds ajax to search: again, this is presented to the user, so at the very least, we need to capitalize the "a" in "adds".
5. The .module file, line 2: $ID$ should be $Id$. I don't know if it matters, but I think that's the standard way to do it.
6. The .module file, line 8: No trailing white space! That should be a blank line, no spaces. I'd even argue against the whole line... I don't know that it's necessary, but your judgment on this one.
7. The .module file, line 13.5: This should really be in it's own modulename.pages.inc. Add the key

<?php
    'file' => 'modulename.pages.inc',
?>

to the menu item, and move the callback to the new file.
8. The .module file, line 15: No trailing white space! That should be a blank line, no spaces.
9. The .module file, line 17.5: There should be a blank line here!
10. The .module file, line 19: @$string should be @param $string with the description on the next line, indented two extra spaces.
11. The .module file, line 22: This looks like its copied-and-pasted from the taxonomy module or something. Either replace it with a more relevant comment or remove it.
12. The .module file, line 25: Same thing, the comment isn't really relevant.
13. The .module file, line 29: if ($string != '' ) { should be if ($string != '') {. The extra space there is against coding standards!
14. The .module file, line 30: As previously discussed, is unnecessary and counterproductive and should be removed; however, if it were to be there, if (strlen($last_string) >= 3) { should be if (strlen($last_string) >= 3) {. The extra space there is against coding standards!
15. The .module file, lines 31-33: Should not be there.
16. The .module file, line 36: Should not be there either :) .
17. The .module file, line 37: $string. $item->word should be $string . $item->word. Only leave out the space in concatenation next to string literals ("string" or 'string').
18. The .module file, lines 39-40: Should not be there, maybe keep one of them without the trailing white space.
19. The .module file, line 44.5: There should be a blank line here! :)
20. The .module file, line 49: No trailing white space! :)

dawehner’s picture

arg there is a coder module you says this all!
sry
i have a question: how can i use the search with the statistics: do i need the function search_data()

dawehner’s picture

StatusFileSize
new1.66 KB

so the statistics work
i only two coder-errors: "// $Id$" seems to be wrong and the line 34

cwgordon7’s picture

Status: Active » Needs work

Ok, it's definitely getting there! A few issues:

<?php
    case 'search_block_form':
      $form[$form_id]['#autocomplete_path'] = 'search/autocomplete';
      break;

    case 'search_theme_form':
      $form[$form_id]['#autocomplete_path'] = 'search/autocomplete';
      break;
?>

would be much easier as

<?php
    case 'search_block_form':
    case 'search_theme_form':
      $form[$form_id]['#autocomplete_path'] = 'search/autocomplete';
      break;
?>

Why does it depend on the dblog module now?

$result2 and the admin settings never appear to be used— why?

dawehner’s picture

StatusFileSize
new1.64 KB

OK.
I wanted to use the "top search page", which is included in dblog. But i missed to remove it from the info fail.
Cool tip, i didn't knew that you can use switch/case like that!
The result2 was a try to do both querys together, but i didn't managed to do it.
You know that there is still a search autocomplete module ?: http://drupal.org/project/search_autocomplete
What for configuration options should i include into the module?

PS:
A "Bug": The watchdog function at the end is called, but nothing happend to the sort until a page reload. Is this a Bug from mysite?

PS2:
I connected the admin settings with the search

dawehner’s picture

StatusFileSize
new2.1 KB

added some options of the search autocomplete module, connected with statistics
access etc.