function hook_init

Perform setup tasks for non-cached page requests.

This hook is run at the beginning of the page request. It is typically used to set up global parameters that are needed later in the request. When this hook is called, the theme and all modules are already loaded in memory.

This hook is not run on cached pages.

To add CSS or JS that should be present on all pages, modules should not implement this hook, but declare these files in their .info file.

See also

hook_boot()

Related topics

15 functions implement hook_init()

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

action_loop_test_init in drupal/core/modules/action/tests/action_loop_test/action_loop_test.module
Implements hook_init().
assetic_init in drupal/core/vendor/kriswallsmith/assetic/src/functions.php
Initializes the global Assetic object.
dblog_init in drupal/core/modules/dblog/dblog.module
Implements hook_init().
language_test_init in drupal/core/modules/language/tests/language_test.module
Implements hook_init().
locale_language_init in drupal/core/modules/locale/locale.module
Implements hook_language_init().

... See full list

2 invocations of hook_init()
LegacyRequestSubscriber::onKernelRequestLegacy in drupal/core/lib/Drupal/Core/EventSubscriber/LegacyRequestSubscriber.php
Initializes the rest of the legacy Drupal subsystems.
_drupal_bootstrap_full in drupal/core/includes/common.inc
Temporary BC function for scripts not using DrupalKernel.

File

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

Code

function hook_init() {

  // Since this file should only be loaded on the front page, it cannot be
  // declared in the info file.
  if (drupal_is_front_page()) {
    drupal_add_css(drupal_get_path('module', 'foo') . '/foo.css');
  }
}