feature #29680 [Form] Add new block_prefix option for an easy form theming (yceruto)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[Form] Add new block_prefix option for an easy form theming

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #29651
| License       | MIT
| Doc PR        | https://github.com/symfony/symfony-docs/pull/10835

Commits
-------

bd50ac44fc Add block_prefix option for an easy form theming
This commit is contained in:
Fabien Potencier 2019-01-03 16:01:11 +01:00
commit b948d5a987
7 changed files with 31 additions and 5 deletions

View File

@ -1,6 +1,11 @@
CHANGELOG
=========
4.3.0
-----
* added `block_prefix` option to `BaseType`.
4.2.0
-----

View File

@ -79,6 +79,9 @@ abstract class BaseType extends AbstractType
for ($type = $form->getConfig()->getType(); null !== $type; $type = $type->getParent()) {
array_unshift($blockPrefixes, $type->getBlockPrefix());
}
if (null !== $options['block_prefix']) {
$blockPrefixes[] = $options['block_prefix'];
}
$blockPrefixes[] = $uniqueBlockPrefix;
$view->vars = array_replace($view->vars, array(
@ -111,6 +114,7 @@ abstract class BaseType extends AbstractType
{
$resolver->setDefaults(array(
'block_name' => null,
'block_prefix' => null,
'disabled' => false,
'label' => null,
'label_format' => null,
@ -119,6 +123,7 @@ abstract class BaseType extends AbstractType
'auto_initialize' => true,
));
$resolver->setAllowedTypes('block_prefix', array('null', 'string'));
$resolver->setAllowedTypes('attr', 'array');
}
}

View File

@ -639,4 +639,16 @@ class FormTypeTest extends BaseTypeTest
{
parent::testSubmitNull(array(), array(), array());
}
public function testPassBlockPrefixToViewWithParent()
{
$view = $this->factory->createNamedBuilder('parent', static::TESTED_TYPE)
->add('child', $this->getTestedType(), array(
'block_prefix' => 'child',
))
->getForm()
->createView();
$this->assertSame(array('form', 'child', '_parent_child'), $view['child']->vars['block_prefixes']);
}
}

View File

@ -32,6 +32,7 @@
"attr",
"auto_initialize",
"block_name",
"block_prefix",
"by_reference",
"data",
"disabled",

View File

@ -12,11 +12,12 @@ Symfony\Component\Form\Extension\Core\Type\ChoiceType (Block prefix: "choice")
choice_translation_domain empty_data attr csrf_protection
choice_value error_bubbling auto_initialize csrf_token_id
choices trim block_name csrf_token_manager
expanded by_reference
group_by data
multiple disabled
placeholder help
preferred_choices help_attr
expanded block_prefix
group_by by_reference
multiple data
placeholder disabled
preferred_choices help
help_attr
inherit_data
label
label_attr

View File

@ -8,6 +8,7 @@
"attr",
"auto_initialize",
"block_name",
"block_prefix",
"by_reference",
"compound",
"data",

View File

@ -10,6 +10,7 @@ Symfony\Component\Form\Extension\Core\Type\FormType (Block prefix: "form")
attr
auto_initialize
block_name
block_prefix
by_reference
compound
data