function hook_install
Same name in other branches
- 7.x modules/system/system.api.php \hook_install()
- 9 core/lib/Drupal/Core/Extension/module.api.php \hook_install()
- 8.9.x core/lib/Drupal/Core/Extension/module.api.php \hook_install()
- 10 core/lib/Drupal/Core/Extension/module.api.php \hook_install()
Perform setup tasks when the module is installed.
Only procedural implementations are supported for this hook.
If the module implements hook_schema(), the database tables will be created before this hook is fired.
If the module provides a MODULE.routing.yml or alters routing information these changes will not be available when this hook is fired. If up-to-date router information is required, for example to use \Drupal\Core\Url, then (preferably) use hook_modules_installed() or rebuild the router in the hook_install() implementation.
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 when a module is installed. 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.
Ensure that anything added or modified in this function that can be removed during uninstall should be removed with hook_uninstall().
Parameters
bool $is_syncing: TRUE if the module is being installed as part of a configuration import. In these cases, your hook implementation needs to carefully consider what changes to configuration objects or configuration entities. Those changes should be made earlier and exported so during import there's no need to do them again.
See also
\Drupal\Core\Config\ConfigInstallerInterface::isSyncing
\Drupal\Core\Extension\ModuleInstaller::install()
Related topics
40 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.
- block_install in core/
modules/ block/ block.install - Implements hook_install().
- comment_install in core/
modules/ comment/ comment.install - Implements hook_install().
- config_test_install in core/
modules/ config/ tests/ config_test/ config_test.install - Implements hook_install().
- contact_storage_test_install in core/
modules/ contact/ tests/ modules/ contact_storage_test/ contact_storage_test.install - Implements hook_install().
- content_moderation_test_resave_install in core/
modules/ content_moderation/ tests/ modules/ content_moderation_test_resave/ content_moderation_test_resave.install - Implements hook_install().
2 invocations of hook_install()
- ModuleInstaller::doInstall in core/
lib/ Drupal/ Core/ Extension/ ModuleInstaller.php - Installs a set of modules.
- NormalizerTestBase::setUp in core/
modules/ serialization/ tests/ src/ Kernel/ NormalizerTestBase.php
File
-
core/
lib/ Drupal/ Core/ Extension/ module.api.php, line 258
Code
function hook_install($is_syncing) : void {
// Set general module variables.
\Drupal::state()->set('my_module.foo', 'bar');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.