mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Order languages in alphabetical order in every language
This commit is contained in:
parent
7b7c327a9a
commit
787ed4a7c2
85
Form/Type/DataTransformer/MultipleObjectsToIdTransformer.php
Normal file
85
Form/Type/DataTransformer/MultipleObjectsToIdTransformer.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
* Copyright (C) 2014 Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -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
|
||||
));
|
||||
}
|
||||
}
|
@ -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 }
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user