Skip to content
Snippets Groups Projects

Hook order class method split

Closed nicxvan requested to merge issue/drupal-3484217:3484217-hookOrder into 11.x
1 unresolved thread
3 files
+ 45
33
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -18,13 +18,15 @@ class HookOrder {
* The container builder.
* @param string $hook
* The name of the hook.
* @param string $class_and_method
* Class and method separated by :: containing the hook implementation.
* @param string $class
* Class containing the hook implementation which should be changed.
* @param string $method
* Method which should be changed.
*
* @return void
*/
public static function first(ContainerBuilder $container, string $hook, string $class_and_method): void {
self::changePriority($container, $hook, $class_and_method, TRUE);
public static function first(ContainerBuilder $container, string $hook, string $class, string $method): void {
self::changePriority($container, $hook, "$class::$method", TRUE);
}
/**
@@ -34,13 +36,15 @@ public static function first(ContainerBuilder $container, string $hook, string $
* The container builder.
* @param string $hook
* The name of the hook.
* @param string $class_and_method
* Class and method separated by :: containing the hook implementation.
* @param string $class
* Class containing the hook implementation which should be changed.
* @param string $method
* Method which should be changed.
*
* @return void
*/
public static function last(ContainerBuilder $container, string $hook, string $class_and_method): void {
self::changePriority($container, $hook, $class_and_method, FALSE);
public static function last(ContainerBuilder $container, string $hook, string $class, string $method): void {
self::changePriority($container, $hook, "$class::$method", FALSE);
}
/**
@@ -54,17 +58,18 @@ public static function last(ContainerBuilder $container, string $hook, string $c
* The container builder.
* @param string $hook
* The name of the hook.
* @param string $class_and_method
* Class and method separated by :: containing the hook implementation which
* should be changed.
* @param string $class
* Class containing the hook implementation which should be changed.
* @param string $method
* Method which should be changed.
* @param string ...$others
* A list specifying the other implementations this hook should fire
* before. Every list member is a class and method separated by ::.
*
* @return void
*/
public static function before(ContainerBuilder $container, string $hook, string $class_and_method, string ...$others): void {
self::changePriority($container, $hook, $class_and_method, TRUE, array_flip($others));
public static function before(ContainerBuilder $container, string $hook, string $class, string $method, string ...$others): void {
self::changePriority($container, $hook, "$class::$method", TRUE, $others);
}
/**
@@ -78,17 +83,18 @@ public static function before(ContainerBuilder $container, string $hook, string
* The container builder.
* @param string $hook
* The name of the hook.
* @param string $class_and_method
* Class and method separated by :: containing the hook implementation which
* should be changed.
* @param string $class
* Class containing the hook implementation which should be changed.
* @param string $method
* Method which should be changed.
* @param string ...$others
* A list specifying the other implementations this hook should fire
* before. Every list member is a class and method separated by ::.
*
* @return void
*/
public static function after(ContainerBuilder $container, string $hook, string $class_and_method, string ...$others): void {
self::changePriority($container, $hook, $class_and_method, FALSE, array_flip($others));
public static function after(ContainerBuilder $container, string $hook, string $class, string $method, string ...$others): void {
self::changePriority($container, $hook, "$class::$method", FALSE, $others);
}
/**
@@ -105,13 +111,19 @@ public static function after(ContainerBuilder $container, string $hook, string $
* TRUE for before/first, FALSE for after/last. Larger priority listeners
* fire first.
* @param array|null $others
* Other hook implementations to compare to, if any. The array is keyed by
* string containing a class and method separated by ::, the value is not
* used.
* Other hook implementations to compare to, if any. The array is an
* alternating list of class and method names.
*
* @return void
*/
protected static function changePriority(ContainerBuilder $container, string $hook, string $class_and_method, bool $should_be_larger, ?array $others = NULL): void {
if (isset($others)) {
$mapped = [];
for ($i = 0; $i < count($others); $i += 2) {
$mapped[$others[$i] . '::' . $others[$i + 1]] = TRUE;
    • I think there should be some sort of check that $others[$i + 1] exists, or that $others has an even number of items. So if one part of a class/method expected pair is missing, maybe throw an Exception?

Please register or sign in to reply
}
$others = $mapped;
}
$event = "drupal_hook.$hook";
foreach ($container->findTaggedServiceIds('kernel.event_listener') as $id => $attributes) {
foreach ($attributes as $key => $tag) {
Loading