function hook_install

7 system.api.php hook_install()
4.7 install.php hook_install()
5 install.php hook_install()
6 install.php hook_install()
8 system.api.php hook_install()

Perform setup tasks when the module is installed.

If the module implements hook_schema(), the database tables will be created before this hook is fired.

Implementations of this hook are by convention declared in the module's .install file. The implementation can rely on the .module file being loaded. The hook will only be called the first time a module is enabled or after it is re-enabled after being uninstalled. The module's schema version will be set to the module's greatest numbered update hook. Because of this, any time a hook_update_N() is added to the module, this function needs to be updated to reflect the current version of the database schema.

See the Schema API documentation for details on hook_schema and how database tables are defined.

Note that since this function is called from a full bootstrap, all functions (including those in modules enabled by the current page request) are available when this hook is called. Use cases could be displaying a user message, or calling a module function necessary for initial setup, etc.

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

See also

hook_schema()

module_enable()

hook_enable()

hook_disable()

hook_uninstall()

hook_modules_installed()

Related topics

28 functions implement hook_install()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

actions_loop_test_install in modules/simpletest/tests/actions_loop_test.install
Implements hook_install().
block_install in modules/block/block.install
Implements hook_install().
blog_install in modules/blog/blog.install
Implements hook_install().
book_install in modules/book/book.install
Implements hook_install().
contact_install in modules/contact/contact.install
Implements hook_install().

... See full list

3 invocations of hook_install()
drupal_check_module in includes/install.inc
Checks a module's requirements.
drupal_install_system in includes/install.inc
Installs the system module.
module_enable in includes/module.inc
Enables or installs a given list of modules.

File

modules/system/system.api.php, line 3298
Hooks provided by Drupal core and the System module.

Code

function hook_install() {
  // Populate the default {node_access} record.
  db_insert('node_access')->fields(array(
    'nid' => 0,
    'gid' => 0,
    'realm' => 'all',
    'grant_view' => 1,
    'grant_update' => 0,
    'grant_delete' => 0,
  ))->execute();
}

Comments

"Note that since this function is called from a full bootstrap, all functions (including those in modules enabled by the current page request) are available when this hook is called." It sounds like it, from that statement, that functions from the module you are enabling are available. We should maybe clarify that a bit more because in the drupal 6 version of this hook, they are not and you will get an undefined function error.

http://api.drupal.org/api/drupal/developer--hooks--install.php/function/...

Note that this function needs to live in a .install file, as .module files will not be loaded during install/uninstall actions.

Hi,

In case I want to restrict the installation of the current module through a condition like checking with available extension or server requirements.

How do I display an error message instead installing the module successfully.
Any ways ??

hook_requirements() is nice, but it is called after the .module file is interpreted. Depending on which restrictions you are trying to determine, it may not be the right hook.

hook_install() can be used to cancel installation by throwing an Exception:

<?php
/**
* Implements hook_install()
*/
function mymodule_install() {
 
$t = get_t();
  if (!
function_exists('function_from_another_module')) {
    throw new
Exception($t('My module cannot be installed because of a missing function.'));
  }
}
?>