diff --git a/Form/Type/DataTransformer/MultipleObjectsToIdTransformer.php b/Form/Type/DataTransformer/MultipleObjectsToIdTransformer.php new file mode 100644 index 000000000..9f2ce1b7c --- /dev/null +++ b/Form/Type/DataTransformer/MultipleObjectsToIdTransformer.php @@ -0,0 +1,85 @@ + + * + * 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\DataTransformer; + +use Symfony\Component\Form\DataTransformerInterface; +use Symfony\Component\Form\Exception\TransformationFailedException; +use Doctrine\Common\Persistence\ObjectManager; +use Doctrine\Common\Collections\ArrayCollection; + +class MultipleObjectsToIdTransformer implements DataTransformerInterface +{ + /** + * @var ObjectManager + */ + private $em; + + /** + * @var string + */ + private $class; + + /** + * @param ObjectManager $em + */ + public function __construct(ObjectManager $em, $class) + { + $this->em = $em; + $this->class = $class; + } + + /** + * Transforms an object (use) to a string (id). + * + * @param array $array + * @return ArrayCollection + */ + public function transform($array) + { + $ret = array(); + + foreach ($array as $el) { + $ret[] = ($el->getId()); + } + + return $ret; + } + + /** + * Transforms a string (id) to an object (item). + * + * @param string $id + * @return ArrayCollection + */ + public function reverseTransform($array) + { + $ret = new ArrayCollection(); + + foreach ($array as $el) { + $ret->add( + $this->em + ->getRepository($this->class) + ->find($el) + ); + } + return $ret; + } +} \ No newline at end of file diff --git a/Form/Type/Select2LanguageType.php b/Form/Type/Select2LanguageType.php index 31808c723..6b0b88ce5 100644 --- a/Form/Type/Select2LanguageType.php +++ b/Form/Type/Select2LanguageType.php @@ -21,10 +21,12 @@ 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; +use Symfony\Component\Form\FormBuilderInterface; +use Chill\MainBundle\Form\Type\DataTransformer\MultipleObjectsToIdTransformer; +use Doctrine\Common\Persistence\ObjectManager; /** * Extends choice to allow adding select2 library on widget for languages (multiple) @@ -32,14 +34,19 @@ use Symfony\Component\HttpFoundation\RequestStack; class Select2LanguageType extends AbstractType { /** - * * @var RequestStack */ private $requestStack; + + /** + * @var ObjectManager + */ + private $em; - public function __construct(RequestStack $requestStack) + public function __construct(RequestStack $requestStack,ObjectManager $em) { $this->requestStack = $requestStack; + $this->em = $em; } public function getName() @@ -47,18 +54,32 @@ class Select2LanguageType extends AbstractType return 'select2_chill_language'; } + public function buildForm(FormBuilderInterface $builder, array $options) + { + $transformer = new MultipleObjectsToIdTransformer($this->em,'Chill\MainBundle\Entity\Language'); + $builder->addModelTransformer($transformer); + } + public function getParent() { - return 'select2_entity'; + return 'select2_choice'; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $locale = $this->requestStack->getCurrentRequest()->getLocale(); - + $languages = $this->em->getRepository('Chill\MainBundle\Entity\Language')->findAll(); + $choices = array(); + + foreach ($languages as $l) { + $choices[$l->getId()] = $l->getName()[$locale]; + } + + asort($choices, SORT_STRING | SORT_FLAG_CASE); + $resolver->setDefaults(array( 'class' => 'Chill\MainBundle\Entity\Language', - 'property' => 'name['.$locale.']' + 'choices' => $choices )); } } \ No newline at end of file diff --git a/Resources/config/services.yml b/Resources/config/services.yml index d6f49c64c..ec8434a8b 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -60,6 +60,7 @@ services: class: Chill\MainBundle\Form\Type\Select2LanguageType arguments: - "@request_stack" + - "@doctrine.orm.entity_manager" tags: - { name: form.type, alias: select2_chill_language }