I need to limit searches to nodes tagged with a specific taxonomy category, determined on the fly. I need that to be completely invisible to the user. Some experimentation hacking the core search module presents an easy solution in the menu callback search_view.

In an effort to not hack core, I've attempted to override this menu callback within my module, but I'm not having much success. Can anyone provide or point me to a fairly specific example of how to go about this?

Comments

ronnbot’s picture

There are multiple ways you can do this. Firstly, did you try altering the menu callback using hook_menu_alter (http://api.drupal.org/api/function/hook_menu_alter/6)?

Another way is to alter just the search form using hook_form_alter (http://api.drupal.org/api/function/hook_form_FORM_ID_alter/6) and have the taxonomy set as a hidden field.

Also, you could create a view with search and have the taxonomy as a filter, which requires the least amount of coding.

johnpitcairn’s picture

Thanks for your reply. I initially tried using hidden field elements via hook_form_alter, but that leaves me open to URL manipulation, since the term ID will be included in the URL for the results page.

Taxonomy as a view filter is tempting, but the taxonomy is dynamically selected based on various criteria, and needs to be provided to the view as a default argument. So I wind up with code to manage that anyway. I prefer to keep views self-contained (and don't want to store php code in the database).

Directly altering how core search functions seems the cleanest option to me. I'll look harder at altering the menu callback, which is what I couldn't get working for this specific menu callback in the first place.

johnpitcairn’s picture

For anyone else trying to do this - hook_menu_alter is the way to go if you want to manipulate the search before and after it gets done, but you want to override the page callback for 'search/node/%menu_tail', not the page callback for 'search':

/**
 * Implementation of hook_menu_alter().
 */
function mymodule_menu_alter(&$items) {
  $items['search/node/%menu_tail']['page callback'] = '_mymodule_search_view';
}

/**
 * Override for search_view().
 *
 * @see
 *   /modules/search/search_pages.inc
 */
function _mymodule_search_view($type = 'node') {
  // paste the content of search_view() here and modify
}
jerome.jaglale’s picture

Thank you, it works just fine.

Defite’s picture

don't work for me at all..pasted all in template.php, flushed theme register and cache..

johnpitcairn’s picture

You don't put this in template.php, you need to create a custom module and put the code in that.

pierrot’s picture

worked fine!