mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
109 lines
3.7 KiB
PHP
109 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Chill\CustomFieldsBundle\Form;
|
|
|
|
use Symfony\Component\Form\AbstractType;
|
|
use Symfony\Component\Form\FormBuilderInterface;
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
|
use Symfony\Component\Translation\TranslatorInterface;
|
|
use Symfony\Component\Form\FormEvents;
|
|
use Symfony\Component\Form\FormEvent;
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
|
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
|
|
|
|
|
|
class CustomFieldsGroupType extends AbstractType
|
|
{
|
|
|
|
|
|
private $customizableEntities; //TODO : add comment about this variable
|
|
|
|
/**
|
|
* @var \Symfony\Component\Translation\TranslatorInterface
|
|
*/
|
|
private $translator;
|
|
|
|
public function __construct(array $customizableEntities, TranslatorInterface $translator)
|
|
{
|
|
$this->customizableEntities = $customizableEntities;
|
|
$this->translator = $translator;
|
|
}
|
|
|
|
/**
|
|
* @param FormBuilderInterface $builder
|
|
* @param array $options
|
|
*/
|
|
//TODO : details about the function
|
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
|
{
|
|
//prepare translation
|
|
$entities = array();
|
|
$customizableEntities = array(); //TODO : change name too close than $this->customizableEntities
|
|
|
|
foreach($this->customizableEntities as $key => $definition) {
|
|
$entities[$definition['class']] = $this->translator->trans($definition['name']);
|
|
$customizableEntities[$definition['class']] = $definition;
|
|
}
|
|
|
|
$builder
|
|
->add('name', TranslatableStringFormType::class)
|
|
->add('entity', ChoiceType::class, array(
|
|
'choices' => array_combine(array_values($entities),array_keys($entities)),
|
|
))
|
|
;
|
|
|
|
$builder->addEventListener(FormEvents::POST_SET_DATA,
|
|
function(FormEvent $event) use ($customizableEntities, $builder){
|
|
$form = $event->getForm();
|
|
$group = $event->getData();
|
|
|
|
//stop the function if entity is not set
|
|
if ($group->getEntity() === NULL) {
|
|
return;
|
|
}
|
|
|
|
if (count($customizableEntities[$group->getEntity()]['options']) > 0) {
|
|
$optionBuilder = $builder
|
|
->getFormFactory()
|
|
->createBuilderForProperty(
|
|
'Chill\CustomFieldsBundle\Entity\CustomFieldsGroup',
|
|
'options'
|
|
)
|
|
->create('options', null, array(
|
|
'compound' => true,
|
|
'auto_initialize' => false,
|
|
'required' => false)
|
|
);
|
|
}
|
|
|
|
foreach($customizableEntities[$group->getEntity()]['options'] as $key => $option) {
|
|
$optionBuilder
|
|
->add($key, $option['form_type'], $option['form_options'])
|
|
;
|
|
}
|
|
if (isset($optionBuilder) && $optionBuilder->count() > 0) {
|
|
$form->add($optionBuilder->getForm());
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param OptionsResolverInterface $resolver
|
|
*/
|
|
public function configureOptions(OptionsResolver $resolver)
|
|
{
|
|
$resolver->setDefaults(array(
|
|
'data_class' => 'Chill\CustomFieldsBundle\Entity\CustomFieldsGroup'
|
|
));
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getBlockPrefix()
|
|
{
|
|
return 'custom_fields_group';
|
|
}
|
|
}
|