mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-16 15:24:26 +00:00
merge of branch add_acl Squashed commit of the following: commit b112df16e568ad4a305e290b4cca9eb696bcf5d7 Merge: f8c44ca 11324cb Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Tue Jun 30 09:59:03 2015 +0200 Merge remote-tracking branch 'origin/master' into add_acl commit f8c44ca20b5a53fb18da00ab265d626dd8c770b2 Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Fri Jun 5 22:47:50 2015 +0200 fix Form signature to match abstractForm OptionsResolverInterface => OptionsResolver commit 586155ecfa85240d683aa8bd37493e948f89cd67 Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Fri Jun 5 22:47:25 2015 +0200 remove deprecation warnings from phpunit commit 6ada3ddef336b958a886215fcdb040a29f5a411f Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Fri Jun 5 22:30:30 2015 +0200 replace deprecated setDefaultOptions setdefaultsOptions => configureOptions commit 28b0e258d52b08ecd3799e19b15bd4b1f1e58f83 Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Fri Jun 5 11:20:30 2015 +0200 fix twig.form.resources deprecations commit cb09035c8f0eb74192f7e3e68c93c6378f158f2f Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Fri Jun 5 10:58:19 2015 +0200 switch to symfony 2.7 [ci skip]
106 lines
3.5 KiB
PHP
106 lines
3.5 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;
|
|
|
|
|
|
class CustomFieldsGroupType extends AbstractType
|
|
{
|
|
|
|
private $customizableEntities;
|
|
|
|
/**
|
|
*
|
|
* @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
|
|
*/
|
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
|
{
|
|
//prepare translation
|
|
$entities = array();
|
|
$customizableEntities = array();
|
|
|
|
foreach($this->customizableEntities as $key => $definition) {
|
|
$entities[$definition['class']] = $this->translator->trans($definition['name']);
|
|
$customizableEntities[$definition['class']] = $definition;
|
|
}
|
|
|
|
$builder
|
|
->add('name', 'translatable_string')
|
|
->add('entity', 'choice', array(
|
|
'choices' => $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 getName()
|
|
{
|
|
return 'custom_fields_group';
|
|
}
|
|
}
|