2

I need to check whether a hook is implemented in a module, so I chose module_hook function. In the documentation it states that the Return Value will be:

TRUE if the module is both installed and enabled, and the hook is implemented in that module.

Now, I named a callback function (not a hook) in my module as "my_module_abc()"

Note: my_module is the name of my module.

If I test this function in module_hook like below:

module_hook('my_module', 'abc');

It returns TRUE.

  1. I thought It would return TRUE only for registered hooks and not for any function callbacks. Does module_hook know the difference between a function and a hook?

  2. Is there any function like hook_information($hook) which returns information about a hook?

Is this right? Am I missing something?

2 Answers 2

3

It's important to understand that a 'hook' is literally a function that's named in a certain way.

If you have a function

function my_module_abc() { }

That is, by the nature of the hook system, my_module's implementation of hook_abc().

Whether hook_abc() was ever intended to be a hook is a completely different matter. The fact that the hook system works entirely on a naming convention means that it's inevitable. In fact it's working exactly as designed.

Hooks are very loose, they are not structured; they don't have meta data, beyond what the module author may choose to document in MODULE.api.php. The 'information' you're looking for doesn't exist, and until someone changes the way Drupal's hook system works, it can't and won't.

4
  • So its time for me to stop the search for such a function. But there are a lot of contrib modules which uses the module name as the beginning of a function name. May be the standard to name so should be strictly stated in the beginning. :( Anyway thanks clive :) Commented Apr 8, 2015 at 9:29
  • 1
    @PravinAjaaz I don't disagree with you in principle, but let's face it: PHP is a pretty crappy, loosely typed, language (which used to be even worse than it is now in versions that Drupal 7 needs to support for historical reasons). You can't really blame people for building crappy, loosely typed systems on top of it ;) I should disclaim that I say that in jest, I'm not disparaging those that implemented it!
    – Clive
    Commented Apr 8, 2015 at 9:32
  • I finally gained a valuable information that "Drupal (and also I) doesn't actually know the difference between a function and a hook." ;) Thanks Clive.. ;) It's time for me to find other possible ways of doing it. Maybe I should stick with your earlier suggestion of reading the "Comment Blocks" :) I thought it was not proper but now I am sure that's the only way of doing it. Commented Apr 8, 2015 at 9:40
  • 1
    Oh it's totally proper to reflect on comment blocks - Drupal 8, and pretty much all modern frameworks, use the technique extensively for annotation-based meta data. It's a lot easier than you might think, too. e.g. $reflection = new ReflectionFunction('block_help'); $comment = $reflection->getDocComment(). $comment will contain "Implements hook_help().", which you could string match against. It should be foolproof for core, where documentation should be perfectly in keeping with standards. It's just contrib you have to worry about
    – Clive
    Commented Apr 8, 2015 at 9:50
2

module_hook() only uses function_exists() to check whether the named function is in the module or not. It doesn't check to see if that function is implementing a hook or not.

Looks like the intended purpose is to check whether a known hook is implemented in a module. If you use it for its purpose, it works fine.

If you use it for a purpose other than intended, results may vary. Granted they probably should call it module_function() instead.

1
  • I missed that function_exists() is called in module_hook() . Thanks Darvanen. :) Commented Apr 8, 2015 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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