2

I'm trying to expose the semantic configuration within a bundle in Symfony 2.0 but I'm having trouble getting the defaultValue to work in the NodeDefinition class. I generated an empty bundle and created the necessary files for the Configuration to work and I am able to get the config value, but I want to set a defaultValue to configuration item. I use the defaultValue() method and remove the configuration item from my config.yml and then it shows up an empty array? Can anyone explain how the defaultValue() actually works, am I missing something?

<?php
// ./DependencyInjection/Configuration.php

namespace Test\Bundle\TestBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('test_bundle');

        $rootNode
            ->children()
                ->scalarNode('foo')->defaultValue('bar')->end()
            ->end();

        return $treeBuilder;
    }
}

-

<?php
// ./DependencyInjection/TestBundleExtension.php

namespace Test\Bundle\TestBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

class TestBundleExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        var_dump($configs); // empty array

        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.xml');
    }
}

So from the above Configuration class surely when the config item 'test_bundle.foo' is missing, it's value will be set to 'bar'... yes? Well that's what I thought but it doesn't.

1 Answer 1

5

Your root node is an array node. By default, if you dont set a key, default value is not applied. 2 examples on how to get your default value working:

  1. Set the value as null:

    # in the config.yml
    test_bundle:
        foo: ~
    
  2. Tell your root node to use default values if not set:

    // in your Configuration.php
    $rootNode
        ->addDefaultsIfNotSet()
        ->children()
            ->scalarNode('foo')->defaultValue('bar')->end()
        ->end();
    
    # in the config.yml
    test_bundle: ~
    
2
  • Example #1 does not work, it treat null as it's value. However example #2 worked, so if you are missing the key the Configuration will add it and set it a defaultValue. Thanks JF Simon!!
    – Chris
    May 5, 2012 at 14:36
  • 1
    For example #1 to work you can use treatNullLike, found in the API docs.
    – Chris
    May 5, 2012 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.