From 79ccfb184a2a866128297578405f2953b8705902 Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Wed, 26 Nov 2014 12:58:01 +0100 Subject: [PATCH] Form type for Languages (select2) --- Form/Type/Select2LanguageType.php | 64 +++++++++++++++++++++++++++++++ Resources/config/services.yml | 10 ++++- 2 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 Form/Type/Select2LanguageType.php diff --git a/Form/Type/Select2LanguageType.php b/Form/Type/Select2LanguageType.php new file mode 100644 index 000000000..31808c723 --- /dev/null +++ b/Form/Type/Select2LanguageType.php @@ -0,0 +1,64 @@ + + * + * 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\MainBundle\Form\Type; + +use Symfony\Component\Form\AbstractType; +use Chill\MainBundle\Templating\TranslatableStringHelper; +use Symfony\Component\OptionsResolver\OptionsResolverInterface; +use Chill\MainBundle\Entity\Language; +use Symfony\Component\HttpFoundation\RequestStack; + +/** + * Extends choice to allow adding select2 library on widget for languages (multiple) + */ +class Select2LanguageType extends AbstractType +{ + /** + * + * @var RequestStack + */ + private $requestStack; + + public function __construct(RequestStack $requestStack) + { + $this->requestStack = $requestStack; + } + + public function getName() + { + return 'select2_chill_language'; + } + + public function getParent() + { + return 'select2_entity'; + } + + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + $locale = $this->requestStack->getCurrentRequest()->getLocale(); + + $resolver->setDefaults(array( + 'class' => 'Chill\MainBundle\Entity\Language', + 'property' => 'name['.$locale.']' + )); + } +} \ No newline at end of file diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 93cd62958..5267c0fae 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -54,7 +54,13 @@ services: - "@request_stack" tags: - { name: form.type, alias: select2_chill_country } - + + chill.main.form.type.select2language: + class: Chill\MainBundle\Form\Type\Select2LanguageType + arguments: + - "@request_stack" + tags: + - { name: form.type, alias: select2_chill_language } + chill.main.search_provider: class: Chill\MainBundle\Search\SearchProvider -