Skip to content

Commit

Permalink
Merge pull request #32 from tienvx/fix-render-accordion-item-body-in-…
Browse files Browse the repository at this point in the history
…symfony

Fix render accordion item body in symfony
  • Loading branch information
tienvx committed Oct 16, 2021
2 parents cff7e30 + 597f71e commit 295cf68
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/.coveralls.yml export-ignore
/.php_cs export-ignore
/phpunit.xml.dist export-ignore
/images export-ignore
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

UX collection JS is a Symfony bundle providing Symfony UX integration for collection form type with the help from [Symfony Collection JS](https://github.com/ruano-a/symfonyCollectionJs) library.

## Screenshots

![Screenshot Bootstrap 5](./images/collection-js-bootstrap-5.png)
![Screenshot EasyAdmin](./images/collection-js-easyadmin.png)

## Installation

UX Collection JS requires PHP 7.4+ and Symfony 4.4+.
Expand Down Expand Up @@ -100,7 +105,14 @@ class FormFieldReferenceController extends AbstractCrudController
{
yield CollectionField::new('collectionSimple', 'Collection Field (simple)')
->setFormType(CollectionJsType::class)
->setFormTypeOption('entry_type', CollectionSimpleType::class)
->setFormTypeOptions([
'entry_type' => CollectionSimpleType::class,
'allow_add' => true,
'allow_delete' => true,
'allow_move_up' => true,
'allow_move_down' => true,
'call_post_add_on_init' => true,
])
->addWebpackEncoreEntries('stimulus');
}
}
Expand Down
Binary file added images/collection-js-bootstrap-5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/collection-js-easyadmin.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 8 additions & 7 deletions src/Form/CollectionJsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CollectionJsType extends AbstractType
Expand All @@ -23,18 +24,18 @@ public function getParent(): string
*/
public function configureOptions(OptionsResolver $resolver): void
{
$entryOptionsNormalizer = function (Options $options, $value) {
$value['block_prefix'] = $value['block_prefix'] ?? 'collection_js_entry';

return $value;
};

$resolver->setDefaults([
'allow_move_up' => false,
'allow_move_down' => false,
'entry_options' => function (OptionsResolver $entryOptionsResolver) {
$entryOptionsResolver
->setDefaults([
'label' => false,
'block_prefix' => 'collection_js_entry',
]);
},
'call_post_add_on_init' => false,
]);
$resolver->setNormalizer('entry_options', $entryOptionsNormalizer);
}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/Resources/views/form_div_layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@
{% endblock collection_js_actions %}

{% block collection_js_accordion_item_body %}
{% for child in form|filter(child => not child.rendered) %}
{{- form_row(child) -}}
{% endfor %}
{% if form|length > 0 %}
{% for child in form|filter(child => not child.rendered) %}
{{- form_row(child) -}}
{% endfor %}
{% else %}
{{ form_widget(form) }}
{% endif %}
{% endblock collection_js_accordion_item_body %}
7 changes: 1 addition & 6 deletions tests/Form/CollectionJsTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class CollectionJsTypeTest extends TypeTestCase
public function testDefaultOptions()
{
$entryOptions = [
'label' => true,
'block_prefix' => 'custom_collection_js_entry',
];
$form = $this->factory
Expand All @@ -23,9 +22,7 @@ public function testDefaultOptions()
'entry_options' => $entryOptions,
])
;
$this->assertSame($entryOptions + [
'block_name' => 'entry',
], $form->getConfig()->getOption('entry_options'));
$this->assertSame($entryOptions, $form->getConfig()->getOption('entry_options'));

$view = $form->createView();
$this->assertFalse($view->vars['allow_move_up']);
Expand All @@ -46,9 +43,7 @@ public function testCustomOptions()
])
;
$this->assertSame([
'label' => false,
'block_prefix' => 'collection_js_entry',
'block_name' => 'entry',
], $form->getConfig()->getOption('entry_options'));

$view = $form->createView();
Expand Down

0 comments on commit 295cf68

Please sign in to comment.