Need some help with your project? Contact me

Top 10 used hooks in Drupal 6 and 7 contrib modules

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.

10. hook_init()

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).

9. hook_views_api()

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 (?!).

8. hook_form_alter()

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.

7. hook_help()

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.

6. hook_schema()

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.

5. hook_theme()

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.

4. hook_install()

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.

3. hook_permission()

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.

2. hook_uninstall()

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.

1. hook_menu()

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.

Comments

I'm not surprised hook_form_alter is this low, because it is best suited for completely custom functionality. If this were to somehow contain all custom drupal sites, I wouldn't be surprised if it was the most used hook.

Also the number of modules using hook_init() is sad. Adding CSS/JS is the main reason, and hook_page_build() is much more appropriate.

Very Useful.... Well arranged.... Thanks A Lot....

Its Very Well Explained, How to Hooks are working in Drupal System.....

Not surprise with hook_menu, hook_form_alter, hook_permission. These are my most extensive hook I always use, specially when working with custom modules.

needs to be changed to: https://gist.github.com/webchick/4409685

Otherwise, very nice , thanks!

Thanks!! Changed.

D

1- hook_validate
2- hook_form

Add new comment

You must have Javascript enabled to use this form.