8000 [Form] Added "collection_entry" block prefix to CollectionType entries · symfony/symfony@2ff1f88 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ff1f88

Browse files
committed
[Form] Added "collection_entry" block prefix to CollectionType entries
1 parent c650fe6 commit 2ff1f88

File tree

4 files changed

+107
-2
lines changed

4 files changed

+107
-2
lines changed

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
5.1.0
55
-----
66

7+
* Added `collection_entry` block prefix to `CollectionType` entries
78
* Added a `choice_filter` option to `ChoiceType`
89
* Added argument `callable|null $filter` to `ChoiceListFactoryInterface::createListFromChoices()` and `createListFromLoader()` - not defining them is deprecated.
910
* Added a `ChoiceList` facade to leverage explicit choice list caching based on options

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,32 @@ public function buildView(FormView $view, FormInterface $form, array $options)
7272
*/
7373
public function finishView(FormView $view, FormInterface $form, array $options)
7474
{
75-
if ($form->getConfig()->hasAttribute('prototype') && $view->vars['prototype']->vars['multipart']) {
76-
$view->vars['multipart'] = true;
75+
$prefixOffset = -1;
76+
// check if the entry type also defines a block prefix
77+
/** @var FormInterface $entry */
78+
foreach ($form as $entry) {
79+
if ($entry->getConfig()->getOption('block_prefix')) {
80+
--$prefixOffset;
81+
}
82+
83+
break;
84+
}
85+
86+
foreach ($view as $entryView) {
87+
array_splice($entryView->vars['block_prefixes'], $prefixOffset, 0, 'collection_entry');
88+
}
89+
90+
/** @var FormInterface $prototype */
91+
if ($prototype = $form->getConfig()->getAttribute('prototype')) {
92+
if ($view->vars['prototype']->vars['multipart']) {
93+
$view->vars['multipart'] = true;
94+
}
95+
96+
if ($prefixOffset > -2 && $prototype->getConfig()->getOption('block_prefix')) {
97+
--$prefixOffset;
98+
}
99+
100+
array_splice($view->vars['prototype']->vars['block_prefixes'], $prefixOffset, 0, 'collection_entry');
77101
}
78102
}
79103

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Form\Tests\Fixtures\Author;
1515
use Symfony\Component\Form\Tests\Fixtures\AuthorType;
16+
use Symfony\Component\Form\Tests\Fixtures\BlockPrefixedFooTextType;
1617

1718
class CollectionTypeTest extends BaseTypeTest
1819
{
@@ -404,6 +405,62 @@ public function testPrototypeNotOverrideRequiredByEntryOptionsInFavorOfParent()
404405
$this->assertFalse($child->createView()->vars['prototype']->vars['required'], '"Prototype" should not be required');
405406
}
406407

408+
public function testEntriesBlockPrefixes()
409+
{
410+
$collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [
411+
'allow_add' => true,
412+
])
413+
->createView()
414+
;
415+
416+
$expectedBlockPrefixes = [
417+
'form',
418+
'text',
419+
'collection_entry',
420+
'_fields_entry',
421+
];
422+
423+
$this->assertCount(1, $collectionView);
424+
$this->assertSame($expectedBlockPrefixes, $collectionView[0]->vars['block_prefixes']);
425+
$this->assertSame($expectedBlockPrefixes, $collectionView->vars['prototype']->vars['block_prefixes']);
426+
}
427+
428+
public function testEntriesBlockPrefixesWithCustomBlockPrefix()
429+
{
430+
$collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [
431+
'entry_options' => ['block_prefix' => 'field'],
432+
])
433+
->createView()
434+
;
435+
436+
$this->assertCount(1, $collectionView);
437+
$this->assertSame([
438+
'form',
439+
'text',
440+
'collection_entry',
441+
'field',
442+
'_fields_entry',
443+
], $collectionView[0]->vars['block_prefixes']);
444+
}
445+
446+
public function testEntriesBlockPrefixesWithCustomBlockPrefixedType()
447+
{
448+
$collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [
449+
'entry_type' => BlockPrefixedFooTextType::class,
450+
])
451+
->createView()
452+
;
453+
454+
$this->assertCount(1, $collectionView);
455+
$this->assertSame([
456+
'form',
457+
'block_prefixed_foo_text',
458+
'collection_entry',
459+
'foo',
460+
'_fields_entry',
461+
], $collectionView[0]->vars['block_prefixes']);
462+
}
463+
407464
public function testSubmitNull($expected = null, $norm = null, $view = null)
408465
{
409466
parent::testSubmitNull([], [], []);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Fixtures;
13+
14+
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\OptionsResolver\OptionsResolver;
16+
17+
class BlockPrefixedFooTextType extends AbstractType
18+
{
19+
public function configureOptions(OptionsResolver $resolver)
20+
{
21+
$resolver->setDefault('block_prefix', 'foo');
22+
}
23+
}

0 commit comments

Comments
 (0)
0