diff --git a/core/lib/Drupal/Core/Extension/ModuleInstaller.php b/core/lib/Drupal/Core/Extension/ModuleInstaller.php index 09f8434370edec2dc5113da26df7b07b2087bc30..3540a42ef0d0ce39c01098a151e9f79be8f4fb69 100644 --- a/core/lib/Drupal/Core/Extension/ModuleInstaller.php +++ b/core/lib/Drupal/Core/Extension/ModuleInstaller.php @@ -193,14 +193,9 @@ public function install(array $module_list, $enable_dependencies = TRUE) { } } - // Update the module handler in order to load the module's code. - // This allows the module to participate in hooks and its existence to - // be discovered by other modules. - // The current ModuleHandler instance is obsolete with the kernel - // rebuild below. + // Update the module handler in order to have the correct module list + // for the kernel update. $this->moduleHandler->setModuleList($module_filenames); - $this->moduleHandler->load($module); - module_load_install($module); // Clear the static cache of the "extension.list.module" service to pick // up the new module, since it merges the installation status of modules @@ -210,6 +205,10 @@ public function install(array $module_list, $enable_dependencies = TRUE) { // Update the kernel to include it. $this->updateKernel($module_filenames); + // Load the module's .module and .install files. + $this->moduleHandler->load($module); + module_load_install($module); + // Replace the route provider service with a version that will rebuild // if routes used during installation. This ensures that a module's // routes are available during installation. This has to occur before diff --git a/core/modules/system/tests/modules/module_autoload_test/module_autoload_test.module b/core/modules/system/tests/modules/module_autoload_test/module_autoload_test.module new file mode 100644 index 0000000000000000000000000000000000000000..cf6983bc7f3e28de413d1180dce9f759b72b784d --- /dev/null +++ b/core/modules/system/tests/modules/module_autoload_test/module_autoload_test.module @@ -0,0 +1,10 @@ +<?php + +/** + * @file + * Test module. + */ + +use Drupal\module_autoload_test\SomeClass; + +define('MODULE_AUTOLOAD_TEST_CONSTANT', SomeClass::TEST); diff --git a/core/modules/system/tests/modules/module_autoload_test/src/SomeClass.php b/core/modules/system/tests/modules/module_autoload_test/src/SomeClass.php index 11993b71f48ec91e60da4c0104651a0a62e0c1dd..65529f23dca7e6871904b654f586198c12441699 100644 --- a/core/modules/system/tests/modules/module_autoload_test/src/SomeClass.php +++ b/core/modules/system/tests/modules/module_autoload_test/src/SomeClass.php @@ -4,6 +4,8 @@ class SomeClass { + const TEST = '\Drupal\module_autoload_test\SomeClass::TEST'; + public function testMethod() { return 'Drupal\\module_autoload_test\\SomeClass::testMethod() was invoked.'; } diff --git a/core/modules/system/tests/src/Functional/Module/ClassLoaderTest.php b/core/modules/system/tests/src/Functional/Module/ClassLoaderTest.php index cb855e34a90f92c69131c6944307f9aa1d749acb..c22247b63fa32c01440df0162c713177ef492603 100644 --- a/core/modules/system/tests/src/Functional/Module/ClassLoaderTest.php +++ b/core/modules/system/tests/src/Functional/Module/ClassLoaderTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\system\Functional\Module; +use Drupal\module_autoload_test\SomeClass; use Drupal\Tests\BrowserTestBase; /** @@ -96,4 +97,19 @@ public function testMultipleModules() { $this->assertTrue(\Drupal::moduleHandler()->moduleExists('module_install_class_loader_test2'), 'The module_install_class_loader_test2 module has been installed.'); } + /** + * Tests that .module files can use class constants in main section. + */ + public function testAutoloadFromModuleFile() { + $this->assertFalse(defined('MODULE_AUTOLOAD_TEST_CONSTANT')); + $this->drupalLogin($this->rootUser); + $edit = [ + "modules[module_autoload_test][enable]" => TRUE, + ]; + $this->drupalPostForm('admin/modules', $edit, t('Install')); + $this->assertSession()->statusCodeEquals(200); + $this->resetAll(); + $this->assertSame(SomeClass::TEST, MODULE_AUTOLOAD_TEST_CONSTANT); + } + }