Support setting service parameters via environment variables

Created on 17 November 2021, about 3 years ago
Updated 2 March 2023, almost 2 years 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

10.1 ✨

Component
Base  →

Last updated 1 day 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

Merge Requests

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.

  • 🇧🇪Belgium mr.baileys 🇧🇪 (Ghent)

    mr.baileys → changed the visibility of the branch issue-3249970--runtime to hidden.

  • 🇧🇪Belgium mr.baileys 🇧🇪 (Ghent)

    mr.baileys → changed the visibility of the branch 3249970-support-setting-service to hidden.

  • Pipeline finished with Failed
    3 months ago
    Total: 156s
    #294064
  • Pipeline finished with Failed
    3 months ago
    Total: 552s
    #294094
  • Pipeline finished with Failed
    3 months ago
    Total: 390s
    #294149
  • Pipeline finished with Failed
    3 months ago
    Total: 409s
    #294208
  • Pipeline finished with Failed
    3 months ago
    Total: 366s
    #294322
  • 🇧🇪Belgium mr.baileys 🇧🇪 (Ghent)
    • Created a new branch issue-3249970-11x, cherry-picked the commits from @duadua and brought it up to date with 11.x;
    • Fixed PHPCS violations;
    • Tested with setting an environment variable via ddev/config.yaml, verifying it is used after this patch is applied;
  • The Needs Review Queue Bot → tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide → to find step-by-step guides for working with issues.

Production build 0.71.5 2024