Skip to content

Commit

Permalink
Merge pull request #152 from DubbleClick/master
Browse files Browse the repository at this point in the history
Allow extra parameters to be sent with the query
  • Loading branch information
tetranz authored Sep 23, 2019
2 parents 1d4b184 + 6370073 commit 4632645
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 126 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ composer.phar

#PhpStorm
.idea

#Visual Studio
.vs
*.phpproj
*.sln
30 changes: 12 additions & 18 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Symfony\Component\Config\Definition\ConfigurationInterface;

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

$rootNode
->children()
->scalarNode('minimum_input_length')->defaultValue(1)->end()
->scalarNode('scroll')->defaultFalse()->end()
->scalarNode('page_limit')->defaultValue(10)->end()
->scalarNode('allow_clear')->defaultFalse()->end()
->integerNode('minimum_input_length')->min(0)->defaultValue(1)->end()
->booleanNode('scroll')->defaultFalse()->end()
->integerNode('page_limit')->defaultValue(10)->end()
->booleanNode('allow_clear')->defaultFalse()->end()
->arrayNode('allow_add')->addDefaultsIfNotSet()
->children()
->scalarNode('enabled')->defaultFalse()->end()
->booleanNode('enabled')->defaultFalse()->end()
->scalarNode('new_tag_text')->defaultValue(' (NEW)')->end()
->scalarNode('new_tag_prefix')->defaultValue('__')->end()
->scalarNode('tag_separators')->defaultValue('[",", " "]')->end()
->end()
->end()
->scalarNode('delay')->defaultValue(250)->end()
->integerNode('delay')->defaultValue(250)->min(0)->end()
->scalarNode('language')->defaultValue('en')->end()
->scalarNode('cache')->defaultTrue()->end()
// default to 1ms for backwards compatibility for older versions where 'cache' is true but the
// user is not aware of the updated caching feature. This way the cache will, by default, not
// be very effective. Realistically this should be like 60000ms (60 seconds).
->scalarNode('cache_timeout')->defaultValue(1)->end()
->booleanNode('cache')->defaultTrue()->end()
->integerNode('cache_timeout')->defaultValue(60000)->min(0)->end()
->scalarNode('width')->defaultNull()->end()
->scalarNode('object_manager')->defaultValue(1)->end()
->scalarNode('object_manager')->defaultNull()->end()
->booleanNode('render_html')->defaultFalse()->end()
->end();

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.

return $treeBuilder;
}
}
96 changes: 43 additions & 53 deletions Form/Type/Select2EntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

namespace Tetranz\Select2EntityBundle\Form\Type;

use Tetranz\Select2EntityBundle\Form\DataTransformer\EntitiesToPropertyTransformer;
use Tetranz\Select2EntityBundle\Form\DataTransformer\EntityToPropertyTransformer;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Routing\RouterInterface;
use Tetranz\Select2EntityBundle\Form\DataTransformer\EntitiesToPropertyTransformer;
use Tetranz\Select2EntityBundle\Form\DataTransformer\EntityToPropertyTransformer;
use Symfony\Component\PropertyAccess\PropertyAccess;

/**
Expand All @@ -22,35 +23,50 @@
*/
class Select2EntityType extends AbstractType
{
/** @var ManagerRegistry */
protected $registry;
/** @var ObjectManager */
protected $em;
/** @var RouterInterface */
protected $router;
/** @var array */
/** @var array */
protected $config;

/**
* @param ObjectManager $em
* @param RouterInterface $router
* @param array $config
* @param ManagerRegistry $registry
* @param RouterInterface $router
* @param array $config
*/
public function __construct(ObjectManager $em, RouterInterface $router, $config)
public function __construct(ManagerRegistry $registry, RouterInterface $router, $config)
{
$this->em = $em;
$this->registry = $registry;
$this->em = $registry->getManager();
$this->router = $router;
$this->config = $config;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
// custom object manager for this entity, override the default entity manager ?
if(isset($options['object_manager'])) {
if (isset($options['object_manager'])) {
$em = $options['object_manager'];
if(!$em instanceof ObjectManager) {
if (!$em instanceof ObjectManager) {
throw new \Exception('The entity manager \'em\' must be an ObjectManager instance');
}
// Use the custom manager instead.
$this->em = $em;
} else if (isset($this->config['object_manager'])) {
$em = $this->registry->getManager($this->config['object_manager']);
if (!$em instanceof ObjectManager) {
throw new \Exception('The entity manager \'em\' must be an ObjectManager instance');
}
$this->em = $em;
}
else {
$manager = $this->registry->getManagerForClass($options['class']);
if ($manager instanceof ObjectManager) {
$this->em = $manager;
}
}

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

// add the default data transformer
} else {

$newTagPrefix = isset($options['allow_add']['new_tag_prefix']) ? $options['allow_add']['new_tag_prefix'] : $this->config['allow_add']['new_tag_prefix'];
$newTagText = isset($options['allow_add']['new_tag_text']) ? $options['allow_add']['new_tag_text'] : $this->config['allow_add']['new_tag_text'];
$newTagPrefix = $options['allow_add']['new_tag_prefix'] ?? $this->config['allow_add']['new_tag_prefix'];
$newTagText = $options['allow_add']['new_tag_text'] ?? $this->config['allow_add']['new_tag_text'];

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

// merge variable names which are only set per instance with those from yml config
$varNames = array_merge(array('multiple', 'placeholder', 'primary_key', 'autostart'), array_keys($this->config));
$varNames = array_merge(['multiple', 'placeholder', 'primary_key', 'autostart', 'query_parameters'], array_keys($this->config));
foreach ($varNames as $varName) {
$view->vars[$varName] = $options[$varName];
}
Expand All @@ -109,11 +124,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
//tags options
$varNames = array_keys($this->config['allow_add']);
foreach ($varNames as $varName) {
if (isset($options['allow_add'][$varName])) {
$view->vars['allow_add'][$varName] = $options['allow_add'][$varName];
} else {
$view->vars['allow_add'][$varName] = $this->config['allow_add'][$varName];
}
$view->vars['allow_add'][$varName] = $options['allow_add'][$varName] ?? $this->config['allow_add'][$varName];
}

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

/**
* Added for pre Symfony 2.7 compatibility
*
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$this->configureOptions($resolver);
}

/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
array(
'object_manager'=> null,
$resolver->setDefaults([
'object_manager' => null,
'class' => null,
'data_class' => null,
'primary_key' => 'id',
'remote_path' => null,
'remote_route' => null,
'remote_params' => array(),
'remote_params' => [],
'multiple' => false,
'compound' => false,
'minimum_input_length' => $this->config['minimum_input_length'],
'page_limit' => $this->config['page_limit'],
'scroll' => $this->config['scroll'],
'allow_clear' => $this->config['allow_clear'],
'allow_add' => array(
'allow_add' => [
'enabled' => $this->config['allow_add']['enabled'],
'new_tag_text' => $this->config['allow_add']['new_tag_text'],
'new_tag_prefix' => $this->config['allow_add']['new_tag_prefix'],
'tag_separators' => $this->config['allow_add']['tag_separators']
),
],
'delay' => $this->config['delay'],
'text_property' => null,
'placeholder' => '',
'placeholder' => false,
'language' => $this->config['language'],
'required' => false,
'cache' => $this->config['cache'],
'cache_timeout' => $this->config['cache_timeout'],
'transformer' => null,
'autostart' => true,
'width' => isset($this->config['width']) ? $this->config['width'] : null,
'req_params' => array(),
'width' => $this->config['width'] ?? null,
'req_params' => [],
'property' => null,
'callback' => null,
'class_type' => null,
)
'query_parameters' => [],
'render_html' => $this->config['render_html'] ?? false
]
);
}

/**
* pre Symfony 3 compatibility
*
* @return string
*/
public function getName()
{
return $this->getBlockPrefix();
}

/**
* Symfony 2.8+
*
* @return string
*/
public function getBlockPrefix()
Expand Down
Loading

0 comments on commit 4632645

Please sign in to comment.