Using Quanta hooks

Using hooks in a custom module, allows you to interact with the Quanta flow, adding your personal functionalities to common site routines (viewing a node, loading a page, submitting a form...), and changing the way they normally behave. 

Before we start talking about it, be sure you are already familiar with the concept of hooking.

How to implement a hook in a custom Quanta module

Each module or class, can provide hook through the use of Environment's hook method

/**
   * Hook function - will look for all modulename_function in all active modules
   * and let the user alter the variables contained into &$vars.
   *
   * @param string $function
   *   The hook function name.
   * @param array $vars
   *   An array of variables.
   *
   * @return bool
   *   Returns TRUE if any module was implementing the hook.
   */  public function hook($function, &$vars = array()) {
    $env = &$this;
    $hooked = FALSE;
    foreach ($this->getLoadedModules() as $module) {
      $hook = $module['name'] . '_' . $function;
      if (function_exists($hook)) {
        $hook($env, $vars);
        $hooked = TRUE;
      }
    }
    return $hooked;
  }

So in example, running (anywhere in your module) the function $env->hook('checkfruits', $vars) 

function _mymodule_makefruits() {
  // Create an array where fruits are keys.
  $fruits = array(
    'apple' => TRUE,
    'orange' => TRUE,
    'strawberry' => TRUE,
    );

  $vars = array('fruits' => &$fruits);
  // Run the checkfruits hook.
  $env->hook('checkfruits', $vars);

  print_r($fruits);
}

Will trigger a 'checkfruits' hook. 

This means, other modules can then respond to the hook, and alter the objects passed by reference into $vars.

/**
 * Implements hook_checkfruits().
 *
 * Checks fruits in the basket.
 *
 * @param Environment $env
 *   The Environment.
 * @param array $vars
 *   An array of variables.
 */function _mymodule_checkfruits($env, $vars) {
  // Grab the apple, if present.
  if (isset($vars['fruits']['apple'])) {
    unset($vars['fruits']['apple']);
  }
  // Add the coconut, if not present.
  if (!isset($vars['fruits']['apple'])) {
    $vars['fruits']['coconut'] = TRUE;
  }
}

Common core hooks

There are several hooks already implemented in core modules, that you can use to interact with basic Quanta processes.
Some examples are: 


Doctor:

hook_doctor_pre_setup
hook_doctor_DOCTORACTION (check, setup, etc.)

Cron:

hook_cron

Forms:

hook_form_validate
hook_form_FORMID_form_validate
hook_form_submit
hook_form_FORMID_form_submit

Lists:
hook_list_item

Language:

hook_fallback_language

Node:

hook_node_open
hook_node_load_cache
hook_node_load
hook_node_build
hook_node_presave
hook_node_save
hook_node_after_save

Page:
hook_page_init
hook_body_classes
hook_page_after_build
hook_page_complete

Qtags
hook_qtag_preload
hook_qtag

Shadow

hook_shadow_CONTEXTID_extra

User

hook_user_pre_validate
hook_user_validate
hook_user_load
hook_user_open
hook_user_save
hook_user_after_save