Skip to content

Instantly share code, notes, and snippets.

@walva
Created February 26, 2020 15:08
Show Gist options
  • Save walva/5cdfecc5ec9d7f57adbe0287c80cdd1d to your computer and use it in GitHub Desktop.
Save walva/5cdfecc5ec9d7f57adbe0287c80cdd1d to your computer and use it in GitHub Desktop.
EntityHiddenType
<?php
namespace App\Form\DataTransformer;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class EntityHiddenTransformer implements DataTransformerInterface
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var string
*/
private $className;
/**
* @var string
*/
private $primaryKey;
public function __construct(EntityManagerInterface $objectManager, string $className, $primaryKey = "id")
{
$this->entityManager = $objectManager;
$this->className = $className;
$this->primaryKey = $primaryKey;
}
public function getEntityManager(): EntityManagerInterface
{
return $this->entityManager;
}
/**
* Transforms an object (entity) to a string (number).
*
* @param object|null $entity
*
* @return string
*/
public function transform($entity)
{
if (null === $entity) {
return '';
}
$method = 'get' . ucfirst($this->primaryKey);
// Probably worth throwing an exception if the method doesn't exist
// Note: you can always use reflection to get the PK even though there's no public getter for it
return $entity->$method();
}
/**
* Transforms a string (number) to an object (entity).
*
* @param string $identifier
*
* @return object|null
* @throws TransformationFailedException if object (entity) is not found.
*/
public function reverseTransform($identifier)
{
if (!$identifier) {
return null;
}
$entity = $this->entityManager->getRepository($this->className)
->find($identifier);
if (null === $entity) {
// causes a validation error
// this message is not shown to the user
// see the invalid_message option
throw new TransformationFailedException(sprintf(
'An entity with ID "%s" does not exist!',
$identifier
));
}
return $entity;
}
}
<?php
namespace App\Form;
use App\Form\DataTransformer\EntityHiddenTransformer;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class EntityHiddenType extends AbstractType
{
/**
* @var DataTransformerInterface $transformer
*/
private $entityManager;
/**
* Constructor
*
* @param DataTransformerInterface $transformer
*/
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @inheritDoc
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
// attach the specified model transformer for this entity list field
// // this will convert data between object and string formats
$builder
->addModelTransformer(
new EntityHiddenTransformer($this->entityManager, $options['class'], $options['primary_key'])
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'class' => null,
'primary_key' => 'id',
]);
}
/**
* @inheritDoc
*/
public function getParent()
{
return HiddenType::class;
}
/**
* @inheritDoc
*/
public function getName()
{
return 'entityhidden';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment