uc_out_of_stock module seems to be invoking hook_init a second time with different path arguments when I am on a product page. I have a drupal_set_message in a custom implementation of hook_init, and the first time hook_init is called my logic for checking the current path works fine, but then it's called again with uc_out_of_stock/query as the path arguments and my logic gets screwed up because it thinks I'm on a different page now.

I'm not totally sure this is a valid bug, is the problem that hook_init is being called a second time, or is the problem that my call for drupal_set_message is in the wrong place?

- uc_out_of_stock 6.x-1.x-dev
- Drupal 6.22

Thanks,
Eric

Comments

hanoii’s picture

Status: Active » Fixed

This module does not invoke hook_init by itself. The only reason why you experience this is because this module makes an ajax call to uc_out_of_stock/query, which is, on itself, a different page, so you get another hook_init() call how drupal works. You should probably not add messages on hook init or if you have to do so, just use the argument to decide whether not to do so on certain paths. But this is not something I am doing, is how drupal works and because of the ajax call.

ericbroder’s picture

Category: bug » support

Wow, thanks for the super quick response hanoii!

You should probably not add messages on hook init...

Any suggestions for where this should go instead? I'm trying to do something like, "show this message unless user is on path X".

hanoii’s picture

Well, not really sure, but even if you want to do it there, you can.. just do show this message unless ser is on path x or user is on uc_out_of_stock/query and that should do it for you.

ericbroder’s picture

Thanks for the advice hanoii. I just found another case where hook_init is also being called a second time, by imagecache module, so I think it's not really practical to put a comprehensive list of path exceptions for the message.

I saw that update module is actually using hook_help for error messages, so I tried that instead. And there's an interesting comment in the docs:

$arg An array that corresponds to the return value of the arg() function, for modules that want to provide help that is specific to certain values of wildcards in $path. For example, you could provide help for the path 'user/1' by looking for the path 'user/%' and $arg[1] == '1'. This array should always be used rather than directly invoking arg(), because your hook implementation may be called for other purposes besides building the current page's help.

http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hoo...

And apparently ctools module is making sure that hook_help is only called once:

Theme override to use CTools to call help instead of the basic call.

This is used only to try and improve performance; this calls through to the same functions that tabs do and executes a complex build. By overriding this to use the CTools version, we can prevent this query from being run twice on the same page.

http://drupalcontrib.org/api/drupal/contributions--ctools--includes--men...

I'm not exactly a hook_help expert, but so far so good on this method. Thanks for your help!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.