function HookCollectorPass::process

File

core/lib/Drupal/Core/Hook/HookCollectorPass.php, line 85

Class

HookCollectorPass
Collects and registers hook implementations.

Namespace

Drupal\Core\Hook

Code

public function process(ContainerBuilder $container) : void {
    $collector = static::collectAllHookImplementations($container->getParameter('container.modules'));
    $map = [];
    $container->register(ProceduralCall::class, ProceduralCall::class)
        ->addArgument($collector->includes);
    $groupIncludes = [];
    foreach ($collector->hookInfo as $function) {
        foreach ($function() as $hook => $info) {
            if (isset($collector->groupIncludes[$info['group']])) {
                $groupIncludes[$hook] = $collector->groupIncludes[$info['group']];
            }
        }
    }
    $definition = $container->getDefinition('module_handler');
    $definition->setArgument('$groupIncludes', $groupIncludes);
    foreach ($collector->implementations as $hook => $class_implementations) {
        foreach ($class_implementations as $class => $method_hooks) {
            if ($container->has($class)) {
                $definition = $container->findDefinition($class);
            }
            else {
                $definition = $container->register($class, $class)
                    ->setAutowired(TRUE);
            }
            foreach ($method_hooks as $method => $hook_data) {
                $map[$hook][$class][$method] = $hook_data['module'];
                $definition->addTag('kernel.event_listener', [
                    'event' => "drupal_hook.{$hook}",
                    'method' => $method,
                    'priority' => $hook_data['priority'],
                ]);
            }
        }
    }
    $container->setParameter('hook_implementations_map', $map);
}

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