Skip to content
Snippets Groups Projects

Issue #3524894: Circular reference error

@@ -7,7 +7,9 @@ namespace Drupal\mailer_override;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Mailer Override configuration override.
@@ -29,10 +31,15 @@ class MailerConfigOverride implements ConfigFactoryOverrideInterface {
/**
* Constructs the MailerConfigOverride object.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The container.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler.
*/
public function __construct(protected readonly ModuleHandlerInterface $moduleHandler) {}
public function __construct(
protected readonly ContainerInterface $container,
protected readonly ModuleHandlerInterface $moduleHandler,
) {}
/**
* {@inheritdoc}
@@ -67,15 +74,13 @@ class MailerConfigOverride implements ConfigFactoryOverrideInterface {
* Build cache of config overrides.
*/
protected function buildCache(): void {
if (!$this->builtCache && $this->moduleHandler->isLoaded()) {
// Getting the definitions can cause reading of config which triggers
// `loadOverrides()` to call this function. Protect against an infinite
// loop by marking the cache as built before starting.
// The calculation of config overrides depends on configuration settings,
// so we have to be careful to avoid a circular dependency.
// - Wait for key services we depend on to load, as they may read config.
// - Avoid dependency injection, instead load services when we use them.
// - Mark the cache as built before processing to ensure it only runs once.
if (!$this->builtCache && $this->moduleHandler->isLoaded() && $this->container->initialized(EntityTypeManagerInterface::class)) {
$this->builtCache = TRUE;
// We cannot use dependency injection because that creates a circular
// dependency.
/** @var \Drupal\mailer_override\OverrideManagerInterface $overrideManager */
$overrideManager = \Drupal::service(OverrideManagerInterface::class);
foreach ($overrideManager->getDefinitions() as $definition) {
$this->configOverrides = array_merge($this->configOverrides, $definition['config_overrides']);
Loading