From b5bf3830056ac6b268d892b62be4ea25cff3d1cf Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Wed, 26 Nov 2014 18:49:24 +0100 Subject: [PATCH] Allow to customize the 'other value' statement in CFChoice - close #361 --- CustomFields/CustomFieldChoice.php | 33 ++++++++++++++++++------ Form/Type/ChoiceWithOtherType.php | 41 +++++++++++++++++------------- 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/CustomFields/CustomFieldChoice.php b/CustomFields/CustomFieldChoice.php index af030f1da..ca4af6a9e 100644 --- a/CustomFields/CustomFieldChoice.php +++ b/CustomFields/CustomFieldChoice.php @@ -40,6 +40,7 @@ use Chill\MainBundle\Templating\TranslatableStringHelper; class CustomFieldChoice implements CustomFieldInterface { const ALLOW_OTHER = 'other'; + const OTHER_VALUE_LABEL = 'otherValueLabel'; const MULTIPLE = 'multiple'; const EXPANDED = 'expanded'; const CHOICES = 'choices'; @@ -77,7 +78,9 @@ class CustomFieldChoice implements CustomFieldInterface //prepare choices $locale = $this->requestStack->getCurrentRequest()->getLocale(); $choices = array(); - foreach($customField->getOptions()[self::CHOICES] as $persistedChoices) { + $customFieldOptions = $customField->getOptions(); + + foreach($customFieldOptions[self::CHOICES] as $persistedChoices) { if ($persistedChoices['active']){ $choices[$persistedChoices['slug']] = $this->translatableStringHelper->localize($persistedChoices['name']); } @@ -85,21 +88,32 @@ class CustomFieldChoice implements CustomFieldInterface //prepare $options $options = array( - 'multiple' => $customField->getOptions()[self::MULTIPLE], + 'multiple' => $customFieldOptions[self::MULTIPLE], 'choices' => $choices, 'required' => false, 'label' => $this->translatableStringHelper->localize($customField->getName()) ); - + //if allow_other = true - if ($customField->getOptions()[self::ALLOW_OTHER] == true) { + if ($customFieldOptions[self::ALLOW_OTHER] == true) { + $otherValueLabel = null; + if(array_key_exists(self::OTHER_VALUE_LABEL, $customFieldOptions)) { + $otherValueLabel = $this->translatableStringHelper->localize( + $customFieldOptions[self::OTHER_VALUE_LABEL] + ); + } + $builder->add( - $builder->create($customField->getSlug(), new ChoiceWithOtherType(), $options) - ->addModelTransformer(new CustomFieldDataTransformer($this, $customField))); + $builder + ->create( + $customField->getSlug(), + new ChoiceWithOtherType($otherValueLabel), + $options) + ->addModelTransformer(new CustomFieldDataTransformer($this, $customField))); } else { //if allow_other = false //we add the 'expanded' to options - $options['expanded'] = $customField->getOptions()[self::EXPANDED]; + $options['expanded'] = $customFieldOptions[self::EXPANDED]; $builder->add( $builder->create($customField->getSlug(), 'choice', $options) @@ -140,10 +154,13 @@ class CustomFieldChoice implements CustomFieldInterface 'expanded' => true, 'multiple' => false )) + ->add(self::OTHER_VALUE_LABEL, 'translatable_string', array( + 'label' => 'Other value label (empty if use by default)')) ->add(self::CHOICES, new ChoicesType(), array( 'type' => new ChoicesListType($this->defaultLocale), 'allow_add' => true - )); + )) + ; return $builder; } diff --git a/Form/Type/ChoiceWithOtherType.php b/Form/Type/ChoiceWithOtherType.php index 6ef480ae0..f2c061125 100644 --- a/Form/Type/ChoiceWithOtherType.php +++ b/Form/Type/ChoiceWithOtherType.php @@ -13,36 +13,41 @@ use Symfony\Component\OptionsResolver\OptionsResolverInterface; */ class ChoiceWithOtherType extends AbstractType { - /* (non-PHPdoc) - * @see \Symfony\Component\Form\AbstractType::buildForm() - */ - public function buildForm(FormBuilderInterface $builder, array $options) - { + private $otherValueLabel = 'Other value'; + + public function __construct($otherValueLabel = Null) { + if($otherValueLabel) { + $this->otherValueLabel = $otherValueLabel; + } + } + + /* (non-PHPdoc) + * @see \Symfony\Component\Form\AbstractType::buildForm() + */ + public function buildForm(FormBuilderInterface $builder, array $options) + { //add an 'other' entry in choices array - $options['choices']['_other'] = 'Other value'; + $options['choices']['_other'] = $this->otherValueLabel; //ChoiceWithOther must always be expanded $options['expanded'] = true; $builder ->add('_other', 'text', array('required' => false)) ->add('_choices', 'choice', $options) - ; - - } + ; + } - /* (non-PHPdoc) - * @see \Symfony\Component\Form\AbstractType::setDefaultOptions() - */ - public function setDefaultOptions(OptionsResolverInterface $resolver) - { + /* (non-PHPdoc) + * @see \Symfony\Component\Form\AbstractType::setDefaultOptions() + */ + public function setDefaultOptions(OptionsResolverInterface $resolver) + { $resolver ->setRequired(array('choices')) ->setAllowedTypes(array('choices' => array('array'))) ->setDefaults(array('multiple' => false)) - ; - - } - + ; + } public function getName() {