Allow to customize the 'other value' statement in CFChoice - close #361

This commit is contained in:
Marc Ducobu 2014-11-26 18:49:24 +01:00
parent a8831056d2
commit b5bf383005
2 changed files with 48 additions and 26 deletions

View File

@ -40,6 +40,7 @@ use Chill\MainBundle\Templating\TranslatableStringHelper;
class CustomFieldChoice implements CustomFieldInterface class CustomFieldChoice implements CustomFieldInterface
{ {
const ALLOW_OTHER = 'other'; const ALLOW_OTHER = 'other';
const OTHER_VALUE_LABEL = 'otherValueLabel';
const MULTIPLE = 'multiple'; const MULTIPLE = 'multiple';
const EXPANDED = 'expanded'; const EXPANDED = 'expanded';
const CHOICES = 'choices'; const CHOICES = 'choices';
@ -77,7 +78,9 @@ class CustomFieldChoice implements CustomFieldInterface
//prepare choices //prepare choices
$locale = $this->requestStack->getCurrentRequest()->getLocale(); $locale = $this->requestStack->getCurrentRequest()->getLocale();
$choices = array(); $choices = array();
foreach($customField->getOptions()[self::CHOICES] as $persistedChoices) { $customFieldOptions = $customField->getOptions();
foreach($customFieldOptions[self::CHOICES] as $persistedChoices) {
if ($persistedChoices['active']){ if ($persistedChoices['active']){
$choices[$persistedChoices['slug']] = $this->translatableStringHelper->localize($persistedChoices['name']); $choices[$persistedChoices['slug']] = $this->translatableStringHelper->localize($persistedChoices['name']);
} }
@ -85,21 +88,32 @@ class CustomFieldChoice implements CustomFieldInterface
//prepare $options //prepare $options
$options = array( $options = array(
'multiple' => $customField->getOptions()[self::MULTIPLE], 'multiple' => $customFieldOptions[self::MULTIPLE],
'choices' => $choices, 'choices' => $choices,
'required' => false, 'required' => false,
'label' => $this->translatableStringHelper->localize($customField->getName()) 'label' => $this->translatableStringHelper->localize($customField->getName())
); );
//if allow_other = true //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->add(
$builder->create($customField->getSlug(), new ChoiceWithOtherType(), $options) $builder
->addModelTransformer(new CustomFieldDataTransformer($this, $customField))); ->create(
$customField->getSlug(),
new ChoiceWithOtherType($otherValueLabel),
$options)
->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'] = $customField->getOptions()[self::EXPANDED]; $options['expanded'] = $customFieldOptions[self::EXPANDED];
$builder->add( $builder->add(
$builder->create($customField->getSlug(), 'choice', $options) $builder->create($customField->getSlug(), 'choice', $options)
@ -140,10 +154,13 @@ class CustomFieldChoice implements CustomFieldInterface
'expanded' => true, 'expanded' => true,
'multiple' => false '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( ->add(self::CHOICES, new ChoicesType(), array(
'type' => new ChoicesListType($this->defaultLocale), 'type' => new ChoicesListType($this->defaultLocale),
'allow_add' => true 'allow_add' => true
)); ))
;
return $builder; return $builder;
} }

View File

@ -13,36 +13,41 @@ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
*/ */
class ChoiceWithOtherType extends AbstractType class ChoiceWithOtherType extends AbstractType
{ {
/* (non-PHPdoc) private $otherValueLabel = 'Other value';
* @see \Symfony\Component\Form\AbstractType::buildForm()
*/ public function __construct($otherValueLabel = Null) {
public function buildForm(FormBuilderInterface $builder, array $options) 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 //add an 'other' entry in choices array
$options['choices']['_other'] = 'Other value'; $options['choices']['_other'] = $this->otherValueLabel;
//ChoiceWithOther must always be expanded //ChoiceWithOther must always be expanded
$options['expanded'] = true; $options['expanded'] = true;
$builder $builder
->add('_other', 'text', array('required' => false)) ->add('_other', 'text', array('required' => false))
->add('_choices', 'choice', $options) ->add('_choices', 'choice', $options)
; ;
}
} /* (non-PHPdoc)
* @see \Symfony\Component\Form\AbstractType::setDefaultOptions()
/* (non-PHPdoc) */
* @see \Symfony\Component\Form\AbstractType::setDefaultOptions() public function setDefaultOptions(OptionsResolverInterface $resolver)
*/ {
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver $resolver
->setRequired(array('choices')) ->setRequired(array('choices'))
->setAllowedTypes(array('choices' => array('array'))) ->setAllowedTypes(array('choices' => array('array')))
->setDefaults(array('multiple' => false)) ->setDefaults(array('multiple' => false))
; ;
}
}
public function getName() public function getName()
{ {