Skip to content

Commit

Permalink
Updated to support Symfony 2.8|3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lifo101 committed Jul 13, 2016
1 parent 10c8eeb commit da548b6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 41 deletions.
81 changes: 47 additions & 34 deletions Form/Type/TypeaheadType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@

namespace Lifo\TypeaheadBundle\Form\Type;

use Doctrine\ORM\EntityManager;
use Lifo\TypeaheadBundle\Form\DataTransformer\EntitiesToPropertyTransformer;
use Lifo\TypeaheadBundle\Form\DataTransformer\EntityToPropertyTransformer;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Bridge\Doctrine\Form\EventListener\MergeDoctrineCollectionListener;
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Exception\RuntimeException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Exception\RuntimeException;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\Collections\Collection;
use Lifo\TypeaheadBundle\Form\DataTransformer\EntityToPropertyTransformer;
use Lifo\TypeaheadBundle\Form\DataTransformer\EntitiesToPropertyTransformer;

class TypeaheadType extends AbstractType
{
Expand Down Expand Up @@ -61,9 +57,9 @@ public function finishView(FormView $view, FormInterface $form, array $options)

// assign some variables to the view template
$vars = array('render', 'route', 'route_params', 'property',
'minLength', 'items', 'delay', 'spinner',
'multiple', 'allow_add', 'allow_remove', 'empty_value',
'resetOnSelect', 'callback');
'minLength', 'items', 'delay', 'spinner',
'multiple', 'allow_add', 'allow_remove', 'empty_value',
'resetOnSelect', 'callback');
foreach ($vars as $var) {
$view->vars[$var] = $options[$var];
}
Expand All @@ -82,35 +78,52 @@ public function finishView(FormView $view, FormInterface $form, array $options)
}
}

// public function configureOptions(OptionsResolver $resolver) // sf2.6+
/**
* Pre Symfony 2.7 compatibility
*
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$this->configureOptions($resolver);
}

//$resolver->setOptional(array(''));
$resolver->setRequired(array('class','render','route'));
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired(array('class', 'render', 'route'));
$resolver->setDefaults(array(
'em' => null,
'query_builder' => null,
'property' => null,
'empty_value' => '',
'route_params' => null,

'multiple' => false,
'allow_add' => false,
'allow_remove' => false,

'delay' => 250,
'minLength' => 2,
'items' => 10,
'spinner' => 'glyphicon glyphicon-refresh spin',
'resetOnSelect' => function(Options $options) { return $options['multiple']; },
'callback' => null,

'compound' => false, //function(Options $options){ return $options['multiple']; },
'em' => null,
'query_builder' => null,
'property' => null,
'empty_value' => '',
'route_params' => null,

'multiple' => false,
'allow_add' => false,
'allow_remove' => false,

'delay' => 250,
'minLength' => 2,
'items' => 10,
'spinner' => 'glyphicon glyphicon-refresh spin',
'resetOnSelect' => function (Options $options) {
return $options['multiple'];
},
'callback' => null,

'compound' => false, //function(Options $options){ return $options['multiple']; },
));
}

public function getName()
{
return $this->getBlockPrefix();
}

public function getBlockPrefix()
{
return 'entity_typeahead';
}
Expand Down
6 changes: 3 additions & 3 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ parameters:
services:
# provide twig functions
lifo_typeahead.twig_extension:
class: %lifo_typeahead.twig_extension.class%
class: "%lifo_typeahead.twig_extension.class%"
tags: [{ name: twig.extension }]

# main typeahead autocomplete form field
lifo_typeahead.typeahead_type:
class: %lifo_typeahead.typeahead_type.class%
arguments: [@service_container, @doctrine.orm.entity_manager, @router]
class: "%lifo_typeahead.typeahead_type.class%"
arguments: ["@service_container", "@doctrine.orm.entity_manager", "@router"]
tags: [{ name: form.type, alias: entity_typeahead }]

# experimental extension ...
Expand Down
2 changes: 1 addition & 1 deletion Twig/Extension/TypeaheadTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function getName()
public function getFunctions()
{
return array(
'lifo_typeahead_init' => new \Twig_Function_Method($this, 'initTypeaheadFunction', array('needs_environment' => true, 'is_safe' => array('html'))),
new \Twig_SimpleFunction('lifo_typeahead_init', array($this, 'initTypeaheadFunction'), array('needs_environment' => true, 'is_safe' => array('html'))),
);
}

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lifo/typeahead-bundle",
"type": "symfony-bundle",
"description": "A Symfony2 bundle that provides a Typeahead form type compatible with Bootstrap v2.x and v3.x",
"description": "A Symfony bundle that provides an autocomplete Typeahead form type compatible with Bootstrap v2|v3",
"keywords": ["symfony","typeahead","autocomplete","bootstrap"],
"license": "MIT",
"authors": [
Expand All @@ -11,11 +11,11 @@
}
],
"require": {
"symfony/symfony": "^2.2"
"symfony/symfony": "^2.6 || ^3.0"
},
"suggest": {
"twbs/bootstrap": "Bootstrap framework",
"braincrafted/bootstrap-bundle": "Bundle to automatically enable Bootstrap styles for Symfony"
"mopa/bootstrap-bundle": "Bundle to automatically enable Bootstrap styles for Symfony"
},
"autoload": {
"psr-0": { "Lifo\\TypeaheadBundle": "" }
Expand Down

0 comments on commit da548b6

Please sign in to comment.