mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
order the type alphabetically
Improve also the form "TranslatableActivityType" to use the TranslatableStringHelper.
This commit is contained in:
parent
97df048dfb
commit
1ab0b95e74
@ -47,6 +47,7 @@ class ChillActivityExtension extends Extension implements PrependExtensionInterf
|
||||
|
||||
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
||||
$loader->load('services.yml');
|
||||
$loader->load('services/repositories.yml');
|
||||
}
|
||||
|
||||
public function prepend(ContainerBuilder $container)
|
||||
|
@ -25,6 +25,10 @@ namespace Chill\ActivityBundle\Form\Type;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Chill\ActivityBundle\Entity\ActivityType;
|
||||
|
||||
/**
|
||||
* Description of TranslatableActivityType
|
||||
@ -33,14 +37,22 @@ use Symfony\Component\HttpFoundation\RequestStack;
|
||||
*/
|
||||
class TranslatableActivityType extends AbstractType
|
||||
{
|
||||
/**
|
||||
* @var RequestStack
|
||||
*/
|
||||
private $requestStack;
|
||||
|
||||
public function __construct(RequestStack $requestStack)
|
||||
/**
|
||||
*
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
protected $translatableStringHelper;
|
||||
|
||||
protected $activityTypeRepository;
|
||||
|
||||
public function __construct(
|
||||
TranslatableStringHelper $helper,
|
||||
EntityRepository $activityTypeRepository
|
||||
)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
$this->translatableStringHelper = $helper;
|
||||
$this->activityTypeRepository = $activityTypeRepository;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
@ -50,16 +62,34 @@ class TranslatableActivityType extends AbstractType
|
||||
|
||||
public function getParent()
|
||||
{
|
||||
return 'entity';
|
||||
return EntityType::class;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$locale = $this->requestStack->getCurrentRequest()->getLocale();
|
||||
// create a local copy for use in closures
|
||||
$translatableStringHelper = $this->translatableStringHelper;
|
||||
$types = $this->activityTypeRepository->findAll();
|
||||
|
||||
// sort by alphabetical order
|
||||
usort($types, function(ActivityType $typeA, ActivityType $typeB) use ($translatableStringHelper) {
|
||||
$strA = $translatableStringHelper->localize($typeA->getName());
|
||||
$strB = $translatableStringHelper->localize($typeB->getName());
|
||||
|
||||
if ($strA === $strB) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $strA < $strB ? -1 : 1;
|
||||
});
|
||||
|
||||
$resolver->setDefaults(
|
||||
array(
|
||||
'class' => 'ChillActivityBundle:ActivityType',
|
||||
'property' => 'name['.$locale.']'
|
||||
'choices' => $types,
|
||||
'choice_label' => function (ActivityType $type) use ($translatableStringHelper) {
|
||||
return $translatableStringHelper->localize($type->getName());
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@ -16,7 +16,8 @@ services:
|
||||
chill.activity.form.type.translatableactivitytype:
|
||||
class: Chill\ActivityBundle\Form\Type\TranslatableActivityType
|
||||
arguments:
|
||||
- "@request_stack"
|
||||
- "@chill.main.helper.translatable_string"
|
||||
- "@chill_activity.repository.activity_type"
|
||||
tags:
|
||||
- { name: form.type, alias: translatable_activity_type }
|
||||
|
||||
|
6
Resources/config/services/repositories.yml
Normal file
6
Resources/config/services/repositories.yml
Normal file
@ -0,0 +1,6 @@
|
||||
services:
|
||||
chill_activity.repository.activity_type:
|
||||
class: Doctrine\ORM\EntityRepository
|
||||
factory: ['@doctrine.orm.entity_manager', getRepository]
|
||||
arguments:
|
||||
- 'Chill\ActivityBundle\Entity\ActivityType'
|
115
Tests/Form/Type/TranslatableActivityTypeTest.php
Normal file
115
Tests/Form/Type/TranslatableActivityTypeTest.php
Normal file
@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2016 Champs-Libres <info@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\ActivityBundle\Tests\Form\Type;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Chill\ActivityBundle\Form\Type\TranslatableActivityType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class TranslatableActivityTypeTest extends KernelTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Symfony\Component\Form\FormBuilderInterface
|
||||
*/
|
||||
protected $builder;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Symfony\Component\DependencyInjection\ContainerInterface
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
self::bootKernel();
|
||||
|
||||
$this->container = self::$kernel->getContainer();
|
||||
|
||||
$this->builder = $this->container
|
||||
->get('form.factory')
|
||||
->createBuilder(FormType::class, null, array(
|
||||
'csrf_protection' => false,
|
||||
'csrf_field_name' => '_token'
|
||||
));
|
||||
|
||||
$request = new \Symfony\Component\HttpFoundation\Request();
|
||||
$request->setLocale('fr');
|
||||
|
||||
$this->container->get('request_stack')
|
||||
->push($request);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return \Chill\ActivityBundle\Entity\ActivityType
|
||||
*/
|
||||
protected function getRandomType()
|
||||
{
|
||||
$types = $this->container->get('doctrine.orm.entity_manager')
|
||||
->getRepository('ChillActivityBundle:ActivityType')
|
||||
->findAll();
|
||||
|
||||
return $types[array_rand($types)];
|
||||
}
|
||||
|
||||
public function testForm()
|
||||
{
|
||||
$type = $this->getRandomType();
|
||||
$form = $this->builder->add('type', TranslatableActivityType::class)
|
||||
->getForm();
|
||||
|
||||
$form->submit(array(
|
||||
'type' => $type->getId()
|
||||
));
|
||||
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
$this->assertInstanceOf(\Chill\ActivityBundle\Entity\ActivityType::class,
|
||||
$form->getData()['type'],
|
||||
"The data is an instance of Chill\ActivityBundle\Entity\ActivityType");
|
||||
$this->assertEquals($type->getId(), $form->getData()['type']->getId());
|
||||
|
||||
// test the ordering of the types in the form
|
||||
$view = $form->createView();
|
||||
|
||||
$this->assertGreaterThan(0, count($view['type']->vars['choices']),
|
||||
"test that there are at least one choice");
|
||||
|
||||
foreach($view['type']->vars['choices'] as $choice) {
|
||||
// initialize the previous value is not set (this is the first)
|
||||
if (!isset($previous)) {
|
||||
$previous = $choice->label;
|
||||
} else {
|
||||
$this->assertTrue($previous < $choice->label);
|
||||
$previous = $choice->label;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user