function HookCollectorPass::writeToContainer

Writes collected definitions to the container builder.

Parameters

\Symfony\Component\DependencyInjection\ContainerBuilder $container: Container builder.

File

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

Class

HookCollectorPass
Collects and registers hook implementations.

Namespace

Drupal\Core\Hook

Code

protected function writeToContainer(ContainerBuilder $container) : void {
  // Gather includes for each hook_hook_info group. Store this in
  // $groupIncludes so the module handler includes the files at runtime when
  // the hooks are invoked.
  $groupIncludes = [];
  foreach ($this->hookInfo as $function) {
    foreach ($function() as $hook => $info) {
      if (isset($this->groupIncludes[$info['group']])) {
        $groupIncludes[$hook] = $this->groupIncludes[$info['group']];
      }
    }
  }
  $implementationsByHook = $this->calculateImplementations();
  static::registerHookServices($container, $implementationsByHook);
  $includes = $this->collectIncludesPerHook($implementationsByHook, $groupIncludes);
  $packed_order_operations = [];
  $order_operations = $this->getOrderOperations();
  foreach (preg_grep('@_alter$@', array_keys($order_operations)) as $alter_hook) {
    $packed_order_operations[$alter_hook] = array_map(fn(OrderOperation $operation) => $operation->pack(), $order_operations[$alter_hook]);
  }
  // Write aggregated data about hooks into a temporary parameter.
  // We use a dot prefixed parameter so it will automatically get cleaned up.
  // This will be stored in a keyvalue store in
  // \Drupal\Core\Hook\HookCollectorKeyValueWritePass.
  $container->setParameter('.hook_data', [
    'hook_list' => $implementationsByHook,
    'preprocess_for_suggestions' => $this->preprocessForSuggestions ?? [],
    'includes' => $includes,
    'group_includes' => $groupIncludes,
    'packed_order_operations' => $packed_order_operations,
  ]);
  // Remove converted flags, they are only needed while building the
  // container.
  $parameters = $container->getParameterBag();
  foreach ($parameters->all() as $name => $value) {
    if (str_ends_with($name, '.skip_procedural_hook_scan')) {
      $parameters->remove($name);
    }
  }
}

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