Hooks

Same name and namespace in other branches
  1. 8.9.x core/core.api.php \hooks
  2. 10 core/core.api.php \hooks
  3. 11.x core/core.api.php \hooks

Define functions that alter the behavior of Drupal core.

One way for modules to alter the core behavior of Drupal (or another module) is to use hooks. Hooks are specially-named functions that a module defines (this is known as "implementing the hook"), which are discovered and called at specific times to alter or add to the base behavior or data (this is known as "invoking the hook"). Each hook has a name (example: hook_batch_alter()), a defined set of parameters, and a defined return value. Your modules can implement hooks that are defined by Drupal core or other modules that they interact with. Your modules can also define their own hooks, in order to let other modules interact with them.

To implement a hook:

  • Locate the documentation for the hook. Hooks are documented in *.api.php files, by defining functions whose name starts with "hook_" (these files and their functions are never loaded by Drupal -- they exist solely for documentation). The function should have a documentation header, as well as a sample function body. For example, in the core file form.api.php, you can find hooks such as hook_batch_alter(). Also, if you are viewing this documentation on an API reference site, the Core hooks will be listed in this topic.
  • Copy the function to your module's .module file.
  • Change the name of the function, substituting your module's short name (name of the module's directory, and .info.yml file without the extension) for the "hook" part of the sample function name. For instance, to implement hook_batch_alter(), you would rename it to my_module_batch_alter().
  • Edit the documentation for the function (normally, your implementation should just have one line saying "Implements hook_batch_alter().").
  • Edit the body of the function, substituting in what you need your module to do.

To define a hook:

  • Choose a unique name for your hook. It should start with "hook_", followed by your module's short name.
  • Provide documentation in a *.api.php file in your module's main directory. See the "implementing" section above for details of what this should contain (parameters, return value, and sample function body).
  • Invoke the hook in your module's code.

To invoke a hook, use methods on \Drupal\Core\Extension\ModuleHandlerInterface such as alter(), invoke(), and invokeAll(). You can obtain a module handler by calling \Drupal::moduleHandler(), or getting the 'module_handler' service on an injected container.

See also

Extending and altering Drupal

Theme system overview

Callbacks

\Drupal\Core\Extension\ModuleHandlerInterface

\Drupal::moduleHandler()

File

core/core.api.php, line 1600

Functions

Title Sort descending File name Summary
hook_aggregator_fetcher_info_alter core/modules/aggregator/aggregator.api.php Perform alterations on the available fetchers.
hook_aggregator_parser_info_alter core/modules/aggregator/aggregator.api.php Perform alterations on the available parsers.
hook_aggregator_processor_info_alter core/modules/aggregator/aggregator.api.php Perform alterations on the available processors.
hook_ajax_render_alter core/lib/Drupal/Core/Form/form.api.php Alter the Ajax command data that is sent to the client.
hook_archiver_info_alter core/lib/Drupal/Core/File/file.api.php Alter archiver information declared by other modules.
hook_batch_alter core/lib/Drupal/Core/Form/form.api.php Alter batch information before a batch is processed.
hook_block_access core/modules/block/block.api.php Control access to a block instance.
hook_block_alter core/modules/block/block.api.php Allow modules to alter the block plugin definitions.
hook_block_build_alter core/modules/block/block.api.php Alter the result of \Drupal\Core\Block\BlockBase::build().
hook_block_build_BASE_BLOCK_ID_alter core/modules/block/block.api.php Provide a block plugin specific block_build alteration.
hook_block_view_alter core/modules/block/block.api.php Alter the result of \Drupal\Core\Block\BlockBase::build().
hook_block_view_BASE_BLOCK_ID_alter core/modules/block/block.api.php Provide a block plugin specific block_view alteration.
hook_cache_flush core/core.api.php Flush all persistent and static caches.
hook_ckeditor_css_alter core/modules/ckeditor/ckeditor.api.php Modify the list of CSS files that will be added to a CKEditor instance.
hook_ckeditor_plugin_info_alter core/modules/ckeditor/ckeditor.api.php Modify the list of available CKEditor plugins.
hook_comment_links_alter core/modules/comment/comment.api.php Alter the links of a comment.
hook_condition_info_alter core/core.api.php Alter the information provided in \Drupal\Core\Condition\ConditionManager::getDefinitions().
hook_config_import_steps_alter core/core.api.php Alter the configuration synchronization steps.
hook_config_schema_info_alter core/core.api.php Alter config typed data definitions.
hook_contextual_links_alter core/lib/Drupal/Core/Menu/menu.api.php Alter contextual links before they are rendered.
hook_contextual_links_plugins_alter core/lib/Drupal/Core/Menu/menu.api.php Alter the plugin definition of contextual links.
hook_contextual_links_view_alter core/modules/contextual/contextual.api.php Alter a contextual links element before it is rendered.
hook_countries_alter core/core.api.php Alter the default country list.
hook_cron core/core.api.php Perform periodic actions.
hook_css_alter core/lib/Drupal/Core/Render/theme.api.php Alter CSS files before they are output on the page.
hook_data_type_info_alter core/core.api.php Alter available data types for typed data wrappers.
hook_display_variant_plugin_alter core/core.api.php Alter display variant plugin definitions.
hook_editor_info_alter core/modules/editor/editor.api.php Performs alterations on text editor definitions.
hook_editor_js_settings_alter core/modules/editor/editor.api.php Modifies JavaScript settings that are added for text editors.
hook_editor_xss_filter_alter core/modules/editor/editor.api.php Modifies the text editor XSS filter that will used for the given text format.
hook_element_info_alter core/lib/Drupal/Core/Render/theme.api.php Alter the element type information returned from modules.
hook_element_plugin_alter core/lib/Drupal/Core/Render/theme.api.php Alter Element plugin definitions.
hook_entity_access core/lib/Drupal/Core/Entity/entity.api.php Control entity operation access.
hook_entity_base_field_info core/lib/Drupal/Core/Entity/entity.api.php Provides custom base field definitions for a content entity type.
hook_entity_base_field_info_alter core/lib/Drupal/Core/Entity/entity.api.php Alter base field definitions for a content entity type.
hook_entity_build_defaults_alter core/lib/Drupal/Core/Entity/entity.api.php Alter entity renderable values before cache checking during rendering.
hook_entity_bundle_create core/lib/Drupal/Core/Entity/entity.api.php Act on entity_bundle_create().
hook_entity_bundle_delete core/lib/Drupal/Core/Entity/entity.api.php Act on entity_bundle_delete().
hook_entity_bundle_field_info core/lib/Drupal/Core/Entity/entity.api.php Provides field definitions for a specific bundle within an entity type.
hook_entity_bundle_field_info_alter core/lib/Drupal/Core/Entity/entity.api.php Alter bundle field definitions.
hook_entity_bundle_info core/lib/Drupal/Core/Entity/entity.api.php Describe the bundles for entity types.
hook_entity_bundle_info_alter core/lib/Drupal/Core/Entity/entity.api.php Alter the bundles for entity types.
hook_entity_create core/lib/Drupal/Core/Entity/entity.api.php Acts when creating a new entity.
hook_entity_create_access core/lib/Drupal/Core/Entity/entity.api.php Control entity create access.
hook_entity_delete core/lib/Drupal/Core/Entity/entity.api.php Respond to entity deletion.
hook_entity_display_build_alter core/lib/Drupal/Core/Entity/entity.api.php Alter the render array generated by an EntityDisplay for an entity.
hook_entity_extra_field_info core/lib/Drupal/Core/Entity/entity.api.php Exposes "pseudo-field" components on content entities.
hook_entity_extra_field_info_alter core/lib/Drupal/Core/Entity/entity.api.php Alter "pseudo-field" components on content entities.
hook_entity_field_access core/lib/Drupal/Core/Entity/entity.api.php Control access to fields.
hook_entity_field_access_alter core/lib/Drupal/Core/Entity/entity.api.php Alter the default access behavior for a given field.
hook_entity_field_storage_info core/lib/Drupal/Core/Entity/entity.api.php Provides field storage definitions for a content entity type.
hook_entity_field_storage_info_alter core/lib/Drupal/Core/Entity/entity.api.php Alter field storage definitions for a content entity type.
hook_entity_field_values_init core/lib/Drupal/Core/Entity/entity.api.php Acts when initializing a fieldable entity object.
hook_entity_form_display_alter core/lib/Drupal/Core/Entity/entity.api.php Alter the settings used for displaying an entity form.
hook_entity_form_mode_alter core/lib/Drupal/Core/Entity/entity.api.php Change the form mode used to build an entity form.
hook_entity_insert core/lib/Drupal/Core/Entity/entity.api.php Respond to creation of a new entity.
hook_entity_load core/lib/Drupal/Core/Entity/entity.api.php Act on entities when loaded.
hook_entity_operation core/lib/Drupal/Core/Entity/entity.api.php Declares entity operations.
hook_entity_operation_alter core/lib/Drupal/Core/Entity/entity.api.php Alter entity operations.
hook_entity_predelete core/lib/Drupal/Core/Entity/entity.api.php Act before entity deletion.
hook_entity_preload core/lib/Drupal/Core/Entity/entity.api.php Act on an array of entity IDs before they are loaded.
hook_entity_prepare_form core/lib/Drupal/Core/Entity/entity.api.php Acts on an entity object about to be shown on an entity form.
hook_entity_prepare_view core/lib/Drupal/Core/Entity/entity.api.php Act on entities as they are being prepared for view.
hook_entity_presave core/lib/Drupal/Core/Entity/entity.api.php Act on an entity before it is created or updated.
hook_entity_revision_create core/lib/Drupal/Core/Entity/entity.api.php Respond to entity revision creation.
hook_entity_revision_delete core/lib/Drupal/Core/Entity/entity.api.php Respond to entity revision deletion.
hook_entity_storage_load core/lib/Drupal/Core/Entity/entity.api.php Act on content entities when loaded from the storage.
hook_entity_translation_create core/lib/Drupal/Core/Entity/entity.api.php Acts when creating a new entity translation.
hook_entity_translation_delete core/lib/Drupal/Core/Entity/entity.api.php Respond to entity translation deletion.
hook_entity_translation_insert core/lib/Drupal/Core/Entity/entity.api.php Respond to creation of a new entity translation.
hook_ENTITY_TYPE_access core/lib/Drupal/Core/Entity/entity.api.php Control entity operation access for a specific entity type.
hook_entity_type_alter core/lib/Drupal/Core/Entity/entity.api.php Alter the entity type definitions.
hook_entity_type_build core/lib/Drupal/Core/Entity/entity.api.php Add to entity type definitions.
hook_ENTITY_TYPE_build_defaults_alter core/lib/Drupal/Core/Entity/entity.api.php Alter entity renderable values before cache checking during rendering.
hook_ENTITY_TYPE_create core/lib/Drupal/Core/Entity/entity.api.php Acts when creating a new entity of a specific type.
hook_ENTITY_TYPE_create_access core/lib/Drupal/Core/Entity/entity.api.php Control entity create access for a specific entity type.
hook_ENTITY_TYPE_delete core/lib/Drupal/Core/Entity/entity.api.php Respond to entity deletion of a particular type.
hook_ENTITY_TYPE_field_values_init core/lib/Drupal/Core/Entity/entity.api.php Acts when initializing a fieldable entity object.
hook_ENTITY_TYPE_insert core/lib/Drupal/Core/Entity/entity.api.php Respond to creation of a new entity of a particular type.
hook_ENTITY_TYPE_load core/lib/Drupal/Core/Entity/entity.api.php Act on entities of a specific type when loaded.
hook_ENTITY_TYPE_predelete core/lib/Drupal/Core/Entity/entity.api.php Act before entity deletion of a particular entity type.
hook_ENTITY_TYPE_prepare_form core/lib/Drupal/Core/Entity/entity.api.php Acts on a particular type of entity object about to be in an entity form.
hook_ENTITY_TYPE_presave core/lib/Drupal/Core/Entity/entity.api.php Act on a specific type of entity before it is created or updated.
hook_ENTITY_TYPE_revision_create core/lib/Drupal/Core/Entity/entity.api.php Respond to entity revision creation.
hook_ENTITY_TYPE_revision_delete core/lib/Drupal/Core/Entity/entity.api.php Respond to entity revision deletion of a particular type.
hook_ENTITY_TYPE_storage_load core/lib/Drupal/Core/Entity/entity.api.php Act on content entities of a given type when loaded from the storage.
hook_ENTITY_TYPE_translation_create core/lib/Drupal/Core/Entity/entity.api.php Acts when creating a new entity translation of a specific type.
hook_ENTITY_TYPE_translation_delete core/lib/Drupal/Core/Entity/entity.api.php Respond to entity translation deletion of a particular type.
hook_ENTITY_TYPE_translation_insert core/lib/Drupal/Core/Entity/entity.api.php Respond to creation of a new entity translation of a particular type.
hook_ENTITY_TYPE_update core/lib/Drupal/Core/Entity/entity.api.php Respond to updates to an entity of a particular type.
hook_ENTITY_TYPE_view core/lib/Drupal/Core/Entity/entity.api.php Act on entities of a particular type being assembled before rendering.
hook_ENTITY_TYPE_view_alter core/lib/Drupal/Core/Entity/entity.api.php Alter the results of the entity build array for a particular entity type.
hook_entity_update core/lib/Drupal/Core/Entity/entity.api.php Respond to updates to an entity.
hook_entity_view core/lib/Drupal/Core/Entity/entity.api.php Act on entities being assembled before rendering.
hook_entity_view_alter core/lib/Drupal/Core/Entity/entity.api.php Alter the results of the entity build array.
hook_entity_view_display_alter core/lib/Drupal/Core/Entity/entity.api.php Alter the settings used for displaying an entity.
hook_entity_view_mode_alter core/lib/Drupal/Core/Entity/entity.api.php Change the view mode of an entity that is being displayed.
hook_entity_view_mode_info_alter core/lib/Drupal/Core/Entity/entity.api.php Alter the view modes for entity types.
hook_extension core/lib/Drupal/Core/Render/theme.api.php Declare a template file extension to be used with a theme engine.
hook_filetransfer_info core/lib/Drupal/Core/File/file.api.php Register information about FileTransfer classes provided by a module.
hook_filetransfer_info_alter core/lib/Drupal/Core/File/file.api.php Alter the FileTransfer class registry.
hook_file_download core/lib/Drupal/Core/File/file.api.php Control access to private file downloads and specify HTTP headers.
hook_file_mimetype_mapping_alter core/lib/Drupal/Core/File/file.api.php Alter MIME type mappings used to determine MIME type from a file extension.
hook_file_url_alter core/lib/Drupal/Core/File/file.api.php Alter the URL to a file.
hook_form_alter core/lib/Drupal/Core/Form/form.api.php Perform alterations before a form is rendered.
hook_form_BASE_FORM_ID_alter core/lib/Drupal/Core/Form/form.api.php Provide a form-specific alteration for shared ('base') forms.
hook_form_FORM_ID_alter core/lib/Drupal/Core/Form/form.api.php Provide a form-specific alteration instead of the global hook_form_alter().
hook_form_system_theme_settings_alter core/lib/Drupal/Core/Render/theme.api.php Allow themes to alter the theme-specific settings form.
hook_hal_relation_uri_alter core/modules/hal/hal.api.php Alter the HAL relation URI.
hook_hal_type_uri_alter core/modules/hal/hal.api.php Alter the HAL type URI.
hook_hook_info core/lib/Drupal/Core/Extension/module.api.php Defines one or more hooks that are exposed by a module.
hook_image_effect_info_alter core/modules/image/image.api.php Alter the information provided in \Drupal\image\Annotation\ImageEffect.
hook_image_style_flush core/modules/image/image.api.php Respond to image style flushing.
hook_install core/lib/Drupal/Core/Extension/module.api.php Perform setup tasks when the module is installed.
hook_install_tasks core/lib/Drupal/Core/Extension/module.api.php Return an array of tasks to be performed by an installation profile.
hook_install_tasks_alter core/lib/Drupal/Core/Extension/module.api.php Alter the full list of installation tasks.
hook_js_alter core/lib/Drupal/Core/Render/theme.api.php Alters JavaScript before it is presented on the page.
hook_js_settings_alter core/lib/Drupal/Core/Render/theme.api.php Perform necessary alterations to the JavaScript settings (drupalSettings).
hook_js_settings_build core/lib/Drupal/Core/Render/theme.api.php Modify the JavaScript settings (drupalSettings).
hook_language_switch_links_alter core/lib/Drupal/Core/Language/language.api.php Perform alterations on language switcher links.
hook_layout_alter core/core.api.php Allow modules to alter layout plugin definitions.
hook_library_info_alter core/lib/Drupal/Core/Render/theme.api.php Alter libraries provided by an extension.
hook_library_info_build core/lib/Drupal/Core/Render/theme.api.php Add dynamic library definitions.
hook_link_alter core/lib/Drupal/Core/Menu/menu.api.php Alter the parameters for links.
hook_local_tasks_alter core/lib/Drupal/Core/Menu/menu.api.php Alter local tasks plugins.
hook_mail core/core.api.php Prepares a message based on parameters.
hook_mail_alter core/core.api.php Alter an email message created with MailManagerInterface->mail().
hook_mail_backend_info_alter core/core.api.php Alter the list of mail backend plugin definitions.
hook_menu_links_discovered_alter core/lib/Drupal/Core/Menu/menu.api.php Alters all the menu links discovered by the menu link plugin manager.
hook_menu_local_actions_alter core/lib/Drupal/Core/Menu/menu.api.php Alter local actions plugins.
hook_menu_local_tasks_alter core/lib/Drupal/Core/Menu/menu.api.php Alter local tasks displayed on the page before they are rendered.
hook_modules_installed core/lib/Drupal/Core/Extension/module.api.php Perform necessary actions after modules are installed.
hook_modules_uninstalled core/lib/Drupal/Core/Extension/module.api.php Perform necessary actions after modules are uninstalled.
hook_module_implements_alter core/lib/Drupal/Core/Extension/module.api.php Alter the registry of modules implementing a hook.
hook_module_preinstall core/lib/Drupal/Core/Extension/module.api.php Perform necessary actions before a module is installed.
hook_module_preuninstall core/lib/Drupal/Core/Extension/module.api.php Perform necessary actions before a module is uninstalled.
hook_page_attachments core/lib/Drupal/Core/Render/theme.api.php Add attachments (typically assets) to a page before it is rendered.
hook_page_attachments_alter core/lib/Drupal/Core/Render/theme.api.php Alter attachments (typically assets) to a page before it is rendered.
hook_page_bottom core/lib/Drupal/Core/Render/theme.api.php Add a renderable array to the bottom of the page.
hook_page_top core/lib/Drupal/Core/Render/theme.api.php Add a renderable array to the top of the page.
hook_plugin_filter_TYPE_alter core/lib/Drupal/Core/Plugin/plugin.api.php Alter the filtering of plugin definitions for a specific plugin type.
hook_plugin_filter_TYPE__CONSUMER_alter core/lib/Drupal/Core/Plugin/plugin.api.php Alter the filtering of plugin definitions for a specific type and consumer.
hook_post_update_NAME core/lib/Drupal/Core/Extension/module.api.php Executes an update which is intended to update data, like entities.
hook_preprocess core/lib/Drupal/Core/Render/theme.api.php Preprocess theme variables for templates.
hook_preprocess_HOOK core/lib/Drupal/Core/Render/theme.api.php Preprocess theme variables for a specific theme hook.
hook_query_alter core/lib/Drupal/Core/Database/database.api.php Perform alterations to a structured query.
hook_query_TAG_alter core/lib/Drupal/Core/Database/database.api.php Perform alterations to a structured query for a given tag.
hook_queue_info_alter core/core.api.php Alter cron queue information before cron runs.
hook_quickedit_editor_alter core/modules/quickedit/quickedit.api.php Allow modules to alter in-place editor plugin metadata.
hook_quickedit_render_field core/modules/quickedit/quickedit.api.php Returns a renderable array for the value of a single field in an entity.
hook_rebuild core/core.api.php Rebuild data based upon refreshed caches.
hook_removed_post_updates core/lib/Drupal/Core/Extension/module.api.php Return an array of removed hook_post_update_NAME() function names.
hook_render_template core/lib/Drupal/Core/Render/theme.api.php Render a template using the theme engine.
hook_requirements core/lib/Drupal/Core/Extension/module.api.php Check installation requirements and do status reporting.
hook_requirements_alter core/lib/Drupal/Core/Extension/module.api.php Alters requirements data.
hook_schema core/lib/Drupal/Core/Database/database.api.php Define the current version of the database schema.
hook_system_breadcrumb_alter core/lib/Drupal/Core/Menu/menu.api.php Perform alterations to the breadcrumb built by the BreadcrumbManager.
hook_system_info_alter core/lib/Drupal/Core/Extension/module.api.php Alter the information parsed from module and theme .info.yml files.
hook_system_themes_page_alter core/modules/system/system.api.php Alters theme operation links.
hook_template_preprocess_default_variables_alter core/lib/Drupal/Core/Render/theme.api.php Alter the default, hook-independent variables for all templates.
hook_theme core/lib/Drupal/Core/Render/theme.api.php Register a module or theme's theme implementations.
hook_themes_installed core/lib/Drupal/Core/Render/theme.api.php Respond to themes being installed.
hook_themes_uninstalled core/lib/Drupal/Core/Render/theme.api.php Respond to themes being uninstalled.
hook_theme_registry_alter core/lib/Drupal/Core/Render/theme.api.php Alter the theme registry information returned from hook_theme().
hook_theme_suggestions_alter core/lib/Drupal/Core/Render/theme.api.php Alters named suggestions for all theme hooks.
hook_theme_suggestions_HOOK core/lib/Drupal/Core/Render/theme.api.php Provides alternate named suggestions for a specific theme hook.
hook_theme_suggestions_HOOK_alter core/lib/Drupal/Core/Render/theme.api.php Alters named suggestions for a specific theme hook.
hook_tokens core/lib/Drupal/Core/Utility/token.api.php Provide replacement values for placeholder tokens.
hook_tokens_alter core/lib/Drupal/Core/Utility/token.api.php Alter replacement values for placeholder tokens.
hook_token_info core/lib/Drupal/Core/Utility/token.api.php Provide information about available placeholder tokens and token types.
hook_token_info_alter core/lib/Drupal/Core/Utility/token.api.php Alter the metadata about available placeholder tokens and token types.
hook_toolbar core/modules/toolbar/toolbar.api.php Add items to the toolbar menu.
hook_toolbar_alter core/modules/toolbar/toolbar.api.php Alter the toolbar menu after hook_toolbar() is invoked.
hook_transliteration_overrides_alter core/lib/Drupal/Core/Language/language.api.php Provide language-specific overrides for transliteration.
hook_uninstall core/lib/Drupal/Core/Extension/module.api.php Remove any information that the module sets.
hook_updater_info core/lib/Drupal/Core/Extension/module.api.php Provide information on Updaters (classes that can update Drupal).
hook_updater_info_alter core/lib/Drupal/Core/Extension/module.api.php Alter the Updater information array.
hook_update_dependencies core/lib/Drupal/Core/Extension/module.api.php Return an array of information about module update dependencies.
hook_update_last_removed core/lib/Drupal/Core/Extension/module.api.php Return a number which is no longer available as hook_update_N().
hook_update_N core/lib/Drupal/Core/Extension/module.api.php Perform a single update between minor versions.
hook_update_projects_alter core/modules/update/update.api.php Alter the list of projects before fetching data and comparing versions.
hook_update_status_alter core/modules/update/update.api.php Alter the information about available updates for projects.
hook_user_cancel core/modules/user/user.api.php Act on user account cancellations.
hook_user_cancel_methods_alter core/modules/user/user.api.php Modify account cancellation methods.
hook_user_format_name_alter core/modules/user/user.api.php Alter the username that is displayed for a user.
hook_user_login core/modules/user/user.api.php The user just logged in.
hook_user_logout core/modules/user/user.api.php The user just logged out.
hook_validation_constraint_alter core/core.api.php Alter validation constraint plugin definitions.
hook_verify_update_archive core/modules/update/update.api.php Verify an archive after it has been downloaded and extracted.
hook_views_ui_display_tab_alter core/modules/views_ui/views_ui.api.php Alter the renderable array representing the edit page for one display.
hook_views_ui_display_top_alter core/modules/views_ui/views_ui.api.php Alter the top of the display for the Views UI.
hook_views_ui_display_top_links_alter core/modules/views_ui/views_ui.api.php Alter the links displayed at the top of the view edit form.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.