I have a module that has a hook_search_item and a hook_view. When I do a search, the hook_view is being called, but hook_search_item is not. Why?

I'm also seeing messages coming out of various php-code pages, so they are being executed as part of the search. Why?

When I search as an anonymous user, I get error messages that I think are from CCK:
# warning: Invalid argument supplied for foreach() in C:\www\drupal\sites\default\modules\cck\fieldgroup.module on line 395.
# warning: Invalid argument supplied for foreach() in C:\www\drupal\sites\default\modules\cck\fieldgroup.module on line 395.
# warning: Invalid argument supplied for foreach() in C:\www\drupal\modules\taxonomy\taxonomy.module on line 1328.

Search shouldn't be this weird.

Comments

mooffie’s picture

I had a cursory look at search.module. It seems that it's a documentation error: search_item is a theming function, not a hook. I.e., you should override theme_search_item instead of implementing hook_search_item. (that is, implement phptemplate_search_item, not mymodule_search_item).

When I do a search, the hook_view is being called [...]
[...] various php-code pages, so they are being executed as part of the search.

The nodes are rendered (a process similar to what happens in node_view), because we want to show the user some excerpt. At this stage the search has finished already.

I don't know what causes the warning messages. Perhaps you could follow the steps outlined in the Quick-and-Dirty Debugging article.

# warning: Invalid argument supplied for foreach() in C:\www\drupal\sites\default\modules\cck\fieldgroup.module on line 395.
# warning: Invalid argument supplied for foreach() in C:\www\drupal\sites\default\modules\cck\fieldgroup.module on line 395.
# warning: Invalid argument supplied for foreach() in C:\www\drupal\modules\taxonomy\taxonomy.module on line 1328.

I looked around these lines. It is code that gets executed in the rendering process. It seems that your nodes somehow are not fully loaded.

(When you're reporting line numbers please paste the lines themselves, or mention the revision number in the $Id line --these files are evolving constantly and the version you have is different than the version your reader has.)

NancyDru’s picture

I see this code in the jobsearch module. Perhaps it works despite itself.

function job_search_item($item) {
  return theme('job_search_item', $item);
}

function theme_job_search_item($item) {
  $output .= '<div id="job_search">';
  $output .= l($item['title'], $item['link']) . '<br />';
  $output .= $item['snippet']. '<br />';
  $output .= $item['user'] . ' | ' . format_date($item['date'], 'small'). '<br /><br />';
  $output .= '</div>';

  return $output ;
}

The Taxonomy module is 5.2. CCK is "// $Id: content.module,v 1.90.2.39 2007/03/11 23:39:49 yched Exp $".

EDIT: I basically duplicated the above structure, but with the code copied from the search module. Neither function is being called.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

mooffie’s picture

I see this code in the jobsearch module. Perhaps it works despite itself.
function job_search_item($item) {

That's most interesting. I've downloaded this module and will have a look at this later today.

mooffie’s picture

OK, I investigated this hook_search_item a bit and here's my findings:

In the past there was hook_search_item. It was removed in February 2006. The dicsussion here explains what happened:

1. killes added a new hook, hook_search_page, to make it possible to alter the whole listing.
2. steven thinks that hook_search_item isn't necessary anymore and removes it.

I think it's good for hook_search_item not to exist, because ppl misundestand it: 'hook' didn't stand for the node type. It stood for either 'node' or 'user'. So implementing job_search_item was wrong anyway.

So the current documentation is wrong (and should be fixed; feel free to do that). There isn't hook_search_item. However, there is theme_search_item.

NancyDru’s picture

I did see hook_search_page but didn't think it's what I wanted. Theme_search_item takes over theming ALL search items, and I really don't want to mess with anyone else's content...

I just want to filter each one of my selected items before it gets presented to the user. So maybe there is a need for a proper hook_search_item.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

mooffie’s picture

Theme_search_item takes over theming ALL search items [...]
So maybe there is a need for a proper hook_search_item.

I wholeheartedly agree.

(You could implement phptemplate_search_item and call theme_search_item from it. But, of course, this isn't a good solution in general, because only one module can define a function with that name...)

Notes:

1. The search module itself knows nothing about nodes, and since the Drupal people are purists, I don't think they would agree to add a proper hook_search_item to it. But some other solutions are possible.

2. Drupal 6.0 adds a member to the node, $node->build_mode, before it sends it to rendering. In your hook_view (or hook_nodeapi('view')) you can examine this member to find out if you've been called to provided a rendering for use on the search page. But your control on the whole item output would still be limited.

NancyDru’s picture

Theme solutions aren't all that great for a contributed module since you never know which theme a user will select.

I've heard mention of the build_mode, but with precious little information on how to use it. And that doesn't help me with supporting users on 5.x.

I've temporarily bypassed the problem by adding a user access check in the hook_view code. Maybe that will have to be my final solution.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database