|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Avocode\FormExtensionsBundle\Form\Type; |
| 4 | + |
| 5 | +use Avocode\FormExtensionsBundle\Form\DataTransformer\ArrayToStringTransformer; |
| 6 | +use Symfony\Component\Form\AbstractType; |
| 7 | +use Symfony\Component\Form\FormBuilderInterface; |
| 8 | +use Symfony\Component\Form\FormView; |
| 9 | +use Symfony\Component\Form\FormInterface; |
| 10 | +use Symfony\Component\OptionsResolver\OptionsResolverInterface; |
| 11 | +use Symfony\Component\OptionsResolver\Options; |
| 12 | + |
| 13 | +/** |
| 14 | + * Select2Type to JQueryLib |
| 15 | + * |
| 16 | + * @author Bilal Amarni <[email protected]> |
| 17 | + * @author Chris Tickner <[email protected]> |
| 18 | + */ |
| 19 | +class Select2Type extends AbstractType |
| 20 | +{ |
| 21 | + private $widget; |
| 22 | + |
| 23 | + public function __construct($widget) |
| 24 | + { |
| 25 | + $this->widget = $widget; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * {@inheritdoc} |
| 30 | + */ |
| 31 | + public function buildForm(FormBuilderInterface $builder, array $options) |
| 32 | + { |
| 33 | + if ('hidden' === $this->widget && !empty($options['configs']['multiple'])) { |
| 34 | + $builder->addViewTransformer(new ArrayToStringTransformer()); |
| 35 | + } elseif ('hidden' === $this->widget && empty($options['configs']['multiple']) && null !== $options['transformer']) { |
| 36 | + $builder->addModelTransformer($options['transformer']); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * {@inheritdoc} |
| 42 | + */ |
| 43 | + public function buildView(FormView $view, FormInterface $form, array $options) |
| 44 | + { |
| 45 | + $view->vars['configs'] = $options['configs']; |
| 46 | + |
| 47 | + // Adds a custom block prefix |
| 48 | + array_splice( |
| 49 | + $view->vars['block_prefixes'], |
| 50 | + array_search($this->getName(), $view->vars['block_prefixes']), |
| 51 | + 0, |
| 52 | + 'select2' |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * {@inheritdoc} |
| 58 | + */ |
| 59 | + public function setDefaultOptions(OptionsResolverInterface $resolver) |
| 60 | + { |
| 61 | + $defaults = array( |
| 62 | + 'placeholder' => 'Select a value', |
| 63 | + 'allowClear' => false, |
| 64 | + 'minimumInputLength' => 0, |
| 65 | + 'width' => 'element', |
| 66 | + ); |
| 67 | + $resolver |
| 68 | + ->setDefaults(array( |
| 69 | + 'configs' => $defaults, |
| 70 | + 'transformer' => null, |
| 71 | + )) |
| 72 | + ->setNormalizers(array( |
| 73 | + 'configs' => function (Options $options, $configs) use ($defaults) { |
| 74 | + return array_merge($defaults, $configs); |
| 75 | + }, |
| 76 | + )) |
| 77 | + ; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * {@inheritdoc} |
| 82 | + */ |
| 83 | + public function getParent() |
| 84 | + { |
| 85 | + return $this->widget; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * {@inheritdoc} |
| 90 | + */ |
| 91 | + public function getName() |
| 92 | + { |
| 93 | + return 'select2_' . $this->widget; |
| 94 | + } |
| 95 | +} |
0 commit comments