From 888bcad81a794b4d459e5b865f9efbbd7bc35dcd Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Tue, 22 Mar 2016 20:34:34 +0100 Subject: [PATCH] Adding type to the creation event form --- DependencyInjection/ChillEventExtension.php | 1 + Form/EventType.php | 3 +- Form/Type/TranslatableEventType.php | 70 +++++++++++++++++++++ Resources/config/services/forms.yml | 7 +++ 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 Form/Type/TranslatableEventType.php create mode 100644 Resources/config/services/forms.yml diff --git a/DependencyInjection/ChillEventExtension.php b/DependencyInjection/ChillEventExtension.php index 5933488f1..29e273178 100644 --- a/DependencyInjection/ChillEventExtension.php +++ b/DependencyInjection/ChillEventExtension.php @@ -29,6 +29,7 @@ class ChillEventExtension extends Extension implements PrependExtensionInterface $loader->load('repositories.yml'); $loader->load('search.yml'); $loader->load('authorization.yml'); + $loader->load('forms.yml'); } /* (non-PHPdoc) diff --git a/Form/EventType.php b/Form/EventType.php index bdfad1855..053c02007 100644 --- a/Form/EventType.php +++ b/Form/EventType.php @@ -5,6 +5,7 @@ namespace Chill\EventBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; +use Chill\EventBundle\Form\Type\TranslatableEventType; class EventType extends AbstractType { @@ -26,7 +27,7 @@ class EventType extends AbstractType ) ) ->add('center') - //->add('type') + ->add('type', TranslatableEventType::class) //->add('circle') ; } diff --git a/Form/Type/TranslatableEventType.php b/Form/Type/TranslatableEventType.php new file mode 100644 index 000000000..cf2abc14a --- /dev/null +++ b/Form/Type/TranslatableEventType.php @@ -0,0 +1,70 @@ +, + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\EventBundle\Form\Type; + +use Symfony\Component\Form\AbstractType; +use Symfony\Component\OptionsResolver\OptionsResolver; +use Chill\MainBundle\Templating\TranslatableStringHelper; +use Symfony\Bridge\Doctrine\Form\Type\EntityType; +use Doctrine\ORM\EntityRepository; +use Chill\EventBundle\Entity\EventType; + +/** + * Description of TranslatableEventType + * + * @author Champs-Libres Coop + */ +class TranslatableEventType extends AbstractType +{ + /** + * @var TranslatableStringHelper + */ + protected $translatableStringHelper; + + public function __construct(TranslatableStringHelper $helper) + { + $this->translatableStringHelper = $helper; + } + + public function getParent() + { + return EntityType::class; + } + + public function configureOptions(OptionsResolver $resolver) + { + $helper = $this->translatableStringHelper; + $resolver->setDefaults( + array( + 'class' => EventType::class, + 'query_builder' => function (EntityRepository $er) { + return $er->createQueryBuilder('et') + ->where('et.active = true'); + }, + 'choice_label' => function (EventType $t) use ($helper) { + return $helper->localize($t->getLabel()); + }, + ) + ); + } +} diff --git a/Resources/config/services/forms.yml b/Resources/config/services/forms.yml new file mode 100644 index 000000000..1dc75d5e4 --- /dev/null +++ b/Resources/config/services/forms.yml @@ -0,0 +1,7 @@ +services: + chill_event.form.event_type_type: + class: Chill\EventBundle\Form\Type\TranslatableEventType + arguments: + - "@chill.main.helper.translatable_string" + tags: + - { name: form.type }