I recently came across this list of all the Drupal hooks arranged in order of the number of times they are implemented in contrib - both Drupal 6 and 7 (with a bit of consolidation). This is a great piece of statistics and I wanted to talk a bit about the top 10 most used hooks.
If you are a developer, you probably heard about all of them. However, you may also be surprised of this ordering and thought a bit differently as to which ones are most used. So let’s see what they are and what they do - from number 10 down.
hook_init() is apparently the 10th most used Drupal hook in contrib. It gets called at the beginning of a page request (but after the modules and themes are loaded into memory) and it’s typically used to set global parameters that may be needed in the request. One thing to note about it is that it is not run on cached pages - you’d use hook_boot() for that (which loads at the bootstrap stage).
Now this is interesting: hook_views_api() is not a Drupal core hook but one from the Views module - you can see why Views is getting into core in D8. It is used to register Views API information (including the necessary files for Views) usually for implementing hook_views_default_views(). The latter is only the 16th used hook in the list (?!).
hook_form_alter() is a very famous hook - to me it is a bit surprising that it’s only the 8th most used. Together with its sisters, hook_form_BASE_FORM_ID_alter() and hook_form_FORM_ID_alter(), these hooks are used to alter the display of given forms on the site. If you want to add other Form API fields to an existing form, this is what you’ll use.
hook_help() is used to provide documentation to a module right on the site where it gets implemented. I’m a bit surprised also about this one as I did not expect so many modules to provide user help like this.
Quite a rightful spot, perhaps thought more modules created tables for themselves, but this is it. hook_schema() is implemented in
.install files in order to define database schemas for the module implementing it. When installing the module, Drupal automatically creates these tables according to this schema.
A very popular one indeed - also in themes not just modules - hook_theme() is used to define and register various theme implementations needed by the module or theme.
hook_install() is another hook that gets implemented in a module’s
.install file and gets called when the module is enabled for the first time or after it has been uninstalled. Additionally, it is called with a full bootstrap which means that all functions are available for it. Very powerful and useful.
This is the third most used hook - impressive stuff, and for good reason. Drupal is very powerful when it comes to user management and the granularity with which you can administer permissions is impressive. Implementing hook_permission() allows a module to register its own custom permission that a user role must have in order to perform some actions. I wrote a more detailed post on how to use this hook.
Sitting on the other side of hook_install(), hook_uninstall() is used to remove anything the module needs to remove when it gets uninstalled - apart from its entry in the systems table and the database tables it defines using hook_schema() as they get removed automatically.
Pampararaaam. I called this one - dooh. What kind of module can there be that does not have to register any paths, menu items or callback functions? hook_menu() does just that (and more). It has a great API documentation page that you should check out if you need to use it in your own module. But note that in Drupal 8, this hook loses a big chunk of its functionality (since Symfony2 routing takes care of it) but remains integral in the process.
OK, so there you have them, the top 10 most used hooks in Drupal 6 and 7 contrib. Impressive list.
Danny founded WEBOMELETTE in 2012 as a passion project, mostly writing about Drupal problems he faced day to day, as well as about new technologies and things that he thought other developers would find useful. Now he now manages a team of developers and designers, delivering quality products that make businesses successful.