From 7276cb971f87dd060cbb82a2648844a5b96f1005 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Mon, 10 Oct 2022 15:59:16 +0200 Subject: [PATCH] issue649: new formType to use select2 with LocationTypeFilter --- .../Filter/ACPFilters/LocationTypeFilter.php | 11 ++-- .../Form/Type/Select2LocationTypeType.php | 51 +++++++++++++++++++ .../translations/messages.fr.yml | 1 + 3 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php diff --git a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/LocationTypeFilter.php b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/LocationTypeFilter.php index 92dc1b0eb..023882cf9 100644 --- a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/LocationTypeFilter.php +++ b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/LocationTypeFilter.php @@ -12,12 +12,11 @@ declare(strict_types=1); namespace Chill\ActivityBundle\Export\Filter\ACPFilters; use Chill\ActivityBundle\Export\Declarations; -use Chill\MainBundle\Entity\LocationType; use Chill\MainBundle\Export\FilterInterface; +use Chill\MainBundle\Form\Type\Select2LocationTypeType; use Chill\MainBundle\Templating\TranslatableStringHelper; use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; -use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\FormBuilderInterface; use function in_array; @@ -61,13 +60,9 @@ class LocationTypeFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_locationtype', EntityType::class, [ - 'class' => LocationType::class, - 'choice_label' => function (LocationType $type) { - return $this->translatableStringHelper->localize($type->getTitle()); - }, + $builder->add('accepted_locationtype', Select2LocationTypeType::class, [ 'multiple' => true, - 'expanded' => true, + //'label' => false, ]); } diff --git a/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php b/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php new file mode 100644 index 000000000..8fd5dbe91 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php @@ -0,0 +1,51 @@ +translatableStringHelper = $translatableStringHelper; + } + + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder->add('locationtype', EntityType::class, [ + 'class' => LocationType::class, + 'choice_label' => function (LocationType $type) { + return $this->translatableStringHelper->localize($type->getTitle()); + }, + 'placeholder' => 'Pick a location type', + 'required' => false, + 'label' => $options['label'], + 'label_attr' => $options['label_attr'], + 'multiple' => $options['multiple'], + 'attr' => ['class' => 'select2'], + ]); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver + ->setDefault('label', 'Location type') + ->setDefault('label_attr', []) + ->setDefault('multiple', false) + ->setAllowedTypes('multiple', ['bool']) + ; + } + + public function getBlockPrefix() + { + return 'select2_location_type_type'; + } +} diff --git a/src/Bundle/ChillMainBundle/translations/messages.fr.yml b/src/Bundle/ChillMainBundle/translations/messages.fr.yml index 1e95e32d0..76dc27b39 100644 --- a/src/Bundle/ChillMainBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillMainBundle/translations/messages.fr.yml @@ -227,6 +227,7 @@ never: jamais Create a new location: Créer une nouvelle localisation Location list: Liste des localisations Location type: Type de localisation +Pick a location type: Choisir un type de localisation Phonenumber1: Numéro de téléphone Phonenumber2: Autre numéro de téléphone Location configuration: Configuration des localisations