Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Understanding the Drupal Way - Hooks

In Drupal a hook is intended to provide a way for actions to be triggered during a point in the execution of a certain event or process. Hooks are defined within individual modules. Those modules then give the opportunity for other modules to "hook into" their processing by calling the hook at the appropriate time.

For example, the Link module makes use of hook_field_info to allow it to appear as an option for a type of field that can be added to a content type. If you look at the fields available to add to a content type when the Link module is disabled, you see that it does not appear in the list. When you enable the link module and then return to the field types that are available to add to a content type you will see that Link appears.

There are other hooks that the Link module must also enable to provide the functionality of that new field type. For instance, hook_field_formatter_info provides the different display types for a link. hook_field_formatter_view contains the actual logic that properly builds a link for display based on which display type was chosen.

The module that defined the hook_field_info hook is, not surprisingly, the Field module. One place that the hook is called is in _field_info_collate_types. Since I think I'm already getting a bit more technical than I really ought to be getting in this post, I won't go into more detail. Suffice it to say that this is where that list of possible fields gets defined for each content type.

Modules use module_implements, module_invoke and module_invoke_all to trigger their hook.

Well, that may have been a bit too much information! To recap, all that you should take away from this post is that prior to enabling the Link module, the Link field type did not appear in the list of available field types. After enabling the Link module it does appear. The method used to make this happen is called a Hook.

If you would like to learn more about Drupal you can take my "Drupal for Beginners" class for just $5.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web