I've been told I should implement this hook thing, what does that mean?
1 Answer
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, especiallyform_alter
. Would be worth explaining it a bit. And put more stress on themy_module
part of function names, please. I still see people confused by it, and leaving it be - then surprised it fails for them.– MołotMay 1, 2014 at 8:47