optionRepository = $optionRepository; $this->translatableStringHelper = $translatableStringHelper; $this->templating = $twigEngine; } public function buildForm(FormBuilderInterface $builder, CustomField $customField) { $options = $customField->getOptions(); $entries = $this->optionRepository->findFilteredByKey( $options[self::KEY], false, true ); //create a local copy of translatable string helper $translatableStringHelper = $this->translatableStringHelper; $builder->add($customField->getSlug(), Select2ChoiceType::class, [ 'choices' => $entries, 'choice_label' => static fn (Option $option) => $translatableStringHelper->localize($option->getText()), 'choice_value' => static fn (Option $key): ?int => null === $key ? null : $key->getId(), 'multiple' => false, 'expanded' => false, 'required' => $customField->isRequired(), 'placeholder' => 'Choose a value', 'group_by' => static function (Option $option) use ($translatableStringHelper) { if ($option->hasParent()) { return $translatableStringHelper->localize($option->getParent()->getText()); } return $translatableStringHelper->localize($option->getText()); }, 'label' => $translatableStringHelper->localize($customField->getName()), ]); $builder ->get($customField->getSlug()) ->addModelTransformer(new CustomFieldDataTransformer($this, $customField)); } public function buildOptionsForm(FormBuilderInterface $builder) { //create a selector between different keys $keys = $this->optionRepository->getKeys(); $choices = []; foreach ($keys as $key) { $choices[$key] = $key; } return $builder->add(self::KEY, ChoiceType::class, [ 'choices' => array_combine(array_values($choices), array_keys($choices)), 'label' => 'Options key', ]); } public function deserialize($serialized, CustomField $customField) { if (null === $serialized) { return null; } return $this->optionRepository->find($serialized); } public function getName() { return 'Long choice field'; } public function render($value, CustomField $customField, $documentType = 'html') { $option = $this->deserialize($value, $customField); $template = 'ChillCustomFieldsBundle:CustomFieldsRendering:choice_long.' . $documentType . '.twig'; return $this->templating ->render($template, [ 'values' => null === $option ? [] : [$option], ]); } public function serialize($value, CustomField $customField) { if (null === $value) { 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(); } }