diff --git a/Form/Type/LinkedCustomFieldsType.php b/Form/Type/LinkedCustomFieldsType.php index 129fd508c..90b89bc1b 100644 --- a/Form/Type/LinkedCustomFieldsType.php +++ b/Form/Type/LinkedCustomFieldsType.php @@ -26,11 +26,12 @@ use Symfony\Component\Form\FormEvents; use Chill\MainBundle\Templating\TranslatableStringHelper; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; /** * This type create a Choice field with custom fields as choices. - * - * This type can only be associated with a customFieldsGroup type. The field + * + * This type can only be associated with a customFieldsGroup type. The field * is populated when the data (a customFieldsGroup entity) is associated with * the form * @@ -43,51 +44,51 @@ class LinkedCustomFieldsType extends AbstractType * @var TranslatableStringHelper */ private $translatableStringHelper; - + /** * The name for the choice field - * + * * Extracted from builder::getName - * + * * @var string */ private $choiceName = 'choice'; - + /** * the option of the form. - * + * * @internal options are stored at the class level to be reused by appendChoice, after data are setted * @var array */ private $options = array(); - + public function __construct(TranslatableStringHelper $helper) { $this->translatableStringHelper = $helper; } - - + + public function buildForm(FormBuilderInterface $builder, array $options) { $this->choiceName = $builder->getName(); $this->options = $options; - - $builder->addEventListener(FormEvents::POST_SET_DATA, + + $builder->addEventListener(FormEvents::POST_SET_DATA, array($this, 'appendChoice')) ; } - + public function getParent() { return 'choice'; } - + /** * append Choice on POST_SET_DATA event - * + * * Choices are extracted from custom_field_group (the data associated * with the root form) - * + * * @param FormEvent $event * @return void */ @@ -95,29 +96,29 @@ class LinkedCustomFieldsType extends AbstractType { $rootForm = $this->getRootForm($event->getForm()); $group = $rootForm->getData(); - + if ($group === NULL) { return; } $choices = array(); foreach($group->getCustomFields() as $customFields) { - $choices[$customFields->getSlug()] = + $choices[$customFields->getSlug()] = $this->translatableStringHelper ->localize($customFields->getName()); } - + $options = array_merge($this->options, array( 'choice_list' => new SimpleChoiceList($choices), )); - - $event->getForm()->getParent()->add($this->choiceName, 'choice', + + $event->getForm()->getParent()->add($this->choiceName, ChoiceType::class, $options); } - + /** * Return the root form (i.e. produced from CustomFieldsGroupType::getForm) - * + * * @param FormInterface $form * @return FormInterface */ @@ -129,7 +130,7 @@ class LinkedCustomFieldsType extends AbstractType return $this->getRootForm($form->getParent()); } } - + public function getName() { return 'custom_fields_group_linked_custom_fields';