|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Infinite\FormBundle\Form\Type; |
| 4 | + |
| 5 | +use Symfony\Component\Form\Exception\LogicException; |
| 6 | +use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToBooleanArrayTransformer; |
| 7 | +use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer; |
| 8 | +use Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToBooleanArrayTransformer; |
| 9 | +use Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer; |
| 10 | +use Symfony\Component\Form\Extension\Core\EventListener\FixCheckboxInputListener; |
| 11 | +use Symfony\Component\Form\Extension\Core\EventListener\FixRadioInputListener; |
| 12 | +use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener; |
| 13 | +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
| 14 | +use Symfony\Component\Form\FormBuilderInterface; |
| 15 | +use Symfony\Component\OptionsResolver\Options; |
| 16 | +use Symfony\Component\OptionsResolver\OptionsResolverInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class ChoiceTreeType based on ChoiceType. |
| 20 | + */ |
| 21 | +class ChoiceTreeType extends ChoiceType |
| 22 | +{ |
| 23 | + /** |
| 24 | + * Caches created choice lists. |
| 25 | + * |
| 26 | + * @var array |
| 27 | + */ |
| 28 | + private $treeChoiceListCache = []; |
| 29 | + |
| 30 | + /** |
| 31 | + * {@inheritdoc} |
| 32 | + */ |
| 33 | + public function buildForm(FormBuilderInterface $builder, array $options) |
| 34 | + { |
| 35 | + $choiceList = $options['choice_list']->getAdaptedList(); |
| 36 | + if (!$choiceList && !is_array($options['choices']) && !$options['choices'] instanceof \Traversable) { |
| 37 | + throw new LogicException('Either the option "choices" or "choice_list" must be set.'); |
| 38 | + } |
| 39 | + |
| 40 | + if ($options['expanded']) { |
| 41 | + $preferredViews = $choiceList->getPreferredViews(); |
| 42 | + $remainingViews = $choiceList->getRemainingViews(); |
| 43 | + |
| 44 | + // Check if the choices already contain the empty value |
| 45 | + // Only add the empty value option if this is not the case |
| 46 | + if (null !== $options['placeholder'] && 0 === count($choiceList->getChoicesForValues(['']))) { |
| 47 | + $placeholderView = new TreeChoiceView(null, '', $options['placeholder'], 0); |
| 48 | + |
| 49 | + // "placeholder" is a reserved index |
| 50 | + $this->addSubForms($builder, ['placeholder' => $placeholderView], $options); |
| 51 | + } |
| 52 | + |
| 53 | + $this->addSubForms($builder, $preferredViews, $options); |
| 54 | + $this->addSubForms($builder, $remainingViews, $options); |
| 55 | + |
| 56 | + if ($options['multiple']) { |
| 57 | + $builder->addViewTransformer(new ChoicesToBooleanArrayTransformer($options['choice_list'])); |
| 58 | + $builder->addEventSubscriber(new FixCheckboxInputListener($options['choice_list']), 10); |
| 59 | + } else { |
| 60 | + $builder->addViewTransformer(new ChoiceToBooleanArrayTransformer($options['choice_list'], $builder->has('placeholder'))); |
| 61 | + $builder->addEventSubscriber(new FixRadioInputListener($options['choice_list'], $builder->has('placeholder')), 10); |
| 62 | + } |
| 63 | + } else { |
| 64 | + if ($options['multiple']) { |
| 65 | + $builder->addViewTransformer(new ChoicesToValuesTransformer($options['choice_list'])); |
| 66 | + } else { |
| 67 | + $builder->addViewTransformer(new ChoiceToValueTransformer($options['choice_list'])); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if ($options['multiple'] && $options['by_reference']) { |
| 72 | + // Make sure the collection created during the client->norm |
| 73 | + // transformation is merged back into the original collection |
| 74 | + $builder->addEventSubscriber(new MergeCollectionListener(true, true)); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * {@inheritdoc} |
| 80 | + */ |
| 81 | + public function setDefaultOptions(OptionsResolverInterface $resolver) |
| 82 | + { |
| 83 | + parent::setDefaultOptions($resolver); |
| 84 | + |
| 85 | + //set our custom choice list |
| 86 | + $treeChoiceListCache = &$this->treeChoiceListCache; |
| 87 | + |
| 88 | + $treeChoiceList = function (Options $options) use (&$treeChoiceListCache) { |
| 89 | + // Harden against NULL values (like in EntityType and ModelType) |
| 90 | + $choices = null !== $options['choices'] ? $options['choices'] : []; |
| 91 | + |
| 92 | + // Reuse existing choice lists in order to increase performance |
| 93 | + $hash = hash('sha256', serialize([$choices, $options['preferred_choices']])); |
| 94 | + |
| 95 | + if (!isset($treeChoiceListCache[$hash])) { |
| 96 | + $treeChoiceListCache[$hash] = new TreeChoiceList($choices, $options['preferred_choices']); |
| 97 | + } |
| 98 | + |
| 99 | + return $treeChoiceListCache[$hash]; |
| 100 | + }; |
| 101 | + |
| 102 | + $resolver->setDefaults(['choice_list' => $treeChoiceList]); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * {@inheritdoc} |
| 107 | + */ |
| 108 | + public function getName() |
| 109 | + { |
| 110 | + return 'infinite_form_choice_tree'; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Adds the sub fields for an expanded choice field. |
| 115 | + * |
| 116 | + * @param FormBuilderInterface $builder The form builder. |
| 117 | + * @param array $choiceViews The choice view objects. |
| 118 | + * @param array $options The build options. |
| 119 | + */ |
| 120 | + private function addSubForms(FormBuilderInterface $builder, array $choiceViews, array $options) |
| 121 | + { |
| 122 | + foreach ($choiceViews as $i => $choiceView) { |
| 123 | + if (is_array($choiceView)) { |
| 124 | + // Flatten groups |
| 125 | + $this->addSubForms($builder, $choiceView, $options); |
| 126 | + } else { |
| 127 | + $choiceOpts = [ |
| 128 | + 'value' => $choiceView->value, |
| 129 | + 'label' => $choiceView->label, |
| 130 | + 'level' => $choiceView->level, |
| 131 | + 'translation_domain' => $options['translation_domain'], |
| 132 | + 'block_name' => 'entry', |
| 133 | + ]; |
| 134 | + |
| 135 | + if ($options['multiple']) { |
| 136 | + $choiceType = 'infinite_form_checkbox_level'; |
| 137 | + // The user can check 0 or more checkboxes. If required |
| 138 | + // is true, he is required to check all of them. |
| 139 | + $choiceOpts['required'] = false; |
| 140 | + } else { |
| 141 | + $choiceType = 'infinite_form_radio_level'; |
| 142 | + } |
| 143 | + $builder->add($i, $choiceType, $choiceOpts); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments