4.2. What Are Hooks, Form Handlers, and Overrides?

When describing what makes Drupal great, many developers cite the fact that Drupal provides the most commonly needed functionality without any custom code but can easily be modified to suit very specific needs. One of the main reasons people choose not to use a framework is that it isn't flexible enough or specific enough to handle a certain business purpose. To solve that common issue, Drupal has created ways to alter its functionality with API features such as hooks, handlers, and overrides.

Hooks:

Hooks are specifically named pieces of code that are called when a certain event happens. One commonly used hook is hook_nodeapi, which is called for various events in the node system, such as the creation of a new piece of content. If your module needs to take an action in response to the creation of a node, you should create a function in "yourmodule" called yourmodule_nodeapi. This function is referred to as a hook implementation of hook_nodeapi. When a hook is processed by Drupal core, all the matching hook functions in all the enabled modules are executed one after another.

Form Handlers:

By default, the Drupal form API will look for and call specific functions to validate and process a form. If you wish to modify this behavior, you can use the hook for altering forms—hook_form_alter—to add new functions to validate the form or process the form when it is submitted. These functions are called validation handlers and submit handlers ...

Get Cracking Drupal®: A Drop in the Bucket now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.