fix deprecations: use fqcn instead of 'choices'

This commit is contained in:
nobohan 2018-04-04 15:55:45 +02:00
parent 65e7354437
commit b84ee91652
7 changed files with 100 additions and 92 deletions

View File

@ -6,6 +6,7 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Doctrine\ORM\Query; use Doctrine\ORM\Query;
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup; use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
@ -252,7 +253,7 @@ class CustomFieldsGroupController extends Controller
'action' => $this->generateUrl('customfield_new'), 'action' => $this->generateUrl('customfield_new'),
'csrf_protection' => false 'csrf_protection' => false
)) ))
->add('type', 'choice', array( ->add('type', ChoiceType::class, array(
'choices' => $fieldChoices 'choices' => $fieldChoices
)) ))
->add('customFieldsGroup', HiddenType::class) ->add('customFieldsGroup', HiddenType::class)

View File

@ -3,17 +3,17 @@
/* /*
* Chill is a software for social workers * Chill is a software for social workers
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop> * Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the * published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version. * License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details. * GNU Affero General Public License for more details.
* *
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
@ -31,9 +31,10 @@ use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
use Symfony\Bridge\Twig\TwigEngine; use Symfony\Bridge\Twig\TwigEngine;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Component\Translation\Translator; use Symfony\Component\Translation\Translator;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
/** /**
* *
* *
* @author Julien Fastré <julien.fastre@champs-libres.coop> * @author Julien Fastré <julien.fastre@champs-libres.coop>
* @author Marc Ducobu <marc@champs-libes.coop> * @author Marc Ducobu <marc@champs-libes.coop>
@ -49,7 +50,7 @@ class CustomFieldChoice extends AbstractCustomField
private $defaultLocales; private $defaultLocales;
/** /**
* *
* @var TwigEngine * @var TwigEngine
*/ */
private $templating; private $templating;
@ -58,9 +59,9 @@ class CustomFieldChoice extends AbstractCustomField
* @var TranslatableStringHelper Helper that find the string in current locale from an array of translation * @var TranslatableStringHelper Helper that find the string in current locale from an array of translation
*/ */
private $translatableStringHelper; private $translatableStringHelper;
public function __construct( public function __construct(
Translator $translator, Translator $translator,
TwigEngine $templating, TwigEngine $templating,
TranslatableStringHelper $translatableStringHelper) TranslatableStringHelper $translatableStringHelper)
{ {
@ -68,7 +69,7 @@ class CustomFieldChoice extends AbstractCustomField
$this->templating = $templating; $this->templating = $templating;
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
} }
public function buildForm(FormBuilderInterface $builder, CustomField $customField) public function buildForm(FormBuilderInterface $builder, CustomField $customField)
{ {
//prepare choices //prepare choices
@ -80,7 +81,7 @@ class CustomFieldChoice extends AbstractCustomField
$choices[$persistedChoices['slug']] = $this->translatableStringHelper->localize($persistedChoices['name']); $choices[$persistedChoices['slug']] = $this->translatableStringHelper->localize($persistedChoices['name']);
} }
} }
//prepare $options //prepare $options
$options = array( $options = array(
'multiple' => $customFieldOptions[self::MULTIPLE], 'multiple' => $customFieldOptions[self::MULTIPLE],
@ -104,13 +105,13 @@ class CustomFieldChoice extends AbstractCustomField
new ChoiceWithOtherType($otherValueLabel), new ChoiceWithOtherType($otherValueLabel),
$options) $options)
->addModelTransformer(new CustomFieldDataTransformer($this, $customField))); ->addModelTransformer(new CustomFieldDataTransformer($this, $customField)));
} else { //if allow_other = false } else { //if allow_other = false
//we add the 'expanded' to options //we add the 'expanded' to options
$options['expanded'] = $customFieldOptions[self::EXPANDED]; $options['expanded'] = $customFieldOptions[self::EXPANDED];
$builder->add( $builder->add(
$builder->create($customField->getSlug(), 'choice', $options) $builder->create($customField->getSlug(), ChoiceType::class, $options)
->addModelTransformer(new CustomFieldDataTransformer($this, $customField)) ->addModelTransformer(new CustomFieldDataTransformer($this, $customField))
); );
} }
@ -119,7 +120,7 @@ class CustomFieldChoice extends AbstractCustomField
public function buildOptionsForm(FormBuilderInterface $builder) public function buildOptionsForm(FormBuilderInterface $builder)
{ {
$builder $builder
->add(self::MULTIPLE, 'choice', array( ->add(self::MULTIPLE, ChoiceType::class, array(
'expanded' => true, 'expanded' => true,
'multiple' => false, 'multiple' => false,
'choices' => array( 'choices' => array(
@ -128,7 +129,7 @@ class CustomFieldChoice extends AbstractCustomField
'empty_data' => '0', 'empty_data' => '0',
'label' => 'Multiplicity' 'label' => 'Multiplicity'
)) ))
->add(self::EXPANDED, 'choice', array( ->add(self::EXPANDED, ChoiceType::class, array(
'expanded' => true, 'expanded' => true,
'multiple' => false, 'multiple' => false,
'choices' => array( 'choices' => array(
@ -137,7 +138,7 @@ class CustomFieldChoice extends AbstractCustomField
'empty_data' => '0', 'empty_data' => '0',
'label' => 'Choice display' 'label' => 'Choice display'
)) ))
->add(self::ALLOW_OTHER, 'choice', array( ->add(self::ALLOW_OTHER, ChoiceType::class, array(
'label' => 'Allow other', 'label' => 'Allow other',
'choices' => array( 'choices' => array(
'0' => 'No', '0' => 'No',
@ -152,7 +153,7 @@ class CustomFieldChoice extends AbstractCustomField
'type' => new ChoicesListType($this->defaultLocales), 'type' => new ChoicesListType($this->defaultLocales),
'allow_add' => true 'allow_add' => true
)); ));
return $builder; return $builder;
} }
@ -160,7 +161,7 @@ class CustomFieldChoice extends AbstractCustomField
{ {
// we always have to adapt to what the current data should be // we always have to adapt to what the current data should be
$options = $customField->getOptions(); $options = $customField->getOptions();
if ($options[self::MULTIPLE]) { if ($options[self::MULTIPLE]) {
return $this->deserializeToMultiple($serialized, $options[self::ALLOW_OTHER]); return $this->deserializeToMultiple($serialized, $options[self::ALLOW_OTHER]);
} else { } else {
@ -168,62 +169,62 @@ class CustomFieldChoice extends AbstractCustomField
} }
return $serialized; return $serialized;
} }
private function deserializeToUnique($serialized, $allowOther) private function deserializeToUnique($serialized, $allowOther)
{ {
$value = $this->guessValue($serialized); $value = $this->guessValue($serialized);
// set in a single value. We must have a single string // set in a single value. We must have a single string
$fixedValue = is_array($value) ? $fixedValue = is_array($value) ?
// check if the array has an element, if not replace by empty string // check if the array has an element, if not replace by empty string
count($value) > 0 ? end($value) : '' count($value) > 0 ? end($value) : ''
: :
$value; $value;
if ($allowOther) { if ($allowOther) {
return $this->deserializeWithAllowOther($serialized, $fixedValue); return $this->deserializeWithAllowOther($serialized, $fixedValue);
} else { } else {
return $fixedValue; return $fixedValue;
} }
} }
/** /**
* deserialized the data from the database to a multiple * deserialized the data from the database to a multiple
* field * field
* *
* @param mixed $serialized * @param mixed $serialized
* @param boolean $allowOther * @param boolean $allowOther
*/ */
private function deserializeToMultiple($serialized, $allowOther) private function deserializeToMultiple($serialized, $allowOther)
{ {
$value = $this->guessValue($serialized); $value = $this->guessValue($serialized);
// set in an array : we want a multiple // set in an array : we want a multiple
$fixedValue = is_array($value) ? $value : array($value); $fixedValue = is_array($value) ? $value : array($value);
if ($allowOther) { if ($allowOther) {
return $this->deserializeWithAllowOther($serialized, $fixedValue); return $this->deserializeWithAllowOther($serialized, $fixedValue);
} else { } else {
return $fixedValue; return $fixedValue;
} }
} }
private function deserializeWithAllowOther($serialized, $value) private function deserializeWithAllowOther($serialized, $value)
{ {
$existingOther = isset($serialized['_other']) ? $serialized['_other'] : ''; $existingOther = isset($serialized['_other']) ? $serialized['_other'] : '';
return array( return array(
'_other' => $existingOther, '_other' => $existingOther,
'_choices' => $value '_choices' => $value
); );
} }
/** /**
* Guess the value from the representation of it. * Guess the value from the representation of it.
* *
* If the value had an 'allow_other' = true option, the returned value * If the value had an 'allow_other' = true option, the returned value
* **is not** the content of the _other field, but the `_other` string. * **is not** the content of the _other field, but the `_other` string.
* *
* @param array|string $value * @param array|string $value
* @return mixed * @return mixed
* @throws \LogicException if the case is not covered by this * @throws \LogicException if the case is not covered by this
@ -233,7 +234,7 @@ class CustomFieldChoice extends AbstractCustomField
if ($value === NULL) { if ($value === NULL) {
return NULL; return NULL;
} }
if (!is_array($value)) { if (!is_array($value)) {
return $value; return $value;
} else { } else {
@ -245,7 +246,7 @@ class CustomFieldChoice extends AbstractCustomField
return $value; return $value;
} }
} }
throw \LogicException("This case is not expected."); throw \LogicException("This case is not expected.");
} }
@ -253,13 +254,13 @@ class CustomFieldChoice extends AbstractCustomField
{ {
return 'Choices'; return 'Choices';
} }
public function isEmptyValue($value, CustomField $customField) public function isEmptyValue($value, CustomField $customField)
{ {
if ($value === NULL) { if ($value === NULL) {
return true; return true;
} }
// if multiple choice OR multiple/single choice with other // if multiple choice OR multiple/single choice with other
if (is_array($value)) if (is_array($value))
{ {
@ -284,7 +285,7 @@ class CustomFieldChoice extends AbstractCustomField
} }
/** /**
* *
* @internal this function is able to receive data whichever is the value of "other", "multiple" * @internal this function is able to receive data whichever is the value of "other", "multiple"
* @param mixed $value * @param mixed $value
* @param CustomField $customField * @param CustomField $customField
@ -294,13 +295,13 @@ class CustomFieldChoice extends AbstractCustomField
{ {
//extract the data. They are under a _choice key if they are stored with allow_other //extract the data. They are under a _choice key if they are stored with allow_other
$data = (isset($value['_choices'])) ? $value['_choices'] : $value; $data = (isset($value['_choices'])) ? $value['_choices'] : $value;
$selected = (is_array($data)) ? $data : array($data); $selected = (is_array($data)) ? $data : array($data);
$choices = $customField->getOptions()[self::CHOICES]; $choices = $customField->getOptions()[self::CHOICES];
if (in_array('_other', $selected)){ if (in_array('_other', $selected)){
$choices[] = array('name' => $value['_other'], 'slug' => '_other'); $choices[] = array('name' => $value['_other'], 'slug' => '_other');
} }
$template = 'ChillCustomFieldsBundle:CustomFieldsRendering:choice.html.twig'; $template = 'ChillCustomFieldsBundle:CustomFieldsRendering:choice.html.twig';
if($documentType == 'csv') { if($documentType == 'csv') {
$template = 'ChillCustomFieldsBundle:CustomFieldsRendering:choice.csv.twig'; $template = 'ChillCustomFieldsBundle:CustomFieldsRendering:choice.csv.twig';
@ -309,7 +310,7 @@ class CustomFieldChoice extends AbstractCustomField
return $this->templating return $this->templating
->render($template, ->render($template,
array( array(
'choices' => $choices, 'choices' => $choices,
'selected' => $selected, 'selected' => $selected,
'multiple' => $customField->getOptions()[self::MULTIPLE], 'multiple' => $customField->getOptions()[self::MULTIPLE],
'expanded' => $customField->getOptions()[self::EXPANDED], 'expanded' => $customField->getOptions()[self::EXPANDED],

View File

@ -27,6 +27,7 @@ use Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option;
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer; use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
use Symfony\Bridge\Twig\TwigEngine; use Symfony\Bridge\Twig\TwigEngine;
use Chill\MainBundle\Form\Type\Select2ChoiceType; use Chill\MainBundle\Form\Type\Select2ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
/** /**
* *
@ -109,7 +110,7 @@ class CustomFieldLongChoice extends AbstractCustomField
$choices[$key] = $key; $choices[$key] = $key;
} }
return $builder->add(self::KEY, 'choice', array( return $builder->add(self::KEY, ChoiceType::class, array(
'choices' => $choices, 'choices' => $choices,
'label' => 'Options key' 'label' => 'Options key'
)); ));

View File

@ -3,7 +3,7 @@
/* /*
* Chill is a software for social workers * Chill is a software for social workers
* *
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS, * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>, <info@champs-libres.coop> * <http://www.champs-libres.coop>, <info@champs-libres.coop>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -28,6 +28,7 @@ use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Bundle\TwigBundle\TwigEngine; use Symfony\Bundle\TwigBundle\TwigEngine;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
/** /**
* @author Julien Fastré <julien.fastre@champs-libres.coop> * @author Julien Fastré <julien.fastre@champs-libres.coop>
@ -35,21 +36,21 @@ use Chill\MainBundle\Templating\TranslatableStringHelper;
*/ */
class CustomFieldText extends AbstractCustomField class CustomFieldText extends AbstractCustomField
{ {
private $requestStack; private $requestStack;
/** /**
* *
* @var TwigEngine * @var TwigEngine
*/ */
private $templating; private $templating;
/** /**
* @var TranslatableStringHelper Helper that find the string in current locale from an array of translation * @var TranslatableStringHelper Helper that find the string in current locale from an array of translation
*/ */
private $translatableStringHelper; private $translatableStringHelper;
public function __construct(RequestStack $requestStack, TwigEngine $templating, public function __construct(RequestStack $requestStack, TwigEngine $templating,
TranslatableStringHelper $translatableStringHelper) TranslatableStringHelper $translatableStringHelper)
{ {
@ -57,16 +58,16 @@ class CustomFieldText extends AbstractCustomField
$this->templating = $templating; $this->templating = $templating;
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
} }
const MAX_LENGTH = 'maxLength'; const MAX_LENGTH = 'maxLength';
const MULTIPLE_CF_INLINE ='multipleCFInline'; const MULTIPLE_CF_INLINE ='multipleCFInline';
/** /**
* Create a form according to the maxLength option * Create a form according to the maxLength option
* *
* if maxLength < 256 THEN the form type is 'text' * if maxLength < 256 THEN the form type is 'text'
* if not, THEN the form type is textarea * if not, THEN the form type is textarea
* *
* @param FormBuilderInterface $builder * @param FormBuilderInterface $builder
* @param CustomField $customField * @param CustomField $customField
*/ */
@ -74,16 +75,16 @@ class CustomFieldText extends AbstractCustomField
{ {
$options = $customField->getOptions(); $options = $customField->getOptions();
$type = ($options[self::MAX_LENGTH] < 256) ? 'text' $type = ($options[self::MAX_LENGTH] < 256) ? 'text'
: 'textarea'; : 'textarea';
$attrArray = array(); $attrArray = array();
if(array_key_exists(self::MULTIPLE_CF_INLINE, $options) and if(array_key_exists(self::MULTIPLE_CF_INLINE, $options) and
$options[self::MULTIPLE_CF_INLINE]) { $options[self::MULTIPLE_CF_INLINE]) {
$attrArray['class'] = 'multiple-cf-inline'; $attrArray['class'] = 'multiple-cf-inline';
} }
$builder->add($customField->getSlug(), $type, array( $builder->add($customField->getSlug(), $type, array(
'label' => $this->translatableStringHelper->localize($customField->getName()), 'label' => $this->translatableStringHelper->localize($customField->getName()),
'required' => false, 'required' => false,
@ -121,9 +122,9 @@ class CustomFieldText extends AbstractCustomField
{ {
return $builder return $builder
->add(self::MAX_LENGTH, 'integer', array('empty_data' => 256)) ->add(self::MAX_LENGTH, 'integer', array('empty_data' => 256))
->add(self::MULTIPLE_CF_INLINE, 'choice', array( ->add(self::MULTIPLE_CF_INLINE, ChoiceType::class, array(
'choices' => array( 'choices' => array(
'1' => 'Multiple boxes on the line', '1' => 'Multiple boxes on the line',
'0' => 'One box on the line' '0' => 'One box on the line'
), ),
'label' => 'Box appearance', 'label' => 'Box appearance',

View File

@ -1,20 +1,20 @@
<?php <?php
/* /*
* Chill is a software for social workers * Chill is a software for social workers
* *
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop> * Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the * published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version. * License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details. * GNU Affero General Public License for more details.
* *
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
@ -27,6 +27,7 @@ use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Bundle\TwigBundle\TwigEngine; use Symfony\Bundle\TwigBundle\TwigEngine;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
class CustomFieldTitle extends AbstractCustomField class CustomFieldTitle extends AbstractCustomField
{ {
@ -37,7 +38,7 @@ class CustomFieldTitle extends AbstractCustomField
private $requestStack; private $requestStack;
/** /**
* *
* @var TwigEngine * @var TwigEngine
*/ */
private $templating; private $templating;
@ -54,7 +55,7 @@ class CustomFieldTitle extends AbstractCustomField
$this->templating = $templating; $this->templating = $templating;
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
} }
public function buildForm(FormBuilderInterface $builder, CustomField $customField) public function buildForm(FormBuilderInterface $builder, CustomField $customField)
{ {
$builder->add($customField->getSlug(), 'custom_field_title', array( $builder->add($customField->getSlug(), 'custom_field_title', array(
@ -68,9 +69,9 @@ class CustomFieldTitle extends AbstractCustomField
} }
public function render($value, CustomField $customField, $documentType = 'html') public function render($value, CustomField $customField, $documentType = 'html')
{ {
return $this->templating return $this->templating
->render('ChillCustomFieldsBundle:CustomFieldsRendering:title.html.twig', ->render('ChillCustomFieldsBundle:CustomFieldsRendering:title.html.twig',
array( array(
'title' => $customField->getName(), 'title' => $customField->getName(),
'type' => $customField->getOptions()[self::TYPE] 'type' => $customField->getOptions()[self::TYPE]
@ -92,7 +93,7 @@ class CustomFieldTitle extends AbstractCustomField
{ {
return 'Title'; return 'Title';
} }
public function isEmptyValue($value, CustomField $customField) public function isEmptyValue($value, CustomField $customField)
{ {
return false; return false;
@ -100,7 +101,7 @@ class CustomFieldTitle extends AbstractCustomField
public function buildOptionsForm(FormBuilderInterface $builder) public function buildOptionsForm(FormBuilderInterface $builder)
{ {
return $builder->add(self::TYPE, 'choice', return $builder->add(self::TYPE, ChoiceType::class,
array( array(
'choices' => array( 'choices' => array(
self::TYPE_TITLE => 'Main title', self::TYPE_TITLE => 'Main title',

View File

@ -8,25 +8,26 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
class CustomFieldsGroupType extends AbstractType class CustomFieldsGroupType extends AbstractType
{ {
private $customizableEntities; //TOOD : add comment about this variable private $customizableEntities; //TOOD : add comment about this variable
/** /**
* *
* @var \Symfony\Component\Translation\TranslatorInterface * @var \Symfony\Component\Translation\TranslatorInterface
*/ */
private $translator; private $translator;
public function __construct(array $customizableEntities, TranslatorInterface $translator) public function __construct(array $customizableEntities, TranslatorInterface $translator)
{ {
$this->customizableEntities = $customizableEntities; $this->customizableEntities = $customizableEntities;
$this->translator = $translator; $this->translator = $translator;
} }
/** /**
* @param FormBuilderInterface $builder * @param FormBuilderInterface $builder
* @param array $options * @param array $options
@ -37,43 +38,43 @@ class CustomFieldsGroupType extends AbstractType
//prepare translation //prepare translation
$entities = array(); $entities = array();
$customizableEntities = array(); //TODO : change name too close than $this->customizableEntities $customizableEntities = array(); //TODO : change name too close than $this->customizableEntities
foreach($this->customizableEntities as $key => $definition) { foreach($this->customizableEntities as $key => $definition) {
$entities[$definition['class']] = $this->translator->trans($definition['name']); $entities[$definition['class']] = $this->translator->trans($definition['name']);
$customizableEntities[$definition['class']] = $definition; $customizableEntities[$definition['class']] = $definition;
} }
$builder $builder
->add('name', 'translatable_string') ->add('name', 'translatable_string')
->add('entity', 'choice', array( ->add('entity', ChoiceType::class, array(
'choices' => $entities 'choices' => $entities
)) ))
; ;
$builder->addEventListener(FormEvents::POST_SET_DATA, $builder->addEventListener(FormEvents::POST_SET_DATA,
function(FormEvent $event) use ($customizableEntities, $builder){ function(FormEvent $event) use ($customizableEntities, $builder){
$form = $event->getForm(); $form = $event->getForm();
$group = $event->getData(); $group = $event->getData();
//stop the function if entity is not set //stop the function if entity is not set
if ($group->getEntity() === NULL) { if ($group->getEntity() === NULL) {
return; return;
} }
if (count($customizableEntities[$group->getEntity()]['options']) > 0) { if (count($customizableEntities[$group->getEntity()]['options']) > 0) {
$optionBuilder = $builder $optionBuilder = $builder
->getFormFactory() ->getFormFactory()
->createBuilderForProperty( ->createBuilderForProperty(
'Chill\CustomFieldsBundle\Entity\CustomFieldsGroup', 'Chill\CustomFieldsBundle\Entity\CustomFieldsGroup',
'options' 'options'
) )
->create('options', null, array( ->create('options', null, array(
'compound' => true, 'compound' => true,
'auto_initialize' => false, 'auto_initialize' => false,
'required' => false) 'required' => false)
); );
} }
foreach($customizableEntities[$group->getEntity()]['options'] as $key => $option) { foreach($customizableEntities[$group->getEntity()]['options'] as $key => $option) {
$optionBuilder $optionBuilder
->add($key, $option['form_type'], $option['form_options']) ->add($key, $option['form_type'], $option['form_options'])
@ -82,10 +83,10 @@ class CustomFieldsGroupType extends AbstractType
if (isset($optionBuilder) && $optionBuilder->count() > 0) { if (isset($optionBuilder) && $optionBuilder->count() > 0) {
$form->add($optionBuilder->getForm()); $form->add($optionBuilder->getForm());
} }
}); });
} }
/** /**
* @param OptionsResolverInterface $resolver * @param OptionsResolverInterface $resolver
*/ */

View File

@ -4,10 +4,12 @@ namespace Chill\CustomFieldsBundle\Form\Type;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
/** /**
* Return a choice widget with an "other" option * Return a choice widget with an "other" option
* *
* @author Julien Fastré <julien.fastre@champs-libres.coop> * @author Julien Fastré <julien.fastre@champs-libres.coop>
* *
*/ */
@ -24,7 +26,7 @@ class ChoiceWithOtherType extends AbstractType
/* (non-PHPdoc) /* (non-PHPdoc)
* @see \Symfony\Component\Form\AbstractType::buildForm() * @see \Symfony\Component\Form\AbstractType::buildForm()
*/ */
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
//add an 'other' entry in choices array //add an 'other' entry in choices array
$options['choices']['_other'] = $this->otherValueLabel; $options['choices']['_other'] = $this->otherValueLabel;
@ -32,17 +34,17 @@ class ChoiceWithOtherType extends AbstractType
$options['expanded'] = true; $options['expanded'] = true;
// adding a default value for choice // adding a default value for choice
$options['empty_data'] = null; $options['empty_data'] = null;
$builder $builder
->add('_other', 'text', array('required' => false)) ->add('_other', TextType::class, array('required' => false))
->add('_choices', 'choice', $options) ->add('_choices', ChoiceType::class, $options)
; ;
} }
/* (non-PHPdoc) /* (non-PHPdoc)
* @see \Symfony\Component\Form\AbstractType::configureOptions() * @see \Symfony\Component\Form\AbstractType::configureOptions()
*/ */
public function configureOptions(OptionsResolver $resolver) public function configureOptions(OptionsResolver $resolver)
{ {
$resolver $resolver
->setRequired(array('choices')) ->setRequired(array('choices'))
@ -57,4 +59,4 @@ class ChoiceWithOtherType extends AbstractType
{ {
return 'choice_with_other'; return 'choice_with_other';
} }
} }