Skip to content

Instantly share code, notes, and snippets.

@ssenkevich
Last active October 12, 2015 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssenkevich/d18b50efad1e1c1245e9 to your computer and use it in GitHub Desktop.
Save ssenkevich/d18b50efad1e1c1245e9 to your computer and use it in GitHub Desktop.
//--AutocompleteEntityType----------------------------------------------------------------
<?php
/**
* This file is part of SSVDeliveryServiceBundle
* Created by JetBrains PhpStorm.
*
* (c) Sergey Senkevich <ssenkevich@yandex.ru>
*
* Date: 5/19/12
* Time: 12:37 PM
*
* For the copyright and license information, please view the LICENSE file
*/
namespace SSV\Bundle\DeliveryServiceBundle\Form\Extension\Type;
use SSV\Bundle\DeliveryServiceBundle\Form\DataTransformer\EntityToIdAndPropertyTransformer;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Class AutocompleteEntityType
*/
class AutocompleteEntityType extends AbstractType
{
protected $registry;
/**
* Constructor.
*
* @param RegistryInterface $registry
*/
public function __construct(RegistryInterface $registry)
{
$this->registry = $registry;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$em = $this->registry->getManager($options['em']);
$propertyOptions = array_intersect_key($options, ['attr' => ['maxlength' => 255], 'label' => '']);
$builder->add('id', 'hidden')->add('property', 'text', $propertyOptions)->addModelTransformer(
new EntityToIdAndPropertyTransformer($em, $options['class'], $options['choice_label'])
);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'em' => 'default',
'class' => null,
'choice_label' => 'title',
'choice_translation_domain' => false
]
);
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return 'form';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'autocomplete_entity';
}
}
//--EntityToIdAndPropertyTransformer------------------------------------------------------
<?php
/**
* This file is part of SSVDeliveryServiceBundle
* Created by JetBrains PhpStorm.
*
* (c) Sergey Senkevich <ssenkevich@yandex.ru>
*
* Date: 5/19/12
* Time: 11:32 AM
*
* For the copyright and license information, please view the LICENSE file
*/
namespace SSV\Bundle\DeliveryServiceBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* Class EntityToIdAndPropertyTransformer
*/
class EntityToIdAndPropertyTransformer implements DataTransformerInterface
{
/** @var ObjectManager */
protected $om;
/** @var string */
protected $class;
/** @var string */
protected $property;
/**
* Constructor.
*
* @param \Doctrine\Common\Persistence\ObjectManager $om EntityManager
* @param string $class Entity ClassName
* @param string $property Property name
*/
public function __construct(ObjectManager $om, $class, $property)
{
$this->om = $om;
$this->class = $class;
$this->property = $property;
}
/**
* Transforms a normalized Entity to array of ID and specified property
*
* @param mixed $entity Instance of Entity
*
* @return array Array of ID and specified property
*
* @throws \Symfony\Component\Form\Exception\UnexpectedTypeException
*/
public function transform($entity)
{
if (null === $entity) {
return [
'id' => null,
'property' => ''
];
}
if (!is_object($entity)) {
throw new UnexpectedTypeException($entity, 'object');
}
$methodName = 'get' . ucfirst($this->property);
if (!method_exists($entity, $methodName)) {
throw new UnexpectedTypeException($entity, $this->class);
}
return [
'id' => $entity->getId(),
'property' => $entity->$methodName()
];
}
/**
* Transforms array of ID and specified property to Entity object
*
* @param array $value Client data array
*
* @return null|object Entity object
*
* @throws \Symfony\Component\Form\Exception\TransformationFailedException
* @throws \Symfony\Component\Form\Exception\UnexpectedTypeException
*/
public function reverseTransform($value)
{
if (empty($value)) {
return null;
}
if (!is_array($value)) {
throw new UnexpectedTypeException($value, 'array');
}
$id = isset($value['id']) ? $value['id'] : null;
if (null === $id || !ctype_digit($id)) {
throw new TransformationFailedException('Invalid ID');
}
$entity = $this->om->getRepository($this->class)->find($id);
if (null === $entity) {
throw new TransformationFailedException(sprintf('A %s with ID = %s does not exists!', $this->class, $id));
}
return $entity;
}
}
//--form_layout.html.twig------------------------------------------------------------------------------------------
...
{% block autocomplete_entity_widget %}
{% spaceless %}
<div class="autocompleteContainer" {{ block('widget_container_attributes') }}>
{{ form_widget(form.id) }}
{{ form_widget(form.property) }}
{{ form_errors(form.property) }}
</div>
{% endspaceless %}
{% endblock autocomplete_entity_widget %}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment