What hook functions are generally included in .install file?

In Drupal, when you want to install a custom module, you must have a mycustommodule.install file. Implementations of the hooks below should be placed in a mycustommodule.install file in the same directory as mycustommodule.module. Some hooks are optional depends on how large your module is. For example, if your module doesn’t require any database table or variable, you wouldn’t have hook_schema in your .install file.

hook_disable Perform necessary actions before module is disabled.
hook_enable Perform necessary actions after module is enabled.
hook_install Install the current version of the database schema, and any other setup tasks.
hook_requirements Check installation requirements and do status reporting
hook_schema Define the current version of the database schema.
hook_uninstall Remove any tables or variables that the module sets.
hook_update_last_removed Return a number which is no longer available as hook_update_N().
hook_update_N Perform a single update.

hook_requirements($phase)

Check installation requirements and do status reporting. This hook must be located in .install file. Use parameter $phase to check installation requirements ($phase == ‘install’ ) and status reporting ($phase == ‘runtime’).

Parameters

$phase: There’re 2 phases to check the requirements during the installation and runtime.

‘install’: the module is being installed.
‘runtime’: the runtime requirements are being checked and shown on the status report page.

Return value

A keyed array of requirements.

  • ‘title’: the name of the requirement.
  • ‘value’: the current value (e.g. version, time, level, …). During install phase, this should only be used for version numbers, do not set it if not applicable.
  • ‘description’: description of the requirement/status.
  • ‘severity’: the requirement’s result/severity level, one of:
    • REQUIREMENT_INFO: For info only.
    • REQUIREMENT_OK: The requirement is satisfied.
    • REQUIREMENT_WARNING: The requirement failed with a warning.
    • REQUIREMENT_ERROR: The requirement failed with an error.

Usage sample:

<?php
function hook_requirements($phase) {   
   // Ensure translations don't break at install time   
   $requirements = array();
   $t = get_t();   // Report Drupal version
   if ($phase == 'runtime') {
     $requirements['drupal'] = array(       'title'--> $t('Drupal'),
      'value' => VERSION,
      'severity' => REQUIREMENT_INFO
    );
  }

  // Test PHP version
  $requirements['php'] = array(
    'title' => $t('PHP'),
    'value' => ($phase == 'runtime') ? l(phpversion(), 'admin/logs/status/php') : phpversion(),
  );
  if (version_compare(phpversion(), DRUPAL_MINIMUM_PHP) < 0) {
   $requirements['php']['description'] = $t('Your PHP installation is too old. Drupal requires at least PHP %version.', array('%version' => DRUPAL_MINIMUM_PHP));
    $requirements['php']['severity'] = REQUIREMENT_ERROR;
  }

  // Report cron status
  if ($phase == 'runtime') {
    $cron_last = variable_get('cron_last', NULL);

    if (is_numeric($cron_last)) {
      $requirements['cron']['value'] = $t('Last run !time ago', array('!time' => format_interval(time() - $cron_last)));
    }
    else {
      $requirements['cron'] = array(
        'description' => $t('Cron has not run. It appears cron jobs have not been setup on your system. Please check the help pages for configuring cron jobs.', array('@url' => 'http://drupal.org/cron')),
        'severity' => REQUIREMENT_ERROR,
        'value' => $t('Never run'),
      );
    }

    $requirements['cron']['description'] .= ' '. t('You can run cron manually.', array('@cron' => url('admin/logs/status/run-cron')));

    $requirements['cron']['title'] = $t('Cron maintenance tasks');
  }
  
   return $requirements;
}
?>

Note: this hook must go into your .install file, in order to block the installation of the module unless requirements exist. If it’s in your .module file, only the runtime phase will execute. A lot of contributed modules get this wrong.


hook_install()

Install the current version of the database schema, and any other setup tasks. The hook will be called the first time a module is installed, and the module’s schema version will be set to the module’s greatest numbered update hook.

The implementation of hook_install() will need to explicitly load the module before any declared functions may be invoked.

Anything added or modified in this function that can be removed during uninstall should be removed with hook_uninstall().

Parameters

No parameters

Return value

none

Usage sample

/*
* Implementation of hook_install().
*/
function videoregistration_install() {
drupal_install_schema(‘videoregistration_type’);
}


hook_uninstall()

This hook will remove any tables or variables that the module has set. The uninstall hook will fire when the module gets uninstalled.

Parameters

None

Return value

None

Usage sample

function videopage_uninstall() {
drupal_uninstall_schema(‘videoshows’);
variable_del(‘video_types’);
}


@ sign in front of variable

Sometimes in your programming you’ll need to use @ sign in front of a variable. For example, @$_REQUEST[“username”]; The @ sign is an error control operator. When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.

For example:

$value = @$cache[$key];
// will not issue a notice if the index $key doesn't exist.