Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loading new item list after adding new created element on sortable collections #8202

Open
wants to merge 5 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Form/Extension/ChoiceTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\Form\FormView;
Expand All @@ -27,6 +30,25 @@
*/
final class ChoiceTypeExtension extends AbstractTypeExtension
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
if ($options['multiple'] && (true === ($options['sortable'] ?? false))) {

Check failure on line 35 in src/Form/Extension/ChoiceTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan

Only booleans are allowed in &&, mixed given on the left side.
$builder->addEventListener(FormEvents::PRE_SUBMIT, static function (FormEvent $event) use ($options) {

Check failure on line 36 in src/Form/Extension/ChoiceTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan

Anonymous function has an unused use $options.
/** @var PreSubmitEvent $event */
$form = $event->getForm();

Check failure on line 38 in src/Form/Extension/ChoiceTypeExtension.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedDocblockClass

src/Form/Extension/ChoiceTypeExtension.php:38:17: UndefinedDocblockClass: Docblock-defined class, interface or enum named Sonata\AdminBundle\Form\Extension\PreSubmitEvent does not exist (see https://psalm.dev/200)

Check failure on line 38 in src/Form/Extension/ChoiceTypeExtension.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedDocblockClass

src/Form/Extension/ChoiceTypeExtension.php:38:25: UndefinedDocblockClass: Docblock-defined class, interface or enum named Sonata\AdminBundle\Form\Extension\PreSubmitEvent does not exist (see https://psalm.dev/200)

Check failure on line 38 in src/Form/Extension/ChoiceTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to method getForm() on an unknown class Sonata\AdminBundle\Form\Extension\PreSubmitEvent.

Check failure on line 38 in src/Form/Extension/ChoiceTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan

PHPDoc tag @var for variable $event contains unknown class Sonata\AdminBundle\Form\Extension\PreSubmitEvent.

Check failure on line 38 in src/Form/Extension/ChoiceTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan

PHPDoc tag @var with type Sonata\AdminBundle\Form\Extension\PreSubmitEvent is not subtype of native type Symfony\Component\Form\FormEvent.
$data = $event->getData();

Check failure on line 39 in src/Form/Extension/ChoiceTypeExtension.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedDocblockClass

src/Form/Extension/ChoiceTypeExtension.php:39:25: UndefinedDocblockClass: Docblock-defined class, interface or enum named Sonata\AdminBundle\Form\Extension\PreSubmitEvent does not exist (see https://psalm.dev/200)

Check failure on line 39 in src/Form/Extension/ChoiceTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to method getData() on an unknown class Sonata\AdminBundle\Form\Extension\PreSubmitEvent.

if (!is_array($data) || count($data) !== 1) {
return;
}

if (str_contains($data[0], ',')) {
$event->setData(explode(',', $data[0]));

Check failure on line 46 in src/Form/Extension/ChoiceTypeExtension.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedDocblockClass

src/Form/Extension/ChoiceTypeExtension.php:46:21: UndefinedDocblockClass: Docblock-defined class, interface or enum named Sonata\AdminBundle\Form\Extension\PreSubmitEvent does not exist (see https://psalm.dev/200)

Check failure on line 46 in src/Form/Extension/ChoiceTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to method setData() on an unknown class Sonata\AdminBundle\Form\Extension\PreSubmitEvent.
Comment on lines +45 to +46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it's work for your usage (with the javascript),
I assume it will introduce a bug if someone use this Extension/FormType in another situation for texte with string like

['Foo, bar']

It will become ['Foo', 'bar'] instead of staying the same...

I dunno if there is a way to avoid such thing...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the correct approach here would be to have multiple hidden inputs, one for each id. Like the ModelAutocompleteType does it.

This would probably be more work, tho.

Another approach would be adding a special prefix to the value, that then can be checked.

}
}, 1);
}
}

public function configureOptions(OptionsResolver $resolver): void
{
$optionalOptions = ['sortable'];
Expand Down
17 changes: 12 additions & 5 deletions src/Resources/views/CRUD/Association/edit_many_script.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,24 @@ This code manages the many-to-[one|many] association field popup
data: {_xml_http_request: true },
dataType: 'html',
type: 'POST',
success: function(html) {
success: function(html) {
jQuery('#field_container_{{ id }}').replaceWith(html);
var newElement = jQuery('#{{ id }} [value="' + data.objectId + '"]');
if (newElement.is("input")) {
newElement.attr('checked', 'checked');

if (newElement.length) {
if (newElement.is("input")) {
newElement.attr('checked', 'checked');
} else {
newElement.attr('selected', 'selected');
}
} else {
newElement.attr('selected', 'selected');
var selections = jQuery('#{{ id }}').val().split(',');
selections.push(data.objectId);
jQuery('#{{ id }}').val(selections.filter((val) => val.length > 0).join(','));
Comment on lines +343 to +345
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If entities use string as id, the usage of , could conflict no ?

}

jQuery('#field_container_{{ id }}').trigger('sonata-admin-append-form-element');
}
}
});

{% endif %}
Expand Down
Loading