1

Is there a way to get the return value of a hook defined in a particular module for eg:

$views_theme_hook = module_invoke('views', 'theme');

By using module_invoke function, I got an array of themes declared in views module. Is this proper way of doing it?

But this function needs arguments for some other hooks for eg: hook_tokens.

How could I get the result for hook_tokens and other similar hooks requiring arguments?

  • There is only one way to do it. You should pass these arguments. – Eugene May 21 '15 at 14:00
  • How can I get the arguments for all the hooks? – user46121 May 21 '15 at 14:02
  • There is no universal way to do it. You should prepare data. For example - load node by nid and use it as data. – Eugene May 21 '15 at 14:04
  • 1
    @alliswell It's not likely you would need to, but PHP has reflection if you're sure that's what you need to do. It would not be an easy job to come up with a pattern that would identify all possible Drupal hook implementations, though. You'd probably need to rely on well-documented code and string matching. There's definitely a better way to solve whatever problem you need to get over – Clive May 21 '15 at 14:08
  • Yes you are right. At last it was a pure PHP answer for a drupal question ;) – user46121 May 21 '15 at 14:13
0

module_invoke() is a very simple function:

function module_invoke($module, $hook) {
  $args = func_get_args();
  // Remove $module and $hook from the arguments.
  unset($args[0], $args[1]);
  if (module_hook($module, $hook)) {
    return call_user_func_array($module . '_' . $hook, $args);
  }
}

As you can see, it passes the arguments straight through to the hook function by way of call_user_func_array().

So, you just need to pass the extra arguments with the call to module_invoke(). For example, to invoke views_block_view() with a specific block delta, you would use:

$block = module_invoke('views', 'block_view', 'some_block_delta');
| improve this answer | |
  • I think PHP reflection is the proper way to do it. I found a function drupal_help_arg() which builds arguments for invoking hook_help. I thought that there might be other functions to build argument like this.Any way I will proceed with reflection. Thank you :) – user46121 May 21 '15 at 14:12
  • @alliswell There is not need to use reflection; just look at the documentation for the hook you are using. – kiamlaluno May 21 '15 at 14:36
  • are you suggesting to build the arguments by looking into the documents of the hook? Actually I need the return value of certain hooks which doesnot alter things but create drupal elements for eg: hook_node_info, hook_entity_info. these hooks don't require arguments but hooks like hook_theme require arguments. – user46121 May 21 '15 at 15:22
  • I don't know how you'd do that programatically, I think you'll be looking in the files manually if that's what you need to know – Clive May 21 '15 at 15:30

Your Answer

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