mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-20 09:14:23 +00:00
allow group_by in PickRoleType and PickStatusType
This commit is contained in:
parent
cf4a0d9e75
commit
e774090959
@ -31,6 +31,8 @@ use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
|
||||
/**
|
||||
* Allow to pick a choice amongst different choices
|
||||
@ -71,8 +73,6 @@ class PickRoleType extends AbstractType
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
// create copy for use in Closure
|
||||
$translatableStringHelper = $this->translatableStringHelper;
|
||||
// create copy for easier management
|
||||
$qb = $options['query_builder'];
|
||||
|
||||
@ -86,16 +86,25 @@ class PickRoleType extends AbstractType
|
||||
->setParameter('active', true);
|
||||
}
|
||||
|
||||
// if ($options['show_group_type'] === true) {
|
||||
// $closure = $options['choice_label'];
|
||||
// $options['choice_label'] = function(Role $r)
|
||||
// use ($translatableStringHelper, $closure) {
|
||||
// return $translatableStringHelper->localize($r->get)
|
||||
//
|
||||
// }
|
||||
// }
|
||||
|
||||
if ($options['group_by'] === null) {
|
||||
$builder->addEventListener(
|
||||
FormEvents::PRE_SET_DATA,
|
||||
function(FormEvent $event) use ($options) {
|
||||
if ($options['event_type'] === null) {
|
||||
$form = $event->getForm();
|
||||
$name = $form->getName();
|
||||
$config = $form->getConfig();
|
||||
$type = $config->getType()->getName();
|
||||
$options = $config->getOptions();
|
||||
|
||||
$form->getParent()->add($name, $type, array_replace($options, array(
|
||||
'group_by' => function(Role $r)
|
||||
{ return $this->translatableStringHelper->localize($r->getType()->getName()); }
|
||||
)));
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
@ -112,9 +121,6 @@ class PickRoleType extends AbstractType
|
||||
// add option allow unactive
|
||||
->setDefault('active_only', true)
|
||||
->setAllowedTypes('active_only', array('boolean'))
|
||||
// add possibility to prepend by group_type
|
||||
->setDefault('show_group_type', false)
|
||||
->setAllowedTypes('show_group_type', array('boolean'))
|
||||
;
|
||||
|
||||
$qb = $this->roleRepository->createQueryBuilder('r');
|
||||
@ -122,6 +128,7 @@ class PickRoleType extends AbstractType
|
||||
$resolver->setDefaults(array(
|
||||
'class' => Role::class,
|
||||
'query_builder' => $qb,
|
||||
'group_by' => null,
|
||||
'choice_attr' => function(Role $r) {
|
||||
return array(
|
||||
'data-event-type' => $r->getType()->getId(),
|
||||
|
@ -31,6 +31,8 @@ use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
|
||||
/**
|
||||
* Allow to pick amongst type
|
||||
@ -89,6 +91,22 @@ class PickStatusType extends AbstractType
|
||||
->setParameter('active', true);
|
||||
}
|
||||
|
||||
if ($options['group_by'] === null && $options['event_type'] === null) {
|
||||
$builder->addEventListener(
|
||||
FormEvents::PRE_SET_DATA,
|
||||
function(FormEvent $event) {
|
||||
$form = $event->getForm();
|
||||
$name = $form->getName();
|
||||
$config = $form->getConfig();
|
||||
$type = $config->getType()->getName();
|
||||
$options = $config->getOptions();
|
||||
$form->getParent()->add($name, $type, array_replace($options, array(
|
||||
'group_by' => function(Status $s)
|
||||
{ return $this->translatableStringHelper->localize($s->getType()->getName()); }
|
||||
)));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -113,6 +131,7 @@ class PickStatusType extends AbstractType
|
||||
$resolver->setDefaults(array(
|
||||
'class' => Status::class,
|
||||
'query_builder' => $qb,
|
||||
'group_by' => null,
|
||||
'choice_attr' => function(Status $s) {
|
||||
return array(
|
||||
'data-event-type' => $s->getType()->getId()
|
||||
|
Loading…
x
Reference in New Issue
Block a user