Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a6aa9ec

Browse filesBrowse files
committed
feature #22610 [Form] [TwigBridge] Added option to disable usage of default themes when rendering a form (emodric)
This PR was merged into the 3.4 branch. Discussion ---------- [Form] [TwigBridge] Added option to disable usage of default themes when rendering a form | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | N/A | License | MIT | Doc PR | symfony/symfony-docs#8495 This adds a possibility to use `only` keyword in `form_theme` tag to disable usage of globally defined form themes, e.g. `{% form_theme form with ['common.html.twig', 'form/fields.html.twig'] only %}` Otherwise, in order to completely control the rendering of the forms (for example in custom admin interfaces), one would need to use a form theme which has all the possible twig blocks defined to prevent globally defined themes to interfere with the rendering. `only` keyword is already used when including a Twig template to transfer only the variables which are explicitly defined in the `include` tag, so it seemed natural to use it here too. This, of course, means that the user will need to manually `use` all of the templates that are required to render the form, including `form_div_layout.html.twig` This issue is described in details over at Symfony Demo repo: symfony/demo#515 TODO: - [x] submit changes to the documentation Commits ------- e0681f9 [Form] [TwigBridge] Added option to disable usage of default themes when rendering a form
2 parents 3f0a3f5 + e0681f9 commit a6aa9ec
Copy full SHA for a6aa9ec

22 files changed

+107
-43
lines changed

‎src/Symfony/Bridge/Twig/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
3.4.0
55
-----
66

7+
* added an `only` keyword to `form_theme` tag to disable usage of default themes when rendering a form
78
* deprecated `Symfony\Bridge\Twig\Form\TwigRenderer`
89
* deprecated `DebugCommand::set/getTwigEnvironment`. Pass an instance of
910
`Twig\Environment` as first argument of the constructor instead

‎src/Symfony/Bridge/Twig/Form/TwigRendererEngine.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Form/TwigRendererEngine.php
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,11 @@ protected function loadResourceForBlockName($cacheKey, FormView $view, $blockNam
124124

125125
// Check the default themes once we reach the root view without success
126126
if (!$view->parent) {
127-
for ($i = count($this->defaultThemes) - 1; $i >= 0; --$i) {
128-
$this->loadResourcesFromTheme($cacheKey, $this->defaultThemes[$i]);
129-
// CONTINUE LOADING (see doc comment)
127+
if (!isset($this->useDefaultThemes[$cacheKey]) || $this->useDefaultThemes[$cacheKey]) {
128+
for ($i = count($this->defaultThemes) - 1; $i >= 0; --$i) {
129+
$this->loadResourcesFromTheme($cacheKey, $this->defaultThemes[$i]);
130+
// CONTINUE LOADING (see doc comment)
131+
}
130132
}
131133
}
132134

‎src/Symfony/Bridge/Twig/Node/FormThemeNode.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Node/FormThemeNode.php
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
*/
2323
class FormThemeNode extends Node
2424
{
25-
public function __construct(Node $form, Node $resources, $lineno, $tag = null)
25+
public function __construct(Node $form, Node $resources, $lineno, $tag = null, $only = false)
2626
{
27-
parent::__construct(array('form' => $form, 'resources' => $resources), array(), $lineno, $tag);
27+
parent::__construct(array('form' => $form, 'resources' => $resources), array('only' => (bool) $only), $lineno, $tag);
2828
}
2929

3030
public function compile(Compiler $compiler)
@@ -44,6 +44,8 @@ public function compile(Compiler $compiler)
4444
->subcompile($this->getNode('form'))
4545
->raw(', ')
4646
->subcompile($this->getNode('resources'))
47+
->raw(', ')
48+
->raw(false === $this->getAttribute('only') ? 'true' : 'false')
4749
->raw(");\n");
4850
}
4951
}

‎src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3HorizontalLayoutTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3HorizontalLayoutTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ protected function renderEnd(FormView $view, array $vars = array())
9999
return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
100100
}
101101

102-
protected function setTheme(FormView $view, array $themes)
102+
protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
103103
{
104-
$this->renderer->setTheme($view, $themes);
104+
$this->renderer->setTheme($view, $themes, $useDefaultThemes);
105105
}
106106
}

‎src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ protected function renderEnd(FormView $view, array $vars = array())
119119
return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
120120
}
121121

122-
protected function setTheme(FormView $view, array $themes)
122+
protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
123123
{
124-
$this->renderer->setTheme($view, $themes);
124+
$this->renderer->setTheme($view, $themes, $useDefaultThemes);
125125
}
126126
}

‎src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap4HorizontalLayoutTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap4HorizontalLayoutTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ protected function renderEnd(FormView $view, array $vars = array())
100100
return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
101101
}
102102

103-
protected function setTheme(FormView $view, array $themes)
103+
protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
104104
{
105-
$this->renderer->setTheme($view, $themes);
105+
$this->renderer->setTheme($view, $themes, $useDefaultThemes);
106106
}
107107
}

‎src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap4LayoutTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap4LayoutTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ protected function renderEnd(FormView $view, array $vars = array())
122122
return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
123123
}
124124

125-
protected function setTheme(FormView $view, array $themes)
125+
protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
126126
{
127-
$this->renderer->setTheme($view, $themes);
127+
$this->renderer->setTheme($view, $themes, $useDefaultThemes);
128128
}
129129
}

‎src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ protected function renderEnd(FormView $view, array $vars = array())
193193
return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
194194
}
195195

196-
protected function setTheme(FormView $view, array $themes)
196+
protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
197197
{
198-
$this->renderer->setTheme($view, $themes);
198+
$this->renderer->setTheme($view, $themes, $useDefaultThemes);
199199
}
200200

201201
public static function themeBlockInheritanceProvider()

‎src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionTableLayoutTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionTableLayoutTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ protected function renderEnd(FormView $view, array $vars = array())
120120
return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
121121
}
122122

123-
protected function setTheme(FormView $view, array $themes)
123+
protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
124124
{
125-
$this->renderer->setTheme($view, $themes);
125+
$this->renderer->setTheme($view, $themes, $useDefaultThemes);
126126
}
127127
}

‎src/Symfony/Bridge/Twig/Tests/Node/FormThemeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/Node/FormThemeTest.php
+23-2Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function testConstructor()
3939

4040
$this->assertEquals($form, $node->getNode('form'));
4141
$this->assertEquals($resources, $node->getNode('resources'));
42+
$this->assertFalse($node->getAttribute('only'));
4243
}
4344

4445
public function testCompile()
@@ -60,7 +61,17 @@ public function testCompile()
6061

6162
$this->assertEquals(
6263
sprintf(
63-
'$this->env->getRuntime("Symfony\\\\Component\\\\Form\\\\FormRenderer")->setTheme(%s, array(0 => "tpl1", 1 => "tpl2"));',
64+
'$this->env->getRuntime("Symfony\\\\Component\\\\Form\\\\FormRenderer")->setTheme(%s, array(0 => "tpl1", 1 => "tpl2"), true);',
65+
$this->getVariableGetter('form')
66+
),
67+
trim($compiler->compile($node)->getSource())
68+
);
69+
70+
$node = new FormThemeNode($form, $resources, 0, null, true);
71+
72+
$this->assertEquals(
73+
sprintf(
74+
'$this->env->getRuntime("Symfony\\\\Component\\\\Form\\\\FormRenderer")->setTheme(%s, array(0 => "tpl1", 1 => "tpl2"), false);',
6475
$this->getVariableGetter('form')
6576
),
6677
trim($compiler->compile($node)->getSource())
@@ -72,7 +83,17 @@ public function testCompile()
7283

7384
$this->assertEquals(
7485
sprintf(
75-
'$this->env->getRuntime("Symfony\\\\Component\\\\Form\\\\FormRenderer")->setTheme(%s, "tpl1");',
86+
'$this->env->getRuntime("Symfony\\\\Component\\\\Form\\\\FormRenderer")->setTheme(%s, "tpl1", true);',
87+
$this->getVariableGetter('form')
88+
),
89+
trim($compiler->compile($node)->getSource())
90+
);
91+
92+
$node = new FormThemeNode($form, $resources, 0, null, true);
93+
94+
$this->assertEquals(
95+
sprintf(
96+
'$this->env->getRuntime("Symfony\\\\Component\\\\Form\\\\FormRenderer")->setTheme(%s, "tpl1", false);',
7697
$this->getVariableGetter('form')
7798
),
7899
trim($compiler->compile($node)->getSource())

‎src/Symfony/Bridge/Twig/Tests/TokenParser/FormThemeTokenParserTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/TokenParser/FormThemeTokenParserTest.php
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ public function getTestsForFormTheme()
100100
'form_theme'
101101
),
102102
),
103+
array(
104+
'{% form_theme form with ["tpl1", "tpl2"] only %}',
105+
new FormThemeNode(
106+
new NameExpression('form', 1),
107+
new ArrayExpression(array(
108+
new ConstantExpression(0, 1),
109+
new ConstantExpression('tpl1', 1),
110+
new ConstantExpression(1, 1),
111+
new ConstantExpression('tpl2', 1),
112+
), 1),
113+
1,
114+
'form_theme',
115+
true
116+
),
117+
),
103118
);
104119
}
105120
}

‎src/Symfony/Bridge/Twig/TokenParser/FormThemeTokenParser.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/TokenParser/FormThemeTokenParser.php
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ public function parse(Token $token)
3737
$stream = $this->parser->getStream();
3838

3939
$form = $this->parser->getExpressionParser()->parseExpression();
40+
$only = false;
4041

4142
if ($this->parser->getStream()->test(Token::NAME_TYPE, 'with')) {
4243
$this->parser->getStream()->next();
4344
$resources = $this->parser->getExpressionParser()->parseExpression();
45+
46+
if ($this->parser->getStream()->nextIf(Token::NAME_TYPE, 'only')) {
47+
$only = true;
48+
}
4449
} else {
4550
$resources = new ArrayExpression(array(), $stream->getCurrent()->getLine());
4651
do {
@@ -50,7 +55,7 @@ public function parse(Token $token)
5055

5156
$stream->expect(Token::BLOCK_END_TYPE);
5257

53-
return new FormThemeNode($form, $resources, $lineno, $this->getTag());
58+
return new FormThemeNode($form, $resources, $lineno, $this->getTag(), $only);
5459
}
5560

5661
/**

‎src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php
+5-4Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ public function getName()
4949
*
5050
* The theme format is "<Bundle>:<Controller>".
5151
*
52-
* @param FormView $view A FormView instance
53-
* @param string|array $themes A theme or an array of theme
52+
* @param FormView $view A FormView instance
53+
* @param string|array $themes A theme or an array of theme
54+
* @param bool $useDefaultThemes If true, will use default themes defined in the renderer
5455
*/
55-
public function setTheme(FormView $view, $themes)
56+
public function setTheme(FormView $view, $themes, $useDefaultThemes = true)
5657
{
57-
$this->renderer->setTheme($view, $themes);
58+
$this->renderer->setTheme($view, $themes, $useDefaultThemes);
5859
}
5960

6061
/**

‎src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ protected function renderEnd(FormView $view, array $vars = array())
121121
return (string) $this->engine->get('form')->end($view, $vars);
122122
}
123123

124-
protected function setTheme(FormView $view, array $themes)
124+
protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
125125
{
126-
$this->engine->get('form')->setTheme($view, $themes);
126+
$this->engine->get('form')->setTheme($view, $themes, $useDefaultThemes);
127127
}
128128

129129
public static function themeBlockInheritanceProvider()

‎src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTableLayoutTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTableLayoutTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ protected function renderEnd(FormView $view, array $vars = array())
122122
return (string) $this->engine->get('form')->end($view, $vars);
123123
}
124124

125-
protected function setTheme(FormView $view, array $themes)
125+
protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
126126
{
127-
$this->engine->get('form')->setTheme($view, $themes);
127+
$this->engine->get('form')->setTheme($view, $themes, $useDefaultThemes);
128128
}
129129
}

‎src/Symfony/Component/Form/AbstractRendererEngine.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/AbstractRendererEngine.php
+9-1Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ abstract class AbstractRendererEngine implements FormRendererEngineInterface
3333
*/
3434
protected $themes = array();
3535

36+
/**
37+
* @var array
38+
*/
39+
protected $useDefaultThemes = array();
40+
3641
/**
3742
* @var array
3843
*/
@@ -57,13 +62,16 @@ public function __construct(array $defaultThemes = array())
5762
/**
5863
* {@inheritdoc}
5964
*/
60-
public function setTheme(FormView $view, $themes)
65+
public function setTheme(FormView $view, $themes /*, $useDefaultThemes = true */)
6166
{
6267
$cacheKey = $view->vars[self::CACHE_KEY_VAR];
6368

6469
// Do not cast, as casting turns objects into arrays of properties
6570
$this->themes[$cacheKey] = is_array($themes) ? $themes : array($themes);
6671

72+
$args = func_get_args();
73+
$this->useDefaultThemes[$cacheKey] = isset($args[2]) ? (bool) $args[2] : true;
74+
6775
// Unset instead of resetting to an empty array, in order to allow
6876
// implementations (like TwigRendererEngine) to check whether $cacheKey
6977
// is set at all.

‎src/Symfony/Component/Form/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ CHANGELOG
77
* added `DebugCommand`
88
* deprecated `ChoiceLoaderInterface` implementation in `TimezoneType`
99
* added options "input" and "regions" to `TimezoneType`
10+
* added an option to ``Symfony\Component\Form\FormRendererEngineInterface::setTheme()`` and
11+
``Symfony\Component\Form\FormRendererInterface::setTheme()`` to disable usage of default themes when rendering a form
1012

1113
3.3.0
1214
-----

‎src/Symfony/Component/Form/Extension/Templating/TemplatingRendererEngine.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Templating/TemplatingRendererEngine.php
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ protected function loadResourceForBlockName($cacheKey, FormView $view, $blockNam
7272

7373
// Check the default themes once we reach the root form without success
7474
if (!$view->parent) {
75-
for ($i = count($this->defaultThemes) - 1; $i >= 0; --$i) {
76-
if ($this->loadResourceFromTheme($cacheKey, $blockName, $this->defaultThemes[$i])) {
77-
return true;
75+
if (!isset($this->useDefaultThemes[$cacheKey]) || $this->useDefaultThemes[$cacheKey]) {
76+
for ($i = count($this->defaultThemes) - 1; $i >= 0; --$i) {
77+
if ($this->loadResourceFromTheme($cacheKey, $blockName, $this->defaultThemes[$i])) {
78+
return true;
79+
}
7880
}
7981
}
8082
}

‎src/Symfony/Component/Form/FormRenderer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/FormRenderer.php
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ public function getEngine()
7070
/**
7171
* {@inheritdoc}
7272
*/
73-
public function setTheme(FormView $view, $themes)
73+
public function setTheme(FormView $view, $themes /*, $useDefaultThemes = true */)
7474
{
75-
$this->engine->setTheme($view, $themes);
75+
$args = func_get_args();
76+
$this->engine->setTheme($view, $themes, isset($args[2]) ? (bool) $args[2] : true);
7677
}
7778

7879
/**

‎src/Symfony/Component/Form/FormRendererEngineInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/FormRendererEngineInterface.php
+6-4Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ interface FormRendererEngineInterface
2121
/**
2222
* Sets the theme(s) to be used for rendering a view and its children.
2323
*
24-
* @param FormView $view The view to assign the theme(s) to
25-
* @param mixed $themes The theme(s). The type of these themes
26-
* is open to the implementation.
24+
* @param FormView $view The view to assign the theme(s) to
25+
* @param mixed $themes The theme(s). The type of these themes
26+
* is open to the implementation.
27+
* @param bool $useDefaultThemes If true, will use default themes specified
28+
* in the engine, will be added to the interface in 4.0
2729
*/
28-
public function setTheme(FormView $view, $themes);
30+
public function setTheme(FormView $view, $themes /*, $useDefaultThemes = true */);
2931

3032
/**
3133
* Returns the resource for a block name.

‎src/Symfony/Component/Form/FormRendererInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/FormRendererInterface.php
+6-4Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ public function getEngine();
2828
/**
2929
* Sets the theme(s) to be used for rendering a view and its children.
3030
*
31-
* @param FormView $view The view to assign the theme(s) to
32-
* @param mixed $themes The theme(s). The type of these themes
33-
* is open to the implementation.
31+
* @param FormView $view The view to assign the theme(s) to
32+
* @param mixed $themes The theme(s). The type of these themes
33+
* is open to the implementation.
34+
* @param bool $useDefaultThemes If true, will use default themes specified
35+
* in the renderer, will be added to the interface in 4.0
3436
*/
35-
public function setTheme(FormView $view, $themes);
37+
public function setTheme(FormView $view, $themes /*, $useDefaultThemes = true */);
3638

3739
/**
3840
* Renders a named block of the form theme.

‎src/Symfony/Component/Form/Tests/AbstractLayoutTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ abstract protected function renderStart(FormView $view, array $vars = array());
125125

126126
abstract protected function renderEnd(FormView $view, array $vars = array());
127127

128-
abstract protected function setTheme(FormView $view, array $themes);
128+
abstract protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true);
129129

130130
public function testLabel()
131131
{

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.