Skip to content

Commit

Permalink
Several updates:
Browse files Browse the repository at this point in the history
* Option `class` is now optional. If not specified the Typeahead input will allow for non-entity lookups. The data passed is simply not transformed. It's up to your backend to do something useful with the values.
* Symfony form names with underscores will now work properly (eg: <form name="my_form">). Previously, form names with underscores would not POST properly.
* Replaced deprecated PropertyAccess::getPropertyAccessor() call with PropertyAccess::createPropertyAccessor().
* Removed unused TypeExtension.
* EntitiesToPropertyTransformer will no longer return an ArrayCollection. It returns a simple array instead.
* The `route` will receive extra parameters now to help it make decisions on how to return the results.
* Implemented `source` option to allow for a custom callback function that can return a list of matches (doesn't need to be AJAX).
  • Loading branch information
lifo101 committed Jul 19, 2016
1 parent 8296731 commit 2e22e97
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 167 deletions.
42 changes: 25 additions & 17 deletions Form/DataTransformer/EntitiesToPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace Lifo\TypeaheadBundle\Form\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Driver\DriverException;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;

class EntitiesToPropertyTransformer extends EntityToPropertyTransformer
{
Expand All @@ -26,14 +23,18 @@ public function transform($array)
throw new UnexpectedTypeException($array, 'array');
}

$return = array();
foreach ($array as $entity) {
$value = parent::transform($entity);
if ($value !== null) {
$return[] = $value;
if ($this->className) {
$return = array();
foreach ($array as $entity) {
$value = parent::transform($entity);
if ($value !== null) {
$return[] = $value;
}
}
return $return;
}
return $return;

return $array;
}


Expand All @@ -47,13 +48,20 @@ public function reverseTransform($array)
throw new UnexpectedTypeException($array, 'array');
}

$return = new ArrayCollection();
foreach ($array as $value) {
$entity = parent::reverseTransform($value);
if ($value !== null) {
$return[] = $entity;
if ($this->className) {
try {
return $this->em->createQueryBuilder()
->select('e')
->from($this->className, 'e')
->where('e.' . $this->property . ' IN (:ids)')
->setParameter('ids', $array)
->getQuery()
->getResult();
} catch (DriverException $ex) {
throw new TransformationFailedException('One or more "' . $this->property . '" values are invalid');
}
}
return $return;

return $array;
}
}
34 changes: 20 additions & 14 deletions Form/DataTransformer/EntityToPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,50 @@ class EntityToPropertyTransformer implements DataTransformerInterface
protected $className;
protected $property;
protected $unitOfWork;
protected $accessor;

public function __construct(EntityManager $em, $class, $property = 'id')
{
$this->em = $em;
$this->unitOfWork = $this->em->getUnitOfWork();
$this->className = $class;
$this->property = $property;
$this->accessor = PropertyAccess::createPropertyAccessor();
}

public function transform($entity)
{
if (null === $entity) {
if (empty($entity)) {
return null;
}

//if (!$this->unitOfWork->isInIdentityMap($entity) and !$this->unitOfWork->isScheduledForInsert($entity)) {
// throw new TransformationFailedException("Entities must be managed");
//}
if ($this->className) {
if (!empty($this->property)) {
return $this->accessor->getValue($entity, $this->property);
} else {
return current($this->unitOfWork->getEntityIdentifier($entity));
}
}

return !empty($this->property)
? PropertyAccess::getPropertyAccessor()->getValue($entity, $this->property)
: current($this->unitOfWork->getEntityIdentifier($entity));
return $entity;
}


public function reverseTransform($value)
{
if ($value === '' or $value === null) {
if (empty($value)) {
return null;
}

$repo = $this->em->getRepository($this->className);
if (!empty($this->property)) {
$entity = $repo->findOneBy(array($this->property => $value));
} else {
$entity = $repo->find($value);
if ($this->className) {
$repo = $this->em->getRepository($this->className);
if (!empty($this->property)) {
return $repo->findOneBy(array($this->property => $value));
} else {
return $repo->find($value);
}
}

return $entity;
return $value;
}
}
28 changes: 0 additions & 28 deletions Form/Extension/TypeaheadTypeExtension.php

This file was deleted.

24 changes: 12 additions & 12 deletions Form/Type/TypeaheadType.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ public function buildForm(FormBuilderInterface $builder, array $options)
public function finishView(FormView $view, FormInterface $form, array $options)
{
parent::finishView($view, $form, $options);
//$cfg = $form->getConfig();

// 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');
$vars = array(
'render', 'route', 'route_params', 'property', 'minLength', 'items', 'delay', 'spinner', 'multiple',
'allow_add', 'allow_remove', 'empty_value', 'resetOnSelect', 'callback', 'source',
);
foreach ($vars as $var) {
$view->vars[$var] = $options[$var];
}
$view->vars['simple'] = empty($options['class']);

// convert the route into an URL
if (!empty($options['route'])) {
Expand Down Expand Up @@ -93,28 +93,28 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired(array('class', 'render', 'route'));
$resolver->setRequired(array('render'));
$resolver->setDefaults(array(
'em' => null,
'query_builder' => null,
'property' => null,
'class' => null,
'property' => 'id',
'empty_value' => '',
'route' => null,
'route_params' => null,

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

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

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

Expand Down
Loading

0 comments on commit 2e22e97

Please sign in to comment.