Platform: Solution: Using a parameter from parameter.yml in override.yml in my bundle

|
|
Next article
 »

This is compatible with eZ Publish 5.

Problem

How can I use a parameter from parameter.yml to specify an override match in override.yml within my bundle?

Solution

Parameters are not included in the ContainerBuilder object in prepend() method, only in the load() method.

You need to specifically set the parameters at the beginning of the prepend() method.

Example

class SiteExtension extends Extension implements PrependExtensionInterface
{
  public function prepend( ContainerBuilder $container )
  {
    // Get current environment
    $environment = $container->getParameterBag()->get('kernel.environment');
    // Generate path to parameter file
    $parameterFile = __DIR__ . '/../Resources/config/parameters_' . $environment . '.yml';
    if (!is_file($parameterFile)) {
      $parameterFile = __DIR__ . '/../Resources/config/parameters.yml';
    }
 
    // Loading bundle parameters, and set them in the container object to be used in other config
    $config = Yaml::parse($parameterFile);
    foreach ($config['parameters'] as $name => $value) {
    $container->setParameter($name, $value);
  }
 
  ...
  }
}

References