Skip to content

Commit 4632645

Browse files
authored
Merge pull request #152 from DubbleClick/master
Allow extra parameters to be sent with the query
2 parents 1d4b184 + 6370073 commit 4632645

File tree

8 files changed

+178
-126
lines changed

8 files changed

+178
-126
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ composer.phar
1717

1818
#PhpStorm
1919
.idea
20+
21+
#Visual Studio
22+
.vs
23+
*.phpproj
24+
*.sln

DependencyInjection/Configuration.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Symfony\Component\Config\Definition\ConfigurationInterface;
77

88
/**
9-
* This is the class that validates and merges configuration from your app/config files
9+
* This is the class that validates and merges configuration from your config/packages files
1010
*
1111
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
1212
*/
@@ -18,37 +18,31 @@ class Configuration implements ConfigurationInterface
1818
public function getConfigTreeBuilder()
1919
{
2020
$treeBuilder = new TreeBuilder('tetranz_select2_entity');
21-
$rootNode = \method_exists($treeBuilder, 'getRootNode') ? $treeBuilder->getRootNode() : $treeBuilder->root('tetranz_select2_entity');
21+
$rootNode = $treeBuilder->getRootNode();
2222

2323
$rootNode
2424
->children()
25-
->scalarNode('minimum_input_length')->defaultValue(1)->end()
26-
->scalarNode('scroll')->defaultFalse()->end()
27-
->scalarNode('page_limit')->defaultValue(10)->end()
28-
->scalarNode('allow_clear')->defaultFalse()->end()
25+
->integerNode('minimum_input_length')->min(0)->defaultValue(1)->end()
26+
->booleanNode('scroll')->defaultFalse()->end()
27+
->integerNode('page_limit')->defaultValue(10)->end()
28+
->booleanNode('allow_clear')->defaultFalse()->end()
2929
->arrayNode('allow_add')->addDefaultsIfNotSet()
3030
->children()
31-
->scalarNode('enabled')->defaultFalse()->end()
31+
->booleanNode('enabled')->defaultFalse()->end()
3232
->scalarNode('new_tag_text')->defaultValue(' (NEW)')->end()
3333
->scalarNode('new_tag_prefix')->defaultValue('__')->end()
3434
->scalarNode('tag_separators')->defaultValue('[",", " "]')->end()
3535
->end()
3636
->end()
37-
->scalarNode('delay')->defaultValue(250)->end()
37+
->integerNode('delay')->defaultValue(250)->min(0)->end()
3838
->scalarNode('language')->defaultValue('en')->end()
39-
->scalarNode('cache')->defaultTrue()->end()
40-
// default to 1ms for backwards compatibility for older versions where 'cache' is true but the
41-
// user is not aware of the updated caching feature. This way the cache will, by default, not
42-
// be very effective. Realistically this should be like 60000ms (60 seconds).
43-
->scalarNode('cache_timeout')->defaultValue(1)->end()
39+
->booleanNode('cache')->defaultTrue()->end()
40+
->integerNode('cache_timeout')->defaultValue(60000)->min(0)->end()
4441
->scalarNode('width')->defaultNull()->end()
45-
->scalarNode('object_manager')->defaultValue(1)->end()
42+
->scalarNode('object_manager')->defaultNull()->end()
43+
->booleanNode('render_html')->defaultFalse()->end()
4644
->end();
4745

48-
// Here you should define the parameters that are allowed to
49-
// configure your bundle. See the documentation linked above for
50-
// more information on that topic.
51-
5246
return $treeBuilder;
5347
}
5448
}

Form/Type/Select2EntityType.php

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
namespace Tetranz\Select2EntityBundle\Form\Type;
44

5+
use Tetranz\Select2EntityBundle\Form\DataTransformer\EntitiesToPropertyTransformer;
6+
use Tetranz\Select2EntityBundle\Form\DataTransformer\EntityToPropertyTransformer;
7+
58
use Doctrine\Common\Persistence\ObjectManager;
9+
use Doctrine\Common\Persistence\ManagerRegistry;
610
use Symfony\Component\Form\AbstractType;
711
use Symfony\Component\Form\DataTransformerInterface;
812
use Symfony\Component\Form\FormBuilderInterface;
913
use Symfony\Component\Form\FormInterface;
1014
use Symfony\Component\Form\FormView;
1115
use Symfony\Component\OptionsResolver\OptionsResolver;
12-
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
1316
use Symfony\Component\Routing\RouterInterface;
14-
use Tetranz\Select2EntityBundle\Form\DataTransformer\EntitiesToPropertyTransformer;
15-
use Tetranz\Select2EntityBundle\Form\DataTransformer\EntityToPropertyTransformer;
1617
use Symfony\Component\PropertyAccess\PropertyAccess;
1718

1819
/**
@@ -22,35 +23,50 @@
2223
*/
2324
class Select2EntityType extends AbstractType
2425
{
26+
/** @var ManagerRegistry */
27+
protected $registry;
2528
/** @var ObjectManager */
2629
protected $em;
2730
/** @var RouterInterface */
2831
protected $router;
29-
/** @var array */
32+
/** @var array */
3033
protected $config;
3134

3235
/**
33-
* @param ObjectManager $em
34-
* @param RouterInterface $router
35-
* @param array $config
36+
* @param ManagerRegistry $registry
37+
* @param RouterInterface $router
38+
* @param array $config
3639
*/
37-
public function __construct(ObjectManager $em, RouterInterface $router, $config)
40+
public function __construct(ManagerRegistry $registry, RouterInterface $router, $config)
3841
{
39-
$this->em = $em;
42+
$this->registry = $registry;
43+
$this->em = $registry->getManager();
4044
$this->router = $router;
4145
$this->config = $config;
4246
}
4347

4448
public function buildForm(FormBuilderInterface $builder, array $options)
4549
{
4650
// custom object manager for this entity, override the default entity manager ?
47-
if(isset($options['object_manager'])) {
51+
if (isset($options['object_manager'])) {
4852
$em = $options['object_manager'];
49-
if(!$em instanceof ObjectManager) {
53+
if (!$em instanceof ObjectManager) {
5054
throw new \Exception('The entity manager \'em\' must be an ObjectManager instance');
5155
}
5256
// Use the custom manager instead.
5357
$this->em = $em;
58+
} else if (isset($this->config['object_manager'])) {
59+
$em = $this->registry->getManager($this->config['object_manager']);
60+
if (!$em instanceof ObjectManager) {
61+
throw new \Exception('The entity manager \'em\' must be an ObjectManager instance');
62+
}
63+
$this->em = $em;
64+
}
65+
else {
66+
$manager = $this->registry->getManagerForClass($options['class']);
67+
if ($manager instanceof ObjectManager) {
68+
$this->em = $manager;
69+
}
5470
}
5571

5672
// add custom data transformer
@@ -70,9 +86,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
7086

7187
// add the default data transformer
7288
} else {
73-
74-
$newTagPrefix = isset($options['allow_add']['new_tag_prefix']) ? $options['allow_add']['new_tag_prefix'] : $this->config['allow_add']['new_tag_prefix'];
75-
$newTagText = isset($options['allow_add']['new_tag_text']) ? $options['allow_add']['new_tag_text'] : $this->config['allow_add']['new_tag_text'];
89+
$newTagPrefix = $options['allow_add']['new_tag_prefix'] ?? $this->config['allow_add']['new_tag_prefix'];
90+
$newTagText = $options['allow_add']['new_tag_text'] ?? $this->config['allow_add']['new_tag_text'];
7691

7792
$transformer = $options['multiple']
7893
? new EntitiesToPropertyTransformer($this->em, $options['class'], $options['text_property'], $options['primary_key'], $newTagPrefix, $newTagText)
@@ -87,10 +102,10 @@ public function finishView(FormView $view, FormInterface $form, array $options)
87102
parent::finishView($view, $form, $options);
88103
// make variables available to the view
89104
$view->vars['remote_path'] = $options['remote_path']
90-
?: $this->router->generate($options['remote_route'], array_merge($options['remote_params'], [ 'page_limit' => $options['page_limit'] ]));
105+
?: $this->router->generate($options['remote_route'], array_merge($options['remote_params'], ['page_limit' => $options['page_limit'] ]));
91106

92107
// merge variable names which are only set per instance with those from yml config
93-
$varNames = array_merge(array('multiple', 'placeholder', 'primary_key', 'autostart'), array_keys($this->config));
108+
$varNames = array_merge(['multiple', 'placeholder', 'primary_key', 'autostart', 'query_parameters'], array_keys($this->config));
94109
foreach ($varNames as $varName) {
95110
$view->vars[$varName] = $options[$varName];
96111
}
@@ -109,11 +124,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
109124
//tags options
110125
$varNames = array_keys($this->config['allow_add']);
111126
foreach ($varNames as $varName) {
112-
if (isset($options['allow_add'][$varName])) {
113-
$view->vars['allow_add'][$varName] = $options['allow_add'][$varName];
114-
} else {
115-
$view->vars['allow_add'][$varName] = $this->config['allow_add'][$varName];
116-
}
127+
$view->vars['allow_add'][$varName] = $options['allow_add'][$varName] ?? $this->config['allow_add'][$varName];
117128
}
118129

119130
if ($options['multiple']) {
@@ -123,73 +134,52 @@ public function finishView(FormView $view, FormInterface $form, array $options)
123134
$view->vars['class_type'] = $options['class_type'];
124135
}
125136

126-
/**
127-
* Added for pre Symfony 2.7 compatibility
128-
*
129-
* @param OptionsResolverInterface $resolver
130-
*/
131-
public function setDefaultOptions(OptionsResolverInterface $resolver)
132-
{
133-
$this->configureOptions($resolver);
134-
}
135-
136137
/**
137138
* @param OptionsResolver $resolver
138139
*/
139140
public function configureOptions(OptionsResolver $resolver)
140141
{
141-
$resolver->setDefaults(
142-
array(
143-
'object_manager'=> null,
142+
$resolver->setDefaults([
143+
'object_manager' => null,
144144
'class' => null,
145145
'data_class' => null,
146146
'primary_key' => 'id',
147147
'remote_path' => null,
148148
'remote_route' => null,
149-
'remote_params' => array(),
149+
'remote_params' => [],
150150
'multiple' => false,
151151
'compound' => false,
152152
'minimum_input_length' => $this->config['minimum_input_length'],
153153
'page_limit' => $this->config['page_limit'],
154154
'scroll' => $this->config['scroll'],
155155
'allow_clear' => $this->config['allow_clear'],
156-
'allow_add' => array(
156+
'allow_add' => [
157157
'enabled' => $this->config['allow_add']['enabled'],
158158
'new_tag_text' => $this->config['allow_add']['new_tag_text'],
159159
'new_tag_prefix' => $this->config['allow_add']['new_tag_prefix'],
160160
'tag_separators' => $this->config['allow_add']['tag_separators']
161-
),
161+
],
162162
'delay' => $this->config['delay'],
163163
'text_property' => null,
164-
'placeholder' => '',
164+
'placeholder' => false,
165165
'language' => $this->config['language'],
166166
'required' => false,
167167
'cache' => $this->config['cache'],
168168
'cache_timeout' => $this->config['cache_timeout'],
169169
'transformer' => null,
170170
'autostart' => true,
171-
'width' => isset($this->config['width']) ? $this->config['width'] : null,
172-
'req_params' => array(),
171+
'width' => $this->config['width'] ?? null,
172+
'req_params' => [],
173173
'property' => null,
174174
'callback' => null,
175175
'class_type' => null,
176-
)
176+
'query_parameters' => [],
177+
'render_html' => $this->config['render_html'] ?? false
178+
]
177179
);
178180
}
179181

180182
/**
181-
* pre Symfony 3 compatibility
182-
*
183-
* @return string
184-
*/
185-
public function getName()
186-
{
187-
return $this->getBlockPrefix();
188-
}
189-
190-
/**
191-
* Symfony 2.8+
192-
*
193183
* @return string
194184
*/
195185
public function getBlockPrefix()

0 commit comments

Comments
 (0)