diff --git a/CustomFields/CustomFieldLongChoice.php b/CustomFields/CustomFieldLongChoice.php index 4d9f6400e..ac37bf2be 100644 --- a/CustomFields/CustomFieldLongChoice.php +++ b/CustomFields/CustomFieldLongChoice.php @@ -26,9 +26,10 @@ use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option; use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer; use Symfony\Bridge\Twig\TwigEngine; +use Chill\MainBundle\Form\Type\Select2ChoiceType; /** - * + * * * @author Julien Fastré */ @@ -39,29 +40,29 @@ class CustomFieldLongChoice extends AbstractCustomField * @var OptionRepository */ private $optionRepository; - + /** * * @var TranslatableStringHelper */ private $translatableStringHelper; - + /** * @var TwigEngine */ private $templating; - + const KEY = 'key'; - + public function __construct(OptionRepository $optionRepository, - TranslatableStringHelper $translatableStringHelper, + TranslatableStringHelper $translatableStringHelper, TwigEngine $twigEngine) { $this->optionRepository = $optionRepository; $this->translatableStringHelper = $translatableStringHelper; $this->templating = $twigEngine; } - + public function buildForm(FormBuilderInterface $builder, CustomField $customField) { $options = $customField->getOptions(); @@ -69,7 +70,7 @@ class CustomFieldLongChoice extends AbstractCustomField false, true); //create a local copy of translatable string helper $translatableStringHelper = $this->translatableStringHelper; - $builder->add($customField->getSlug(), 'select2_choice', array( + $builder->add($customField->getSlug(), Select2ChoiceType::class, array( 'choices' => $entries, 'choice_label' => function(Option $option) use ($translatableStringHelper) { return $translatableStringHelper->localize($option->getText()); @@ -96,7 +97,7 @@ class CustomFieldLongChoice extends AbstractCustomField )); $builder->get($customField->getSlug()) ->addModelTransformer(new CustomFieldDataTransformer($this, $customField)); - + } public function buildOptionsForm(FormBuilderInterface $builder) @@ -107,12 +108,12 @@ class CustomFieldLongChoice extends AbstractCustomField foreach ($keys as $key) { $choices[$key] = $key; } - + return $builder->add(self::KEY, 'choice', array( 'choices' => $choices, 'label' => 'Options key' )); - + } public function deserialize($serialized, \Chill\CustomFieldsBundle\Entity\CustomField $customField) @@ -120,8 +121,8 @@ class CustomFieldLongChoice extends AbstractCustomField if ($serialized === NULL) { return NULL; } - - + + return $this->optionRepository->find($serialized); } @@ -135,7 +136,7 @@ class CustomFieldLongChoice extends AbstractCustomField $option = $this->deserialize($value, $customField); $template = 'ChillCustomFieldsBundle:CustomFieldsRendering:choice_long.' .$documentType.'.twig'; - + return $this->templating ->render($template, array( 'values' => $option === NULL ? array() : array($option) @@ -147,13 +148,13 @@ class CustomFieldLongChoice extends AbstractCustomField if ($value === NULL) { return NULL; } - + if (!$value instanceof Option) { throw new \LogicException('the value should be an instance of ' . 'Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option, ' . is_object($value) ? get_class($value) : gettype($value).' given'); } - + // we place the id in array, to allow in the future multiple select return $value->getId(); }