4

I have a confusion regarding the hook and a normal function, So can any one tell me what is the significant difference between a Drupal hook and a normal PHP function in Drupal.

0

1 Answer 1

2

This is a good question. To clarify it first, Hook is a function. But it is a function that is named in such a way, that other modules and themes can override it with their own versions.

For example:

  1. Your module 'custom_node' has a function that adds information to the custom node. That function is called 'custom_node_add_data()'.
  2. Soon after you release the module, you realize, that other people liked it, and they are asking you to implement a way for them to add their custom data to a custom node, from their custom modules, so that they dont have to hack yours.
  3. So what do you do? You make your 'custom_node_add_data()' a hook. You name it 'hook_add_data()', and then you add your own overrides in 'custom_node_add_data()'.
  4. Under the hood, Drupal finds the declared hooks, and searched for functions in all code base that serve the hook formula, where the 'hook' is replaced by the module name.

Thus, profit! Custom modules can now override your hook function with their own code. They 'hook in' to what your module does, because now their hook function will be called.

Hooks allow to replace initial module's functionality with your own functionality in procedural code. In OOP code, like in Drupal 8, hooks are being replaced by subclassing and implementation of interfaces. Some hooks fill still remain in theming.

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