2

I've been told I should implement this hook thing, what does that mean?

closed as too broad by kiamlaluno Oct 6 '15 at 3:52

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

4

The details for how to implement a hook varies from one hook to another.

One of the easiest examples though, is hook_init(). Hook init is a generic way of saying "Do something on every un-cached page load". The _un_cached part is important, if you are logged out when testing this, probably nothing will happen.

In a module file named "my_module.module", one would implement hook_init like this

<?php
my_module_init() {
}

To make something actually happen, we can add a call to drupal_set_message().

<?php
my_module_init() {
  drupal_set_message('Hello Drupal!');
}

If you have Devel installed, a common way to do debugging could look something like this:

<?php
my_module_init() {
  dpm(menu_get_item());
}

For more details and examples, see What is the basic concept behind hooks?

  • Most common used hooks here seems to be ones from alter family, especially form_alter. Would be worth explaining it a bit. And put more stress on the my_module part of function names, please. I still see people confused by it, and leaving it be - then surprised it fails for them. – Mołot May 1 '14 at 8:47

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