function ModuleHandler::triggerErrorForDuplicateAlterHookListener
Triggers an error on duplicate alter listeners.
This is called when the same method is registered for multiple hooks, which are now part of the same alter call.
Parameters
list<string> $hooks: Hook names from the ->alter() call.
string $module: The module name for one of the hook implementations.
string $other_module: The module name for another hook implementation.
callable $listener: The hook listener.
string $identifier: String identifier of the hook listener.
1 call to ModuleHandler::triggerErrorForDuplicateAlterHookListener()
- ModuleHandler::getCombinedListeners in core/
lib/ Drupal/ Core/ Extension/ ModuleHandler.php - Builds a list of implementations for an alter hook.
File
-
core/
lib/ Drupal/ Core/ Extension/ ModuleHandler.php, line 560
Class
- ModuleHandler
- Class that manages modules in a Drupal installation.
Namespace
Drupal\Core\ExtensionCode
protected function triggerErrorForDuplicateAlterHookListener(array $hooks, string $module, string $other_module, callable $listener, string $identifier) : void {
$log_message_replacements = [
'@implementation' => is_array($listener) ? 'method ' . $identifier . '()' : 'function ' . $listener[1] . '()',
'@hooks' => "['" . implode("', '", $hooks) . "']",
];
if ($other_module !== $module) {
// There is conflicting information about which module this
// implementation is registered for. At this point we cannot even
// be sure if the module is the one from the main hook or the extra
// hook. This means that ordering may not work as expected and it is
// unclear if the intention is to execute the code multiple times. This
// can be resolved by using a separate method for alter hooks that
// implement on behalf of other modules.
trigger_error((string) new FormattableMarkup('The @implementation is registered for more than one of the alter hooks @hooks from the current ->alter() call, on behalf of different modules @module and @other_module. Only one instance will be part of the implementation list for this hook combination. For the purpose of ordering, the module @module will be used.', [
$log_message_replacements,
'@module' => "'{$module}'",
'@other_module' => "'{$other_module}'",
]), E_USER_WARNING);
}
else {
// There is no conflict, but probably one or more redundant #[Hook]
// attributes should be removed.
trigger_error((string) new FormattableMarkup('The @implementation is registered for more than one of the alter hooks @hooks from the current ->alter() call. Only one instance will be part of the implementation list for this hook combination.', $log_message_replacements), E_USER_NOTICE);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.