Skip to content

Commit 1c9dc5b

Browse files
author
catch
committed
Issue #3481778 by nicxvan, oily, dww, quietone, smustgrave, daffie, longwave, mikelutz: Deprecate functions using ModuleHandler::add()
1 parent 80cff8d commit 1c9dc5b

File tree

11 files changed

+119
-59
lines changed

11 files changed

+119
-59
lines changed

authorize.php

-7
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,6 @@ function authorize_access_allowed(Request $request) {
8585
exit;
8686
}
8787

88-
// We have to enable the user and system modules, even to check access and
89-
// display errors via the maintenance theme.
90-
\Drupal::moduleHandler()->addModule('system', 'core/modules/system');
91-
\Drupal::moduleHandler()->addModule('user', 'core/modules/user');
92-
\Drupal::moduleHandler()->load('system');
93-
\Drupal::moduleHandler()->load('user');
94-
9588
// Initialize the maintenance theme for this administrative script.
9689
drupal_maintenance_theme();
9790

includes/install.core.inc

-9
Original file line numberDiff line numberDiff line change
@@ -493,15 +493,6 @@ function install_begin_request($class_loader, &$install_state) {
493493
\Drupal::translation()->setDefaultLangcode($install_state['parameters']['langcode']);
494494
}
495495

496-
// Override the module list with a minimal set of modules.
497-
$module_handler = \Drupal::moduleHandler();
498-
if (!$module_handler->moduleExists('system')) {
499-
$module_handler->addModule('system', 'core/modules/system');
500-
}
501-
if ($profile && !$module_handler->moduleExists($profile)) {
502-
$module_handler->addProfile($profile, $install_state['profiles'][$profile]->getPath());
503-
}
504-
505496
// Load all modules and perform request related initialization.
506497
$kernel->preHandle($request);
507498

lib/Drupal/Core/DrupalKernel.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ public function discoverServiceProviders() {
626626

627627
// Retrieve enabled modules and register their namespaces.
628628
if (!isset($this->moduleList)) {
629-
$extensions = $this->getConfigStorage()->read('core.extension');
629+
$extensions = $this->getExtensions();
630630
// If core.extension configuration does not exist and we're not in the
631631
// installer itself, then we need to put the kernel into a pre-installer
632632
// mode. The container should not be dumped because Drupal is yet to be
@@ -1689,7 +1689,7 @@ protected function addServiceFiles(array $service_yamls) {
16891689
* no install profile or NULL if Drupal is being installed.
16901690
*/
16911691
protected function getInstallProfile() {
1692-
$config = $this->getConfigStorage()->read('core.extension');
1692+
$config = $this->getExtensions();
16931693
if (is_array($config) && !array_key_exists('profile', $config)) {
16941694
return FALSE;
16951695
}
@@ -1715,4 +1715,14 @@ protected function initializeEphemeralSession(Request $request): void {
17151715
$request->setSession($session);
17161716
}
17171717

1718+
/**
1719+
* Get the core.extension config object.
1720+
*
1721+
* @return array|false
1722+
* The core.extension config object if it exists or FALSE.
1723+
*/
1724+
protected function getExtensions(): array|false {
1725+
return $this->getConfigStorage()->read('core.extension');
1726+
}
1727+
17181728
}

lib/Drupal/Core/Extension/ModuleHandler.php

+6
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,15 @@ public function setModuleList(array $module_list = []) {
166166
* {@inheritdoc}
167167
*/
168168
public function addModule($name, $path) {
169+
@trigger_error(__METHOD__ . '() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. There is no direct replacement. See https://www.drupal.org/node/3491200', E_USER_DEPRECATED);
169170
$this->add('module', $name, $path);
170171
}
171172

172173
/**
173174
* {@inheritdoc}
174175
*/
175176
public function addProfile($name, $path) {
177+
@trigger_error(__METHOD__ . '() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. There is no direct replacement. See https://www.drupal.org/node/3491200', E_USER_DEPRECATED);
176178
$this->add('profile', $name, $path);
177179
}
178180

@@ -185,6 +187,10 @@ public function addProfile($name, $path) {
185187
* The module name; e.g., 'node'.
186188
* @param string $path
187189
* The module path; e.g., 'core/modules/node'.
190+
*
191+
* @deprecated in drupal:11.2.0 and is removed from drupal:12.0.0.
192+
* There is no direct replacement.
193+
* @see https://www.drupal.org/node/3491200
188194
*/
189195
protected function add($type, $name, $path) {
190196
$pathname = "$path/$name.info.yml";

lib/Drupal/Core/Extension/ModuleHandlerInterface.php

+8
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public function setModuleList(array $module_list = []);
8282
* The module name; e.g., 'node'.
8383
* @param string $path
8484
* The module path; e.g., 'core/modules/node'.
85+
*
86+
* @deprecated in drupal:11.2.0 and is removed from drupal:12.0.0.
87+
* There is no direct replacement.
88+
* @see https://www.drupal.org/node/3491200
8589
*/
8690
public function addModule($name, $path);
8791

@@ -92,6 +96,10 @@ public function addModule($name, $path);
9296
* The profile name; e.g., 'standard'.
9397
* @param string $path
9498
* The profile path; e.g., 'core/profiles/standard'.
99+
*
100+
* @deprecated in drupal:11.2.0 and is removed from drupal:12.0.0.
101+
* There is no direct replacement.
102+
* @see https://www.drupal.org/node/3491200
95103
*/
96104
public function addProfile($name, $path);
97105

lib/Drupal/Core/Installer/InstallerKernel.php

+14
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,18 @@ protected function attachSynthetic(ContainerInterface $container): void {
9797
$this->container?->reset();
9898
}
9999

100+
/**
101+
* {@inheritdoc}
102+
*/
103+
protected function getExtensions(): array {
104+
$extensions = parent::getExtensions() ?: [];
105+
// Ensure that the System module is always available to the installer.
106+
$extensions['module']['system'] ??= 0;
107+
if (empty($extensions['profile']) && !empty($GLOBALS['install_state']) && ($profile = _install_select_profile($GLOBALS['install_state']))) {
108+
$extensions['profile'] = $profile;
109+
$extensions['module'][$profile] = 1000;
110+
}
111+
return $extensions;
112+
}
113+
100114
}

modules/migrate_drupal/tests/src/Kernel/StateFileExistsTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ public function testMigrationState(): void {
8585
$modules_enabled = $module_handler->getModuleList();
8686
$modules_to_enable = array_keys(array_diff_key($all_modules, $modules_enabled));
8787
$this->enableModules($modules_to_enable);
88+
// Note that the kernel has rebuilt the container in enableModules this
89+
// $module_handler is no longer the $module_handler instance from above.
90+
$module_handler = $this->container->get('module_handler');
8891

8992
// Modules with a migrate_drupal.yml file.
9093
$has_state_file = (new YamlDiscovery('migrate_drupal', array_map(function ($value) {

tests/Drupal/KernelTests/Core/Extension/ExtensionExistsConstraintValidatorTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function testValidation(): void {
4545
$this->assertSame("Module 'user' is not installed.", (string) $violations->get(0)->getMessage());
4646

4747
$this->enableModules(['user']);
48+
$data = $typed_data->create($definition, 'core');
4849
$this->assertCount(0, $data->validate());
4950

5051
// NULL should not trigger a validation error: a value may be nullable.

tests/Drupal/KernelTests/KernelTestBase.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -839,20 +839,21 @@ protected function enableModules(array $modules) {
839839
// the event dispatcher which can prevent modules from registering events.
840840
$active_storage = $this->container->get('config.storage');
841841
$extension_config = $active_storage->read('core.extension');
842+
$extensions = $module_handler->getModuleList();
842843

843844
foreach ($modules as $module) {
844845
if ($module_handler->moduleExists($module)) {
845846
continue;
846847
}
847-
$module_handler->addModule($module, $module_list[$module]->getPath());
848+
$extensions[$module] = $module_list[$module];
848849
// Maintain the list of enabled modules in configuration.
849850
$extension_config['module'][$module] = 0;
850851
}
851852
$active_storage->write('core.extension', $extension_config);
852853

853854
// Update the kernel to make their services available.
854-
$extensions = $module_handler->getModuleList();
855855
$this->container->get('kernel')->updateModules($extensions, $extensions);
856+
$this->container = $this->container->get('kernel')->getContainer();
856857

857858
// Ensure isLoaded() is TRUE in order to make
858859
// \Drupal\Core\Theme\ThemeManagerInterface::render() work.

tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php

+71-33
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,31 @@ protected function setUp(): void {
5151
* @return \Drupal\Core\Extension\ModuleHandler
5252
* The module handler to test.
5353
*/
54-
protected function getModuleHandler($implementations = []) {
55-
$module_handler = new ModuleHandler($this->root, [], $this->eventDispatcher, $implementations);
56-
$module_handler->addModule('module_handler_test', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test');
57-
return $module_handler;
54+
protected function getModuleHandler($modules = [], $implementations = [], $loadAll = TRUE) {
55+
// This only works if there's a single $hook.
56+
if ($implementations) {
57+
$listeners = array_map(fn ($function) => [new ProceduralCall([]), $function], array_keys($implementations));
58+
$this->eventDispatcher->expects($this->once())
59+
->method('getListeners')
60+
->with("drupal_hook.hook")
61+
->willReturn($listeners);
62+
$implementations = ['hook' => [ProceduralCall::class => $implementations]];
63+
}
64+
$modules['module_handler_test'] = 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test';
65+
$moduleList = [];
66+
foreach ($modules as $module => $path) {
67+
$filename = "$module.module";
68+
$moduleList[$module] = [
69+
'type' => 'module',
70+
'pathname' => "$path/$module.info.yml",
71+
'filename' => file_exists("$this->root/$path/$filename") ? $filename : NULL,
72+
];
73+
}
74+
$moduleHandler = new ModuleHandler($this->root, $moduleList, $this->eventDispatcher, $implementations);
75+
if ($loadAll) {
76+
$moduleHandler->loadAll();
77+
}
78+
return $moduleHandler;
5879
}
5980

6081
/**
@@ -63,11 +84,13 @@ protected function getModuleHandler($implementations = []) {
6384
* @covers ::load
6485
*/
6586
public function testLoadModule(): void {
66-
$module_handler = $this->getModuleHandler();
87+
$moduleList = [
88+
'module_handler_test_added' => 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added',
89+
];
90+
$module_handler = $this->getModuleHandler($moduleList);
6791
$this->assertTrue($module_handler->load('module_handler_test'));
6892
$this->assertTrue(function_exists('module_handler_test_hook'));
6993

70-
$module_handler->addModule('module_handler_test_added', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added');
7194
$this->assertTrue($module_handler->load('module_handler_test_added'));
7295
$this->assertTrue(function_exists('module_handler_test_added_helper'), 'Function exists after being loaded.');
7396
$this->assertTrue($module_handler->load('module_handler_test_added'));
@@ -81,9 +104,11 @@ public function testLoadModule(): void {
81104
* @covers ::loadAll
82105
*/
83106
public function testLoadAllModules(): void {
84-
$module_handler = $this->getModuleHandler();
85-
$module_handler->addModule('module_handler_test_all1', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all1');
86-
$module_handler->addModule('module_handler_test_all2', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all2');
107+
$moduleList = [
108+
'module_handler_test_all1' => 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all1',
109+
'module_handler_test_all2' => 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all2',
110+
];
111+
$module_handler = $this->getModuleHandler($moduleList);
87112
$module_handler->loadAll();
88113
$this->assertTrue(function_exists('module_handler_test_all1_hook'), 'Function exists after being loaded.');
89114
$this->assertTrue(function_exists('module_handler_test_all2_hook'), 'Function exists after being loaded.');
@@ -109,20 +134,14 @@ public function testModuleReloading(): void {
109134
->onlyMethods(['load'])
110135
->getMock();
111136
$calls = [
112-
// First reload.
113-
'module_handler_test',
114-
// Second reload.
115137
'module_handler_test',
116-
'module_handler_test_added',
117138
];
118-
$module_handler->expects($this->exactly(count($calls)))
139+
$module_handler->expects($this->once())
119140
->method('load')
120141
->with($this->callback(function (string $module) use (&$calls): bool {
121142
return $module === array_shift($calls);
122143
}));
123144
$module_handler->reload();
124-
$module_handler->addModule('module_handler_test_added', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added');
125-
$module_handler->reload();
126145
}
127146

128147
/**
@@ -131,7 +150,7 @@ public function testModuleReloading(): void {
131150
* @covers ::isLoaded
132151
*/
133152
public function testIsLoaded(): void {
134-
$module_handler = $this->getModuleHandler();
153+
$module_handler = $this->getModuleHandler(loadAll: FALSE);
135154
$this->assertFalse($module_handler->isLoaded());
136155
$module_handler->loadAll();
137156
$this->assertTrue($module_handler->isLoaded());
@@ -197,9 +216,11 @@ public function testSetModuleList(): void {
197216
*
198217
* @covers ::addModule
199218
* @covers ::add
219+
*
220+
* @group legacy
200221
*/
201222
public function testAddModule(): void {
202-
223+
$this->expectDeprecation('Drupal\Core\Extension\ModuleHandler::addModule() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. There is no direct replacement. See https://www.drupal.org/node/3491200');
203224
$module_handler = $this->getMockBuilder(ModuleHandler::class)
204225
->setConstructorArgs([
205226
$this->root, [], $this->eventDispatcher, [],
@@ -219,9 +240,11 @@ public function testAddModule(): void {
219240
*
220241
* @covers ::addProfile
221242
* @covers ::add
243+
*
244+
* @group legacy
222245
*/
223246
public function testAddProfile(): void {
224-
247+
$this->expectDeprecation('Drupal\Core\Extension\ModuleHandler::addProfile() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. There is no direct replacement. See https://www.drupal.org/node/3491200');
225248
$module_handler = $this->getMockBuilder(ModuleHandler::class)
226249
->setConstructorArgs([
227250
$this->root, [], $this->eventDispatcher, [],
@@ -310,15 +333,18 @@ public function testInvokeModuleEnabled(): void {
310333
* @covers ::loadAllIncludes
311334
*/
312335
public function testImplementsHookModuleEnabled(): void {
313-
$implementations['hook'][ProceduralCall::class]['module_handler_test_hook'] = 'module_handler_test';
314-
$module_handler = $this->getModuleHandler($implementations);
336+
$implementations = [
337+
'module_handler_test_hook' => 'module_handler_test',
338+
'module_handler_test_added_hook' => 'module_handler_test_added',
339+
];
340+
$moduleList = [
341+
'module_handler_test_added' => 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added',
342+
'module_handler_test_no_hook' => 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_no_hook',
343+
];
344+
$module_handler = $this->getModuleHandler($moduleList, $implementations);
315345

316346
$this->assertTrue($module_handler->hasImplementations('hook', 'module_handler_test'), 'Installed module implementation found.');
317-
318-
$module_handler->addModule('module_handler_test_added', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added');
319347
$this->assertTrue($module_handler->hasImplementations('hook', 'module_handler_test_added'), 'Runtime added module with implementation in include found.');
320-
321-
$module_handler->addModule('module_handler_test_no_hook', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_no_hook');
322348
$this->assertFalse($module_handler->hasImplementations('hook', 'module_handler_test_no_hook'), 'Missing implementation not found.');
323349
}
324350

@@ -328,9 +354,16 @@ public function testImplementsHookModuleEnabled(): void {
328354
* @covers ::invokeAll
329355
*/
330356
public function testInvokeAll(): void {
331-
$module_handler = $this->getModuleHandler();
332-
$module_handler->addModule('module_handler_test_all1', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all1');
333-
$module_handler->addModule('module_handler_test_all2', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all2');
357+
$implementations = [
358+
'module_handler_test_hook' => 'module_handler_test',
359+
'module_handler_test_all1_hook' => 'module_handler_test_all1',
360+
'module_handler_test_all2_hook' => 'module_handler_test_all2',
361+
];
362+
$moduleList = [
363+
'module_handler_test_all1' => 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all1',
364+
'module_handler_test_all2' => 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all2',
365+
];
366+
$module_handler = $this->getModuleHandler($moduleList, $implementations);
334367
$this->assertEquals([TRUE, TRUE, TRUE], $module_handler->invokeAll('hook', [TRUE]));
335368
}
336369

@@ -349,7 +382,7 @@ function some_method(): void {
349382

350383
};
351384
$implementations['some_hook'][get_class($c)]['some_method'] = 'some_module';
352-
$module_handler = $this->getModuleHandler($implementations);
385+
$module_handler = new ModuleHandler($this->root, [], $this->eventDispatcher, $implementations, []);
353386
$module_handler->setModuleList(['some_module' => TRUE]);
354387
$r = new \ReflectionObject($module_handler);
355388

@@ -376,10 +409,15 @@ function some_method(): void {
376409
* @covers ::getModuleDirectories
377410
*/
378411
public function testGetModuleDirectories(): void {
379-
$module_handler = $this->getModuleHandler();
380-
$module_handler->setModuleList([]);
381-
$module_handler->addModule('node', 'core/modules/node');
382-
$this->assertEquals(['node' => $this->root . '/core/modules/node'], $module_handler->getModuleDirectories());
412+
$moduleList = [
413+
'node' => 'core/modules/node',
414+
];
415+
$module_handler = $this->getModuleHandler($moduleList);
416+
$moduleDirectories = [
417+
'node' => $this->root . '/core/modules/node',
418+
'module_handler_test' => $this->root . '/core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test',
419+
];
420+
$this->assertEquals($moduleDirectories, $module_handler->getModuleDirectories());
383421
}
384422

385423
/**

tests/Drupal/Tests/UpdatePathTestTrait.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,7 @@ protected function runUpdates($update_url = NULL) {
9898
$modules_installed = FALSE;
9999
// Modules that are in configuration but not the module handler have been
100100
// installed.
101-
/** @var \Drupal\Core\Extension\ModuleExtensionList $module_list */
102-
$module_list = $this->container->get('extension.list.module');
103-
foreach (array_keys(array_diff_key($config_module_list, $module_handler_list)) as $module) {
104-
$module_handler->addModule($module, $module_list->getPath($module));
105-
$modules_installed = TRUE;
106-
}
101+
$modules_installed = !empty(array_diff_key($config_module_list, $module_handler_list));
107102
$modules_uninstalled = FALSE;
108103
$module_handler_list = $module_handler->getModuleList();
109104
// Modules that are in the module handler but not configuration have been

0 commit comments

Comments
 (0)