Hook Explanation

Ãœbercart was developed on the Drupal web site development framework. To interact with Drupal, the modules use functions called hooks. These are so named because they "hook" into Drupal at the right place to modify any aspect of the page, whether it's page loading, content serving, form submission, or anything else. A list of Drupal hooks may be found at the Drupal API website, http://api.drupal.org/api/HEAD/group/hooks.

For example, a Drupal module that restricts access to certain functions might include a hook_perm function to setup a new permission type that can be granted to user account roles. The function name is constructed with the simple formula (module name)_(hook name). When a hook is invoked, it looks through every active module for any function that matches the naming formula and calls it, passing it a determined set of arguments and expecting a certain type of return value. Hook functions must conform to these expectations to work properly.

In this example, the file uc_store.module wants to define a store administration permission that it can use to restrict access to the administration menu. This is accomplished with hook_perm, which expects no arguments and receives as its return value an array of permissions to add to the list of permissions.

<?php
/**
* Implementation of hook_perm().
*/
function uc_store_perm() {
  return array(
'administer store');
}
?>

Ãœbercart uses Drupal's design philosophy, providing access to any store function by creating and invoking its own hooks. This means contributed modules will be able to hook into virtually any part of the store and change the way it functions. By creating and documenting these hooks, we make it much easier for module developers to extend the cart system and incredibly easier for store owners who may have no knowledge of PHP to tweak the features of their cart. We want to eliminate the need for patching and hacking, avoiding a major pitfall of the mess that osCommerce has become.

What does this look like in Ãœbercart? Well, one hook that is already in place is hook_checkout_pane. This hook is used to define the available checkout panes that build the checkout form (at /cart/checkout) and review screen (at /cart/checkout/review). It also defines the installed panes and their default positions for administrative setup. The file uc_cart.module uses this function to define the default panes included with Ãœbercart. The function itself looks something like this:

<?php
/**
* Implementation of hook_checkout_pane().
*/
function uc_cart_checkout_pane() {
 
$panes[] = array(
   
'id' => 'cart',
   
'callback' => 'uc_checkout_pane_cart',
   
'title' => t('Cart Contents'),
   
'desc' => t("Display the contents of a customer's shopping cart."),
   
'weight' => 1,
   
'process' => FALSE,
   
'collapsible' => FALSE,
  );
  ...
  return
$panes;
}
?>

In this case, the function will build up the array of panes defined by the cart module and return that array. Some hooks are expected to return a value, like this one, whereas other hooks are simply there to allow you to fire off some code, like hook_order. Furthermore, some hooks won't be passed any arguments, while others will be passed a variable number of arguments. These expectations can be found in the handbook here.

This is a quick overview of how the hook system works. As hooks are developed, they will be documented in this section of the handbook for easy reference.

Enjoy! Cool