Skip to content

Commit 3f03463

Browse files
committed
feature #11343 [Twig][Form] Moved twig.form.resources to a higher level (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
2 parents bd66165 + b19a2e4 commit 3f03463

File tree

7 files changed

+48
-5
lines changed

7 files changed

+48
-5
lines changed

DependencyInjection/Configuration.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function getConfigTreeBuilder()
3939
;
4040

4141
$this->addFormSection($rootNode);
42+
$this->addFormThemesSection($rootNode);
4243
$this->addGlobalsSection($rootNode);
4344
$this->addTwigOptions($rootNode);
4445

@@ -48,8 +49,19 @@ public function getConfigTreeBuilder()
4849
private function addFormSection(ArrayNodeDefinition $rootNode)
4950
{
5051
$rootNode
52+
->validate()
53+
->ifTrue(function ($v) {
54+
return count($v['form']['resources']) > 0;
55+
})
56+
->then(function ($v) {
57+
$v['form_themes'] = array_unique(array_merge($v['form']['resources'], $v['form_themes']));
58+
59+
return $v;
60+
})
61+
->end()
5162
->children()
5263
->arrayNode('form')
64+
->info('Deprecated since 2.6, to be removed in 3.0. Use twig.form_themes instead')
5365
->addDefaultsIfNotSet()
5466
->fixXmlConfig('resource')
5567
->children()
@@ -70,6 +82,26 @@ private function addFormSection(ArrayNodeDefinition $rootNode)
7082
;
7183
}
7284

85+
private function addFormThemesSection(ArrayNodeDefinition $rootNode)
86+
{
87+
$rootNode
88+
->fixXmlConfig('form_theme')
89+
->children()
90+
->arrayNode('form_themes')
91+
->addDefaultChildrenIfNoneSet()
92+
->prototype('scalar')->defaultValue('form_div_layout.html.twig')->end()
93+
->example(array('MyBundle::form.html.twig'))
94+
->validate()
95+
->ifTrue(function ($v) { return !in_array('form_div_layout.html.twig', $v); })
96+
->then(function ($v) {
97+
return array_merge(array('form_div_layout.html.twig'), $v);
98+
})
99+
->end()
100+
->end()
101+
->end()
102+
;
103+
}
104+
73105
private function addGlobalsSection(ArrayNodeDefinition $rootNode)
74106
{
75107
$rootNode

DependencyInjection/TwigExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function load(array $configs, ContainerBuilder $container)
5555

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

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

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

Resources/config/schema/twig-1.0.xsd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
<xsd:complexType name="config">
1111
<xsd:sequence>
12+
<!-- @deprecated since 2.6, to be removed in 3.0 -->
1213
<xsd:element name="form" type="form" minOccurs="0" maxOccurs="1" />
14+
<xsd:element name="form-theme" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
1315
<xsd:element name="global" type="global" minOccurs="0" maxOccurs="unbounded" />
1416
<xsd:element name="path" type="path" minOccurs="0" maxOccurs="unbounded" />
1517
</xsd:sequence>

Tests/DependencyInjection/Fixtures/php/full.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
$container->loadFromExtension('twig', array(
44
'form' => array(
55
'resources' => array(
6-
'MyBundle::form.html.twig',
6+
'MyBundle::formDeprecated.html.twig',
77
)
88
),
9+
'form_themes' => array(
10+
'MyBundle::form.html.twig'
11+
),
912
'globals' => array(
1013
'foo' => '@bar',
1114
'baz' => '@@qux',
@@ -24,5 +27,5 @@
2427
'path2',
2528
'namespaced_path1' => 'namespace1',
2629
'namespaced_path2' => 'namespace2',
27-
),
30+
),
2831
));

Tests/DependencyInjection/Fixtures/xml/full.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
<twig:config auto-reload="true" autoescape="true" base-template-class="stdClass" cache="/tmp" charset="ISO-8859-1" debug="true" strict-variables="true">
1010
<twig:form>
11-
<twig:resource>MyBundle::form.html.twig</twig:resource>
11+
<twig:resource>MyBundle::formDeprecated.html.twig</twig:resource>
1212
</twig:form>
13+
<twig:form-theme>MyBundle::form.html.twig</twig:form-theme>
1314
<twig:global key="foo" id="bar" type="service" />
1415
<twig:global key="baz">@@qux</twig:global>
1516
<twig:global key="pi">3.14</twig:global>

Tests/DependencyInjection/Fixtures/yml/full.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
twig:
2+
form_themes:
3+
- MyBundle::form.html.twig
24
form:
35
resources:
4-
- MyBundle::form.html.twig
6+
- MyBundle::formDeprecated.html.twig
57
globals:
68
foo: "@bar"
79
baz: "@@qux"

Tests/DependencyInjection/TwigExtensionTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function testLoadEmptyConfiguration()
3131
$this->compileContainer($container);
3232

3333
$this->assertEquals('Twig_Environment', $container->getParameter('twig.class'), '->load() loads the twig.xml file');
34+
3435
$this->assertContains('form_div_layout.html.twig', $container->getParameter('twig.form.resources'), '->load() includes default template for form resources');
3536

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

6063
// Globals
6164
$calls = $container->getDefinition('twig')->getMethodCalls();

0 commit comments

Comments
 (0)