1

I'm having a bit of trouble with troubleshooting a module I am working. I am trying to modify a module to redirect traffic but even though its enabled it doesn't seem to do anything. I've had problems in the past with using hooks and i'm not sure what I'm doing wrong.

I am basically using the finder module and here's the code to redirect:

function test_finder_goto_alter(&$result, $finder) {
  /* Redirect to a different page in the current directory that was requested */
    drupal_goto("testpage");
}

I see the module in modules and enabled it but it still doesn't redirect the way I want it to. Do you have any suggestion on what I'm doing wrong? I don't think its specific to this module I think I might be doing something wrong in general.

1

Implementations of hook_*_alter() are generally supposed the first parameter passed as reference, not to execute drupal_goto(), or any other functions that redirect the user.

Looking at the code of the Finder module, I notice that the following code is one that handles the hook you implemented.

$base_module = &$finder->base_handler['#module'];

if ((isset($finder->go) && $finder->go && count($result)) || ($goto == 'best' && count($result) === 1)) {
  drupal_alter('finder_goto', $result, $finder);
  $current_result = current($result);
  module_invoke($base_module, 'finder_goto', $finder, $current_result);
} 

Finder calls that hook with drupal_alter(), which is the function used to invoke such hooks, and it passes the altered argument to the implementation of hook_finder_goto() implemented in the $base_module module.

| improve this answer | |
  • Thanks so much Kiamlaluno. I tried to work though what you did to understand and although my php is weak, I now understand the code. My problem is it still doesn't work. I don't really want the specific answer but was wondering how do you troubleshoot modules? My problem is I am getting no feedback from the module, with java or unix you get errors or can debug, with drupal i'm not sure how to troubleshoot a module. Is there a package to help code modules? or a way to run it in an monitored environment(like how in netbeans you can debug code) – Lostsoul May 24 '11 at 19:51
  • @LostSoul You could install Devel, which has some functions that can be used to output as Drupal messages the content of a variable. If you use dsm($finder) or dsm($result), you can find the content of those variables. – kiamlaluno May 24 '11 at 19:56
  • I had that installed already. I kept the goto function still it in and forgot to end with ";" and I got a white screen of death. Thats good because as least I know its aware of my module. I added the ; and commented out the goto, but I get no output. Where would I find output of the dsm command? – Lostsoul May 24 '11 at 20:02
  • when I used dsm to troubleshoot views in the past, it showed up in the preview, but this time there's no preview area. – Lostsoul May 24 '11 at 20:03
  • @Lostsoul It should be showed the next time a page is rendered by Drupal. In the default theme I use, those messages are shown on the top of the page. – kiamlaluno May 24 '11 at 20:06
1

Hooks 101

hooks should be created in a module. The module which they are called from should be enabled.

So in the first instance you will need to create a .info and a .module file with the correct information in.

Then to implement a hook you must use the module name.

So the example code above would work if the php code is in a module called test which was enabled.

So hook_menu() in a module called test.module would be a function called test_menu()

Secondly hooks will only be called when invoked, some get invoked rarely when for example building the menu system, and others are invoked when specific actions happen. You will need to read up on the documentation for the hook in question to find out more.

The same goes for parameters and return values, each hook is different. So the documentation is important there.

So a general checklist for hooks.

  • Is the hook in a module?

  • Is the module enabled?

  • Is the hook named the same as the module?

  • Is the action which is calling the hook being called?

  • Have I cleared the cache?

  • Am I using the hook in the correct way?

| improve this answer | |
  • Awesome thank you as always Jeremy. I just printed this and put it on my wall until I get better at writing modules. – Lostsoul May 24 '11 at 19:47
0

You should clear Drupal cache after you implementing hook.

Go to the admin-> Performance -> Clear cache

| improve this answer | |
  • i tried that, but no luck. While I develop, I actually disable the cache. – Lostsoul May 24 '11 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.