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
{
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)
$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;
}

View File

@ -13,13 +13,21 @@ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
*/
class ChoiceWithOtherType extends AbstractType
{
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;
@ -27,7 +35,6 @@ class ChoiceWithOtherType extends AbstractType
->add('_other', 'text', array('required' => false))
->add('_choices', 'choice', $options)
;
}
/* (non-PHPdoc)
@ -40,10 +47,8 @@ class ChoiceWithOtherType extends AbstractType
->setAllowedTypes(array('choices' => array('array')))
->setDefaults(array('multiple' => false))
;
}
public function getName()
{
return 'choice_with_other';