Skip to content

Commit f0d120f

Browse files
committed
Add block_prefix option for an easy form theming
1 parent 32a53bf commit f0d120f

File tree

7 files changed

+30
-5
lines changed

7 files changed

+30
-5
lines changed

src/Symfony/Component/Form/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
7+
* added `block_prefix` option to `BaseType`.
8+
49
4.2.0
510
-----
611

src/Symfony/Component/Form/Extension/Core/Type/BaseType.php

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public function buildView(FormView $view, FormInterface $form, array $options)
7979
for ($type = $form->getConfig()->getType(); null !== $type; $type = $type->getParent()) {
8080
array_unshift($blockPrefixes, $type->getBlockPrefix());
8181
}
82+
if (null !== $options['block_prefix']) {
83+
$blockPrefixes[] = $options['block_prefix'];
84+
}
8285
$blockPrefixes[] = $uniqueBlockPrefix;
8386

8487
$view->vars = array_replace($view->vars, array(
@@ -111,6 +114,7 @@ public function configureOptions(OptionsResolver $resolver)
111114
{
112115
$resolver->setDefaults(array(
113116
'block_name' => null,
117+
'block_prefix' => null,
114118
'disabled' => false,
115119
'label' => null,
116120
'label_format' => null,

src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -639,4 +639,16 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
639639
{
640640
parent::testSubmitNull(array(), array(), array());
641641
}
642+
643+
public function testPassBlockPrefixToViewWithParent()
644+
{
645+
$view = $this->factory->createNamedBuilder('parent', static::TESTED_TYPE)
646+
->add('child', $this->getTestedType(), array(
647+
'block_prefix' => 'child',
648+
))
649+
->getForm()
650+
->createView();
651+
652+
$this->assertSame(array('form', 'child', '_parent_child'), $view['child']->vars['block_prefixes']);
653+
}
642654
}

src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_1.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"attr",
3333
"auto_initialize",
3434
"block_name",
35+
"block_prefix",
3536
"by_reference",
3637
"data",
3738
"disabled",

src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_1.txt

+6-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ Symfony\Component\Form\Extension\Core\Type\ChoiceType (Block prefix: "choice")
1212
choice_translation_domain empty_data attr csrf_protection
1313
choice_value error_bubbling auto_initialize csrf_token_id
1414
choices trim block_name csrf_token_manager
15-
expanded by_reference
16-
group_by data
17-
multiple disabled
18-
placeholder help
19-
preferred_choices help_attr
15+
expanded block_prefix
16+
group_by by_reference
17+
multiple data
18+
placeholder disabled
19+
preferred_choices help
20+
help_attr
2021
inherit_data
2122
label
2223
label_attr

src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_2.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"attr",
99
"auto_initialize",
1010
"block_name",
11+
"block_prefix",
1112
"by_reference",
1213
"compound",
1314
"data",

src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Symfony\Component\Form\Extension\Core\Type\FormType (Block prefix: "form")
1010
attr
1111
auto_initialize
1212
block_name
13+
block_prefix
1314
by_reference
1415
compound
1516
data

0 commit comments

Comments
 (0)