Commit 2ca89760 authored by catch's avatar catch
Browse files

Issue #3505049 by nicxvan, godotislate, raveen_thakur51, sher1, catch,...

Issue #3505049 by nicxvan, godotislate, raveen_thakur51, sher1, catch, jnicola, johnv, reinimax, rudi teschner: Drupal 11.1.2 upgrade causes  \Drupal::$container is not initialized yet error

(cherry picked from commit 78813e01)
parent acc2daf0
Loading
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -230,7 +230,9 @@ public function addProfile($name, $path) {
  protected function add($type, $name, $path) {
    $pathname = "$path/$name.info.yml";
    $php_file_path = $this->root . "/$path/$name.$type";
    $filename = file_exists($php_file_path) ? "$name.$type" : NULL;
    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]]);
+2 −6
Original line number Diff line number Diff line
@@ -403,12 +403,6 @@ protected function collectModuleHookImplementations($dir, $module, $module_preg,
      $extension = $fileinfo->getExtension();
      $filename = $fileinfo->getPathname();

      if (($extension === 'module' || $extension === 'profile') && !$iterator->getDepth() && !$skip_procedural) {
        // There is an expectation for all modules and profiles to be loaded.
        // .module and .profile files are not supposed to be in subdirectories.
        // These need to be loaded even if the module has no procedural hooks.
        include_once $filename;
      }
      if ($extension === 'php') {
        $cached = $hook_file_cache->get($filename);
        if ($cached) {
@@ -512,11 +506,13 @@ protected function addProceduralImplementation(\SplFileInfo $fileinfo, string $h
    $function = $module . '_' . $hook;
    if ($hook === 'hook_info') {
      $this->hookInfo[] = $function;
      include_once $fileinfo->getPathname();
    }
    elseif ($hook === 'module_implements_alter') {
      $message = "$function without a #[LegacyModuleImplementsAlter] attribute is deprecated in drupal:11.2.0 and removed in drupal:12.0.0. See https://www.drupal.org/node/3496788";
      @trigger_error($message, E_USER_DEPRECATED);
      $this->moduleImplementsAlters[] = $function;
      include_once $fileinfo->getPathname();
    }
    $this->proceduralImplementations[$hook][] = $module;
    if ($fileinfo->getExtension() !== 'module') {
+5 −0
Original line number Diff line number Diff line
name: 'Container initialize'
type: module
description: 'Support module for HookCollectorPass testing.'
package: Testing
version: VERSION
+10 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Used to test bare container calls in .module files.
 */

declare(strict_types=1);

\Drupal::getContainer()->getParameter('site.path');
+61 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\system\Functional\Hook;

use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Url;

/**
 * Tests services in .module files.
 *
 * @group Hook
 */
class HookCollectorPassTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['container_initialize'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Tests installing a module with a Drupal container call outside functions.
   *
   * If this is removed then it needs to be moved to a test that installs modules through
   * admin/modules.
   */
  public function testContainerOutsideFunction(): void {
    $settings['settings']['rebuild_access'] = (object) [
      'value' => TRUE,
      'required' => TRUE,
    ];

    // This simulates installing the module and running a cache rebuild in a
    // separate request.
    $this->writeSettings($settings);
    $this->rebuildAll();
    $this->drupalGet(Url::fromUri('base:core/rebuild.php'));
    $this->assertSession()->pageTextNotContains('ContainerNotInitializedException');
    // Successful response from rebuild.php should redirect to the front page.
    $this->assertSession()->addressEquals('/');

    // If this file is removed then this test needs to be updated to trigger
    // the container rebuild error from https://www.drupal.org/i/3505049
    $config_module_file = $this->root . '/core/modules/system/tests/modules/container_initialize/container_initialize.module';
    $this->assertFileExists($config_module_file, 'This test depends on a container call in a .module file');
    // Confirm that the file still has a bare container call.
    $bare_container = "declare(strict_types=1);

\Drupal::getContainer()->getParameter('site.path');
";
    $file_content = file_get_contents($config_module_file);
    $this->assertStringContainsString($bare_container, $file_content, 'container_initialize.module container test feature is missing.');
  }

}