Skip to content

Commit

Permalink
Merge pull request #1291 from necyberteam/md-dev
Browse files Browse the repository at this point in the history
Md dev
  • Loading branch information
a-pasquale authored Nov 21, 2024
2 parents 6d630f6 + 97aedf2 commit 3f80570
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions composer.patches.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"patches": {
"drupal/core": {
"ModuleHandler skips all hook implementations when invoked before the module files have been loaded": "patches/3207813-27.patch",
"More link is missing in pager...": "https://www.drupal.org/files/issues/2023-10-09/3381979-13.patch",
"big_pipe.js' checkMutation() does not check if node exists before using it": "https://www.drupal.org/files/issues/2024-01-21/drupal-big_pipe_does_not_check_before_using_node_object-3416141.patch",
"entity bundled fields in views": "https://www.drupal.org/files/issues/2024-05-08/2981047-190.patch"
Expand Down
82 changes: 82 additions & 0 deletions patches/3207813-27.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
diff --git a/core/lib/Drupal/Core/Extension/ModuleHandler.php b/core/lib/Drupal/Core/Extension/ModuleHandler.php
index 262865403e6..46c8ed3fa46 100644
--- a/core/lib/Drupal/Core/Extension/ModuleHandler.php
+++ b/core/lib/Drupal/Core/Extension/ModuleHandler.php
@@ -629,6 +629,12 @@ protected function getImplementationInfo($hook) {
protected function buildImplementationInfo($hook) {
$implementations = [];
$hook_info = $this->getHookInfo();
+
+ // In case it's an early stage and the hook info was loaded from cache, the
+ // module files are not yet loaded at this point and discovery below
+ // produces an empty list.
+ $this->loadAll();
+
foreach ($this->moduleList as $module => $extension) {
$include_file = isset($hook_info[$hook]['group']) && $this->loadInclude($module, 'inc', $module . '.' . $hook_info[$hook]['group']);
// Since $this->implementsHook() may needlessly try to load the include
@@ -676,6 +682,17 @@ protected function buildImplementationInfo($hook) {
* from the cache.
*/
protected function verifyImplementations(&$implementations, $hook) {
+ // Under certain circumstances the module files are not yet loaded. This
+ // happens for example when invoking a hook inside the constructor of a
+ // http_middleware service; these services are constructed very early as
+ // a dependency of http_kernel service. A more concrete example is a
+ // middleware service using the entity_type.manager. Most of the times
+ // the entity type information is retrieved from cache (stored in the
+ // discovery cache bin). When this cache however is missing, hooks
+ // like hook_entity_type_build() and hook_entity_type_alter() need to be
+ // invoked at this early stage.
+ $this->loadAll();
+
$all_valid = TRUE;
foreach ($implementations as $module => $group) {
// If this hook implementation is stored in a lazy-loaded file, include
diff --git a/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php b/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php
index c989ea870db..64122069129 100644
--- a/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php
+++ b/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php
@@ -391,6 +391,7 @@ function (callable $hook, string $module) use (&$implementors) {
* Tests getImplementations.
*
* @covers ::invokeAllWith
+ * @covers ::verifyImplementations
*/
public function testCachedGetImplementationsMissingMethod() {
$this->cacheBackend->expects($this->exactly(1))
@@ -417,7 +418,6 @@ public function testCachedGetImplementationsMissingMethod() {
])
->onlyMethods(['buildImplementationInfo'])
->getMock();
- $module_handler->load('module_handler_test');

$module_handler->expects($this->never())->method('buildImplementationInfo');
$implementors = [];
@@ -533,4 +533,26 @@ public function testGetModuleDirectories() {
$this->assertEquals(['node' => $this->root . '/core/modules/node'], $module_handler->getModuleDirectories());
}

+ /**
+ * Tests that modules are included in case of a partial cache miss.
+ *
+ * @covers ::hasImplementations
+ * @covers ::getImplementationInfo
+ * @covers ::buildImplementationInfo
+ */
+ public function testMissingHookImplementationCache() {
+ // Simulate missing cache entry for hook implementations, but existing one
+ // for hook info.
+ $this->cacheBackend
+ ->expects($this->exactly(2))
+ ->method('get')
+ ->willReturnMap([
+ ['hook_info', FALSE, (object) ['data' => []]],
+ ['module_implements', FALSE, FALSE],
+ ]);
+
+ $module_handler = $this->getModuleHandler();
+ $this->assertTrue($module_handler->hasImplementations('hook', 'module_handler_test'));
+ }
+
}

0 comments on commit 3f80570

Please sign in to comment.