ModelAutocompleteType.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Form\Type;
  11. use Sonata\AdminBundle\Form\DataTransformer\ModelToIdPropertyTransformer;
  12. use Symfony\Component\Form\AbstractType;
  13. use Symfony\Component\Form\Extension\Core\EventListener\ResizeFormListener;
  14. use Symfony\Component\Form\FormBuilderInterface;
  15. use Symfony\Component\Form\FormInterface;
  16. use Symfony\Component\Form\FormView;
  17. use Symfony\Component\OptionsResolver\Options;
  18. use Symfony\Component\OptionsResolver\OptionsResolver;
  19. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  20. /**
  21. * This type defines a standard text field with autocomplete feature.
  22. *
  23. * @author Andrej Hudec <pulzarraider@gmail.com>
  24. * @author Florent Denis <dflorent.pokap@gmail.com>
  25. */
  26. class ModelAutocompleteType extends AbstractType
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function buildForm(FormBuilderInterface $builder, array $options)
  32. {
  33. $builder->addViewTransformer(new ModelToIdPropertyTransformer($options['model_manager'], $options['class'], $options['property'], $options['multiple'], $options['to_string_callback']), true);
  34. $builder->setAttribute('property', $options['property']);
  35. $builder->setAttribute('callback', $options['callback']);
  36. $builder->setAttribute('minimum_input_length', $options['minimum_input_length']);
  37. $builder->setAttribute('items_per_page', $options['items_per_page']);
  38. $builder->setAttribute('req_param_name_page_number', $options['req_param_name_page_number']);
  39. $builder->setAttribute(
  40. 'disabled',
  41. $options['disabled']
  42. // NEXT_MAJOR: Remove this when bumping Symfony constraint to 2.8+
  43. || (array_key_exists('read_only', $options) && $options['read_only'])
  44. );
  45. $builder->setAttribute('to_string_callback', $options['to_string_callback']);
  46. $builder->setAttribute('target_admin_access_action', $options['target_admin_access_action']);
  47. if ($options['multiple']) {
  48. $resizeListener = new ResizeFormListener(
  49. 'hidden', array(), true, true, true
  50. );
  51. $builder->addEventSubscriber($resizeListener);
  52. }
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function buildView(FormView $view, FormInterface $form, array $options)
  58. {
  59. $view->vars['admin_code'] = $options['admin_code'];
  60. $view->vars['placeholder'] = $options['placeholder'];
  61. $view->vars['multiple'] = $options['multiple'];
  62. $view->vars['minimum_input_length'] = $options['minimum_input_length'];
  63. $view->vars['items_per_page'] = $options['items_per_page'];
  64. $view->vars['width'] = $options['width'];
  65. // ajax parameters
  66. $view->vars['url'] = $options['url'];
  67. $view->vars['route'] = $options['route'];
  68. $view->vars['req_params'] = $options['req_params'];
  69. $view->vars['req_param_name_search'] = $options['req_param_name_search'];
  70. $view->vars['req_param_name_page_number'] = $options['req_param_name_page_number'];
  71. $view->vars['req_param_name_items_per_page'] = $options['req_param_name_items_per_page'];
  72. $view->vars['quiet_millis'] = $options['quiet_millis'];
  73. $view->vars['cache'] = $options['cache'];
  74. // CSS classes
  75. $view->vars['container_css_class'] = $options['container_css_class'];
  76. $view->vars['dropdown_css_class'] = $options['dropdown_css_class'];
  77. $view->vars['dropdown_item_css_class'] = $options['dropdown_item_css_class'];
  78. $view->vars['dropdown_auto_width'] = $options['dropdown_auto_width'];
  79. // template
  80. $view->vars['template'] = $options['template'];
  81. $view->vars['context'] = $options['context'];
  82. }
  83. /**
  84. * NEXT_MAJOR: Remove method, when bumping requirements to SF 2.7+.
  85. *
  86. * {@inheritdoc}
  87. */
  88. public function setDefaultOptions(OptionsResolverInterface $resolver)
  89. {
  90. $this->configureOptions($resolver);
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function configureOptions(OptionsResolver $resolver)
  96. {
  97. $compound = function (Options $options) {
  98. return $options['multiple'];
  99. };
  100. $resolver->setDefaults(array(
  101. 'attr' => array(),
  102. 'compound' => $compound,
  103. 'model_manager' => null,
  104. 'class' => null,
  105. 'admin_code' => null,
  106. 'callback' => null,
  107. 'multiple' => false,
  108. 'width' => '',
  109. 'context' => '',
  110. 'placeholder' => '',
  111. 'minimum_input_length' => 3, //minimum 3 chars should be typed to load ajax data
  112. 'items_per_page' => 10, //number of items per page
  113. 'quiet_millis' => 100,
  114. 'cache' => false,
  115. 'to_string_callback' => null,
  116. // ajax parameters
  117. 'url' => '',
  118. 'route' => array('name' => 'sonata_admin_retrieve_autocomplete_items', 'parameters' => array()),
  119. 'req_params' => array(),
  120. 'req_param_name_search' => 'q',
  121. 'req_param_name_page_number' => '_page',
  122. 'req_param_name_items_per_page' => '_per_page',
  123. // security
  124. 'target_admin_access_action' => 'list',
  125. // CSS classes
  126. 'container_css_class' => '',
  127. 'dropdown_css_class' => '',
  128. 'dropdown_item_css_class' => '',
  129. 'dropdown_auto_width' => false,
  130. 'template' => 'SonataAdminBundle:Form/Type:sonata_type_model_autocomplete.html.twig',
  131. ));
  132. $resolver->setRequired(array('property'));
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function getBlockPrefix()
  138. {
  139. return 'sonata_type_model_autocomplete';
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function getName()
  145. {
  146. return $this->getBlockPrefix();
  147. }
  148. }