class ModuleHandler implements ModuleHandlerInterface (View source)

Class that manages modules in a Drupal installation.

Properties

protected array $loadedFiles

List of loaded files.

protected Extension[] $moduleList

List of installed modules.

protected bool $loaded

Boolean indicating whether modules have been loaded.

protected array $implementations

List of hook implementations keyed by hook name.

protected true[] $verified

List of hooks where the implementations have been "verified".

protected array $hookInfo

Information returned by hook_hook_info() implementations.

protected CacheBackendInterface $cacheBackend

Cache backend for storing module hook implementation information.

protected bool $cacheNeedsWriting

Whether the cache needs to be written.

protected array $alterFunctions

List of alter hook implementations keyed by hook name(s).

protected string $root

The app root.

protected array $includeFileKeys

A list of module include file keys.

Methods

__construct(string $root, array $module_list, CacheBackendInterface $cache_backend)

Constructs a ModuleHandler object.

bool
load(string $name)

Includes a module's .module file.

loadAll()

Loads all enabled modules.

reload()

Reloads all enabled modules.

bool
isLoaded()

Returns whether all modules have been loaded.

getModuleList()

Returns the list of currently active modules.

getModule(string $name)

Returns a module extension object from the currently active modules list.

setModuleList(array $module_list = [])

Sets an explicit list of currently active modules.

addModule(string $name, string $path)

Adds a module to the list of currently active modules.

addProfile(string $name, string $path)

Adds an installation profile to the list of currently active modules.

add(string $type, string $name, string $path)

Adds a module or profile to the list of currently active modules.

The
buildModuleDependencies(array $modules)

Determines which modules require and are required by each module.

bool
moduleExists(string $module)

Determines whether a given module is enabled.

loadAllIncludes(string $type, string $name = NULL)

Loads an include file for each enabled module.

string|false
loadInclude(string $module, string $type, string $name = NULL)

Loads a module include file.

array
getHookInfo()

Retrieves a list of hooks that are declared through hook_hook_info().

buildHookInfo()

Builds hook_hook_info() information.

array
getImplementations(string $hook)

Determines which modules are implementing a hook.

writeCache()

Write the hook implementation info to the cache.

resetImplementations()

Resets the cached list of hook implementations.

bool
implementsHook(string $module, string $hook)

Returns whether a given module implements a given hook.

mixed
invoke(string $module, string $hook, array $args = [])

Invokes a hook in a particular module.

array
invokeAll(string $hook, array $args = [])

Invokes a hook in all enabled modules that implement it.

mixed
invokeDeprecated(string $description, string $module, string $hook, array $args = [])

Invokes a deprecated hook in a particular module.

array
invokeAllDeprecated(string $description, string $hook, array $args = [])

Invokes a deprecated hook in all enabled modules that implement it.

alter(string|array $type, mixed $data, mixed $context1 = NULL, mixed $context2 = NULL)

Passes alterable variables to specific hook_TYPE_alter() implementations.

alterDeprecated(string $description, string|array $type, mixed $data, mixed $context1 = NULL, mixed $context2 = NULL)

Passes alterable variables to deprecated hook_TYPE_alter() implementations.

array
getImplementationInfo(string $hook)

Provides information about modules' implementations of a hook.

array
buildImplementationInfo(string $hook)

Builds hook implementation information for a given hook name.

bool
verifyImplementations(string[] $implementations, string $hook)

Verifies an array of implementations loaded from the cache, by including the lazy-loaded $module.$group.inc, and checking function_exists().

array
getModuleDirectories()

Returns an array of directories for all enabled modules. Useful for tasks such as finding a file that exists in all module directories.

string
getName(string $module)

Gets the human readable name of a given module.

Details

__construct(string $root, array $module_list, CacheBackendInterface $cache_backend)

Constructs a ModuleHandler object.

Parameters

string $root

The app root.

array $module_list

An associative array whose keys are the names of installed modules and whose values are Extension class parameters. This is normally the %container.modules% parameter being set up by DrupalKernel.

CacheBackendInterface $cache_backend

Cache backend for storing module hook implementation information.

See also

DrupalKernel
CoreServiceProvider

bool load(string $name)

Includes a module's .module file.

This prevents including a module more than once.

Parameters

string $name

The name of the module to load.

Return Value

bool

TRUE if the item is loaded or has already been loaded.

loadAll()

Loads all enabled modules.

reload()

Reloads all enabled modules.

bool isLoaded()

Returns whether all modules have been loaded.

Return Value

bool

A Boolean indicating whether all modules have been loaded. This means all modules; the load status of bootstrap modules cannot be checked.

Extension[] getModuleList()

Returns the list of currently active modules.

Return Value

Extension[]

An associative array whose keys are the names of the modules and whose values are Extension objects.

Extension getModule(string $name)

Returns a module extension object from the currently active modules list.

Parameters

string $name

The name of the module to return.

Return Value

Extension

An extension object.

Exceptions

UnknownExtensionException

setModuleList(array $module_list = [])

Sets an explicit list of currently active modules.

Parameters

array $module_list

An associative array whose keys are the names of the modules and whose values are Extension objects.

addModule(string $name, string $path)

Adds a module to the list of currently active modules.

Parameters

string $name

The module name; e.g., 'node'.

string $path

The module path; e.g., 'core/modules/node'.

addProfile(string $name, string $path)

Adds an installation profile to the list of currently active modules.

Parameters

string $name

The profile name; e.g., 'standard'.

string $path

The profile path; e.g., 'core/profiles/standard'.

protected add(string $type, string $name, string $path)

Adds a module or profile to the list of currently active modules.

Parameters

string $type

The extension type; either 'module' or 'profile'.

string $name

The module name; e.g., 'node'.

string $path

The module path; e.g., 'core/modules/node'.

The buildModuleDependencies(array $modules)

Determines which modules require and are required by each module.

Parameters

array $modules

An array of module objects keyed by module name. Each object contains information discovered during a Drupal\Core\Extension\ExtensionDiscovery scan.

Return Value

The

same array with the new keys for each module:

  • requires: An array with the keys being the modules that this module requires.
  • required_by: An array with the keys being the modules that will not work without this module.

bool moduleExists(string $module)

Determines whether a given module is enabled.

Parameters

string $module

The name of the module (without the .module extension).

Return Value

bool

TRUE if the module is both installed and enabled.

loadAllIncludes(string $type, string $name = NULL)

Loads an include file for each enabled module.

Parameters

string $type

The include file's type (file extension).

string $name

(optional) The base file name (without the $type extension). If omitted, each module's name is used; i.e., "$module.$type" by default.

string|false loadInclude(string $module, string $type, string $name = NULL)

Loads a module include file.

Examples:

Parameters

string $module

The module to which the include file belongs.

string $type

The include file's type (file extension).

string $name

(optional) The base file name (without the $type extension). If omitted, $module is used; i.e., resulting in "$module.$type" by default.

Return Value

string|false

The name of the included file, if successful; FALSE otherwise.

array getHookInfo()

Retrieves a list of hooks that are declared through hook_hook_info().

Return Value

array

An associative array whose keys are hook names and whose values are an associative array containing a group name. The structure of the array is the same as the return value of hook_hook_info().

protected buildHookInfo()

Builds hook_hook_info() information.

array getImplementations(string $hook)

Determines which modules are implementing a hook.

Parameters

string $hook

The name of the hook (e.g. "help" or "menu").

Return Value

array

An array with the names of the modules which are implementing this hook.

writeCache()

Write the hook implementation info to the cache.

resetImplementations()

Resets the cached list of hook implementations.

bool implementsHook(string $module, string $hook)

Returns whether a given module implements a given hook.

Parameters

string $module

The name of the module (without the .module extension).

string $hook

The name of the hook (e.g. "help" or "menu").

Return Value

bool

TRUE if the module is both installed and enabled, and the hook is implemented in that module.

mixed invoke(string $module, string $hook, array $args = [])

Invokes a hook in a particular module.

Parameters

string $module

The name of the module (without the .module extension).

string $hook

The name of the hook to invoke.

array $args

Arguments to pass to the hook implementation.

Return Value

mixed

The return value of the hook implementation.

array invokeAll(string $hook, array $args = [])

Invokes a hook in all enabled modules that implement it.

Parameters

string $hook

The name of the hook to invoke.

array $args

Arguments to pass to the hook.

Return Value

array

An array of return values of the hook implementations. If modules return arrays from their implementations, those are merged into one array recursively. Note: integer keys in arrays will be lost, as the merge is done using Drupal\Component\Utility\NestedArray::mergeDeepArray().

mixed invokeDeprecated(string $description, string $module, string $hook, array $args = [])

Invokes a deprecated hook in a particular module.

Invoking a deprecated hook adds the behavior of triggering an E_USER_DEPRECATED error if any implementations are found.

API maintainers should use this method instead of invoke() when their hook is deprecated. This method does not detect when a hook is deprecated.

Parameters

string $description

Helpful text describing what to do instead of implementing this hook.

string $module

The name of the module (without the .module extension).

string $hook

The name of the hook to invoke.

array $args

Arguments to pass to the hook implementation.

Return Value

mixed

The return value of the hook implementation.

array invokeAllDeprecated(string $description, string $hook, array $args = [])

Invokes a deprecated hook in all enabled modules that implement it.

Invoking a deprecated hook adds the behavior of triggering an E_USER_DEPRECATED error if any implementations are found.

API maintainers should use this method instead of invokeAll() when their hook is deprecated. This method does not detect when a hook is deprecated.

Parameters

string $description

Helpful text describing what to do instead of implementing this hook.

string $hook

The name of the hook to invoke.

array $args

Arguments to pass to the hook.

Return Value

array

An array of return values of the hook implementations. If modules return arrays from their implementations, those are merged into one array recursively. Note: integer keys in arrays will be lost, as the merge is done using Drupal\Component\Utility\NestedArray::mergeDeepArray().

alter(string|array $type, mixed $data, mixed $context1 = NULL, mixed $context2 = NULL)

Passes alterable variables to specific hook_TYPE_alter() implementations.

This dispatch function hands off the passed-in variables to type-specific hook_TYPE_alter() implementations in modules. It ensures a consistent interface for all altering operations.

A maximum of 2 alterable arguments is supported. In case more arguments need to be passed and alterable, modules provide additional variables assigned by reference in the last $context argument:

Parameters

string|array $type

A string describing the type of the alterable $data. 'form', 'links', 'node_content', and so on are several examples. Alternatively can be an array, in which case hook_TYPE_alter() is invoked for each value in the array, ordered first by module, and then for each module, in the order of values in $type. For example, when Form API is using $this->alter() to execute both hook_form_alter() and hook_form_FORM_IDalter() implementations, it passes array('form', 'form' . $form_id) for $type.

mixed $data

The variable that will be passed to hook_TYPE_alter() implementations to be altered. The type of this variable depends on the value of the $type argument. For example, when altering a 'form', $data will be a structured array. When altering a 'profile', $data will be an object.

mixed $context1

(optional) An additional variable that is passed by reference.

mixed $context2

(optional) An additional variable that is passed by reference. If more context needs to be provided to implementations, then this should be an associative array as described above.

alterDeprecated(string $description, string|array $type, mixed $data, mixed $context1 = NULL, mixed $context2 = NULL)

Passes alterable variables to deprecated hook_TYPE_alter() implementations.

This method triggers an E_USER_DEPRECATED error if any implementations of the alter hook are found. It is otherwise identical to alter().

See the documentation for alter() for more details.

Parameters

string $description

Helpful text describing what to do instead of implementing this alter hook.

string|array $type

A string describing the type of the alterable $data. 'form', 'links', 'node_content', and so on are several examples. Alternatively can be an array, in which case hook_TYPE_alter() is invoked for each value in the array, ordered first by module, and then for each module, in the order of values in $type. For example, when Form API is using $this->alter() to execute both hook_form_alter() and hook_form_FORM_IDalter() implementations, it passes array('form', 'form' . $form_id) for $type.

mixed $data

The variable that will be passed to hook_TYPE_alter() implementations to be altered. The type of this variable depends on the value of the $type argument. For example, when altering a 'form', $data will be a structured array. When altering a 'profile', $data will be an object.

mixed $context1

(optional) An additional variable that is passed by reference.

mixed $context2

(optional) An additional variable that is passed by reference. If more context needs to be provided to implementations, then this should be an associative array as described above.

protected array getImplementationInfo(string $hook)

Provides information about modules' implementations of a hook.

Parameters

string $hook

The name of the hook (e.g. "help" or "menu").

Return Value

array

An array whose keys are the names of the modules which are implementing this hook and whose values are either a string identifying a file in which the implementation is to be found, or FALSE, if the implementation is in the module file.

protected array buildImplementationInfo(string $hook)

Builds hook implementation information for a given hook name.

Parameters

string $hook

The name of the hook (e.g. "help" or "menu").

Return Value

array

An array whose keys are the names of the modules which are implementing this hook and whose values are either a string identifying a file in which the implementation is to be found, or FALSE, if the implementation is in the module file.

Exceptions

RuntimeException

See also

ModuleHandler::getImplementationInfo

protected bool verifyImplementations(string[] $implementations, string $hook)

Verifies an array of implementations loaded from the cache, by including the lazy-loaded $module.$group.inc, and checking function_exists().

Parameters

string[] $implementations

Implementation "group" by module name.

string $hook

The hook name.

Return Value

bool

TRUE, if all implementations exist. FALSE, if one or more implementations don't exist and need to be removed from the cache.

array getModuleDirectories()

Returns an array of directories for all enabled modules. Useful for tasks such as finding a file that exists in all module directories.

Return Value

array

string getName(string $module)

Gets the human readable name of a given module.

Parameters

string $module

The machine name of the module which title should be shown.

Return Value

string

Returns the human readable name of the module or the machine name passed in if no matching module is found.