function ModuleHandler::add

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Extension/ModuleHandler.php \Drupal\Core\Extension\ModuleHandler::add()
  2. 8.9.x core/lib/Drupal/Core/Extension/ModuleHandler.php \Drupal\Core\Extension\ModuleHandler::add()
  3. 10 core/lib/Drupal/Core/Extension/ModuleHandler.php \Drupal\Core\Extension\ModuleHandler::add()

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'.

Deprecated

in drupal:11.2.0 and is removed from drupal:12.0.0. There is no direct replacement.

See also

https://www.drupal.org/node/3491200

2 calls to ModuleHandler::add()
ModuleHandler::addModule in core/lib/Drupal/Core/Extension/ModuleHandler.php
Adds a module to the list of currently active modules.
ModuleHandler::addProfile in core/lib/Drupal/Core/Extension/ModuleHandler.php
Adds an installation profile to the list of currently active modules.

File

core/lib/Drupal/Core/Extension/ModuleHandler.php, line 234

Class

ModuleHandler
Class that manages modules in a Drupal installation.

Namespace

Drupal\Core\Extension

Code

protected function add($type, $name, $path) {
  $pathname = "{$path}/{$name}.info.yml";
  $php_file_path = $this->root . "/{$path}/{$name}.{$type}";
  if ($filename = file_exists($php_file_path) ? "{$name}.{$type}" : NULL) {
    include_once $php_file_path;
  }
  $this->moduleList[$name] = new Extension($this->root, $type, $pathname, $filename);
  $this->resetImplementations();
  $hook_collector = HookCollectorPass::collectAllHookImplementations([
    $name => [
      'pathname' => $pathname,
    ],
  ]);
  // A module freshly added will not be registered on the container yet.
  // ProceduralCall service does not yet know about it.
  // Note in HookCollectorPass:
  // phpcs:ignore Drupal.Files.LineLength
  // - $container->register(ProceduralCall::class, ProceduralCall::class)->addArgument($collector->includes);
  // Load all includes so the legacy section of invoke can handle hooks in
  // includes.
  $hook_collector->loadAllIncludes();
  // Register procedural implementations.
  foreach ($hook_collector->getImplementations() as $hook => $moduleImplements) {
    foreach ($moduleImplements as $module => $classImplements) {
      foreach ($classImplements[ProceduralCall::class] ?? [] as $method) {
        $this->listenersByHook[$hook][] = $method;
        $this->modulesByHook[$hook][] = $module;
        $this->invokeMap[$hook][$module][] = $method;
      }
    }
  }
}

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