Skip to content

Commit

Permalink
feature #11343 [Twig][Form] Moved twig.form.resources to a higher lev…
Browse files Browse the repository at this point in the history
…el (stefanosala)

This PR was merged into the 2.6-dev branch.

Discussion
----------

[Twig][Form] Moved twig.form.resources to a higher level

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #11296
| License       | MIT
| Doc PR        | symfony/symfony-docs#4003

Commits
-------

ab0b5e6 [Twig][Form] Moved configuration key twig.form.resources to twig.form_themes
  • Loading branch information
webmozart committed Sep 15, 2014
2 parents 05c7207 + ab0b5e6 commit 6ebb017
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 5 deletions.
Expand Up @@ -39,6 +39,7 @@ public function getConfigTreeBuilder()
;

$this->addFormSection($rootNode);
$this->addFormThemesSection($rootNode);
$this->addGlobalsSection($rootNode);
$this->addTwigOptions($rootNode);

Expand All @@ -48,8 +49,19 @@ public function getConfigTreeBuilder()
private function addFormSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->validate()
->ifTrue(function ($v) {
return count($v['form']['resources']) > 0;
})
->then(function ($v) {
$v['form_themes'] = array_unique(array_merge($v['form']['resources'], $v['form_themes']));

return $v;
})
->end()
->children()
->arrayNode('form')
->info('Deprecated since 2.6, to be removed in 3.0. Use twig.form_themes instead')
->addDefaultsIfNotSet()
->fixXmlConfig('resource')
->children()
Expand All @@ -70,6 +82,26 @@ private function addFormSection(ArrayNodeDefinition $rootNode)
;
}

private function addFormThemesSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->fixXmlConfig('form_theme')
->children()
->arrayNode('form_themes')
->addDefaultChildrenIfNoneSet()
->prototype('scalar')->defaultValue('form_div_layout.html.twig')->end()
->example(array('MyBundle::form.html.twig'))
->validate()
->ifTrue(function ($v) { return !in_array('form_div_layout.html.twig', $v); })
->then(function ($v) {
return array_merge(array('form_div_layout.html.twig'), $v);
})
->end()
->end()
->end()
;
}

private function addGlobalsSection(ArrayNodeDefinition $rootNode)
{
$rootNode
Expand Down
Expand Up @@ -55,7 +55,7 @@ public function load(array $configs, ContainerBuilder $container)

$container->setParameter('twig.exception_listener.controller', $config['exception_controller']);

$container->setParameter('twig.form.resources', $config['form']['resources']);
$container->setParameter('twig.form.resources', $config['form_themes']);

$twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.filesystem');

Expand Down
Expand Up @@ -9,7 +9,9 @@

<xsd:complexType name="config">
<xsd:sequence>
<!-- @deprecated since 2.6, to be removed in 3.0 -->
<xsd:element name="form" type="form" minOccurs="0" maxOccurs="1" />
<xsd:element name="form-theme" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="global" type="global" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="path" type="path" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
Expand Down
Expand Up @@ -3,9 +3,12 @@
$container->loadFromExtension('twig', array(
'form' => array(
'resources' => array(
'MyBundle::form.html.twig',
'MyBundle::formDeprecated.html.twig',
)
),
'form_themes' => array(
'MyBundle::form.html.twig'
),
'globals' => array(
'foo' => '@bar',
'baz' => '@@qux',
Expand All @@ -24,5 +27,5 @@
'path2',
'namespaced_path1' => 'namespace1',
'namespaced_path2' => 'namespace2',
),
),
));
Expand Up @@ -8,8 +8,9 @@

<twig:config auto-reload="true" autoescape="true" base-template-class="stdClass" cache="/tmp" charset="ISO-8859-1" debug="true" strict-variables="true">
<twig:form>
<twig:resource>MyBundle::form.html.twig</twig:resource>
<twig:resource>MyBundle::formDeprecated.html.twig</twig:resource>
</twig:form>
<twig:form-theme>MyBundle::form.html.twig</twig:form-theme>
<twig:global key="foo" id="bar" type="service" />
<twig:global key="baz">@@qux</twig:global>
<twig:global key="pi">3.14</twig:global>
Expand Down
@@ -1,7 +1,9 @@
twig:
form_themes:
- MyBundle::form.html.twig
form:
resources:
- MyBundle::form.html.twig
- MyBundle::formDeprecated.html.twig
globals:
foo: "@bar"
baz: "@@qux"
Expand Down
Expand Up @@ -31,6 +31,7 @@ public function testLoadEmptyConfiguration()
$this->compileContainer($container);

$this->assertEquals('Twig_Environment', $container->getParameter('twig.class'), '->load() loads the twig.xml file');

$this->assertContains('form_div_layout.html.twig', $container->getParameter('twig.form.resources'), '->load() includes default template for form resources');

// Twig options
Expand All @@ -56,6 +57,8 @@ public function testLoadFullConfiguration($format)
$resources = $container->getParameter('twig.form.resources');
$this->assertContains('form_div_layout.html.twig', $resources, '->load() includes default template for form resources');
$this->assertContains('MyBundle::form.html.twig', $resources, '->load() merges new templates into form resources');
// @deprecated since 2.6, to be removed in 3.0
$this->assertContains('MyBundle::formDeprecated.html.twig', $resources, '->load() merges new templates into form resources');

// Globals
$calls = $container->getDefinition('twig')->getMethodCalls();
Expand Down

0 comments on commit 6ebb017

Please sign in to comment.