-
-
Save Clindbergh/acbff51e71d17df0054f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Date: 30-Sep-14 | |
* Time: 14:15 | |
*/ | |
use Doctrine\ORM\EntityManager; | |
use \DigitalBundle\Form\DataTransformer\ObjectListToStringTransformer; | |
use Symfony\Component\Form\AbstractTypeExtension; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\Form\FormInterface; | |
use Symfony\Component\Form\FormView; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
class TagTypeExtension extends AbstractTypeExtension | |
{ | |
private $em; | |
function __construct(EntityManager $em) | |
{ | |
$this->em = $em; | |
} | |
/** | |
* Returns the name of the type being extended. | |
* | |
* @return string The name of the type being extended | |
*/ | |
public function getExtendedType() | |
{ | |
return 'entity'; | |
} | |
public function buildView(FormView $view, FormInterface $form, array $options) | |
{ | |
$view->vars['select2'] = $options['select2']; | |
$view->vars['max_choice'] = $options['max_choice']; | |
$view->vars['allow_new'] = $options['allow_new']; | |
$view->vars['selected'] = $options['selected']; | |
} | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
if ($options['select2']) { | |
$builder->addModelTransformer(new ObjectListToStringTransformer($this->em, | |
[ | |
'class' => $options['class'], | |
'property' => $options['property'], | |
'delimiter' => ',' | |
] | |
) | |
); | |
/* $builder->addEventListener(FormEvents::PRE_SUBMIT, | |
function (FormEvent $event) use ($options) { | |
$optionsTransformer = [ | |
'class' => $options['class'], | |
'property' => $options['property'], | |
'delimiter' => ',' | |
]; | |
$event->setData((new ObjectListToStringTransformer($this->em, $optionsTransformer | |
))->reverseTransform($event->getData()[0])); | |
} | |
);*/ | |
} | |
parent::buildForm($builder, $options); | |
} | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'select2' => false, | |
'max_choice' => 0, | |
'allow_new' => false, | |
'selected' => array(), | |
// 'constraints' => array(new NotBlank(array('message' => '.'))) | |
// 'mapped' => function (Options $options) { | |
// return !$options['select2']; | |
// } | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment