Support setting service parameters via environment variables

Created on 17 November 2021, over 2 years ago
Updated 29 November 2023, 7 months ago

Problem/Motivation

The symfony container allows to define service parameters from environment variables. (Symfony documentation on configuration and Environment Variable Processors)
However this does not work with Drupal.

Steps to reproduce

Add a service parameter like this:

parameters:
  twig.config:
    debug: '%env(bool:MY_TWIG_DEBUG_ENV_VARIABLE)%'

And observe the error:

In ParameterBag.php line 100:
                                                                                                                    
  The parameter "twig.config" has a dependency on a non-existent parameter "env(bool:MY_TWIG_DEBUG_ENV_VARIABLE)".  

Proposed resolution

Use the EnvPlaceholderParameterBag and make sure environment variables are replaced correctly.

Remaining tasks

User interface changes

none

API changes

Support for environment variables in service parameters.

Data model changes

none

Release notes snippet

tbd

📌 Task
Status

Needs work

Version

11.0 🔥

Component
Base  →

Last updated less than a minute ago

Created by

🇨🇭Switzerland bircher 🇨🇿

Live updates comments and jobs are added and updated live.
  • Needs change record

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇱Israel jkdev

    Just override the parameter from your own ServiceProvider.

    Instead of using symfony magic and replace the way the container is being built, use the drupal/symfony way to alter services/parameters in the container:
    https://www.drupal.org/docs/drupal-apis/services-and-dependency-injectio... →

    In essence what we did is to create file: my_module/src/MyModuleServiceProvider.php (the name matters) looks like this:

    
    namespace Drupal\my_module;
    
    use Drupal\Core\DependencyInjection\ContainerBuilder;
    use Drupal\Core\DependencyInjection\ServiceProviderBase;
    
    class MyModuleServiceProvider extends ServiceProviderBase {
      /**
       * {@inheritdoc}
       */
      public function alter(ContainerBuilder $container) {
        $cors_config = $container->getParameter('cors.config');
        $cors_config['allowedOrigins'] = ['https://' . $_ENV['CONSUMER_HOST']];
        $container->setParameter('cors.config', $cors_config);
      }
    }
    
    

    Make sure clear cache (so container will be compiled fresh) - drush cr / drush cc container

  • First commit to issue fork.
  • @bircher
    I added some basic test coverage for resolving the env variable at compile time.

  • I'm also playing around with the idea to support using environment variables during runtime, see the --runtime branch.
    This is clearly a bit more involved, and I think belongs really into its own issue in the first place.

  • 🇧🇪Belgium DieterHolvoet Brussels

    @jkdev by doing it that way the values from environment variables are cached in the container, which means you need to rebuild caches anytime an environment variable is changed. I don't think we want that.

    @duadua looks like you haven't pushed anything yet to the issue-3249970--runtime and 3249970-support-setting-service branches. I'm not sure why, but it contains some commits from other issues. Only issue-3249970 contains relevant commits.

Production build 0.69.0 2024