- Issue created by @gpietrzak
- First commit to issue fork.
The escape
function defined in Drupal\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumper file is changing all occurrences of the percent sign to a double percent, as stated in the Symfony documentation.
However, if we set a parameter that is an array, the abc%de
string from this array will be escaped to abc%%%%de
, and passed like this to the services.
1. Create a CompilerPass and register it:
namespace Drupal\my_module\Compiler;
use Drupal\Core\Site\Settings;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class MyModuleCompilerPass implements CompilerPassInterface {
public function process(ContainerBuilder $container): void {
$container->setParameter('my_module.configuration', [
'option1' => 'string',
'option2' => 'string%',
'option3' => 'string%%',
]);
}
}
namespace Drupal\my_module;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Drupal\my_module\Compiler\MyModuleCompilerPass;
class MyModuleServiceProvider extends ServiceProviderBase {
public function register(ContainerBuilder $container): void {
$container->addCompilerPass(new MyModuleCompilerPass());
}
}
2. Define a service that uses these parameters:
services:
my_module.my_service:
class: Drupal\my_module\MyService
arguments:
- '%my_module.configuration%'
class MyService {
public function __construct(protected readonly array $configuration) {
// Actual result:
// $configuration['option1'] = 'string'
// $configuration['option2'] = 'string%%%%'
// $configuration['option3'] = 'string%%%%%%%%'
// Expected result:
// $configuration['option1'] = 'string'
// $configuration['option2'] = 'string%'
// $configuration['option3'] = 'string%%'
}
This happens because the escape function is called twice on the same data.
In case of defining a string parameter (instead of the array), the value is properly escaped.
Active
11.0 π₯
Last updated