chill-bundles/Form/CustomFieldType.php
Julien Fastré 408c774e53 move to symfony 2.7 + implements acl
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]
2015-06-30 14:43:27 +02:00

98 lines
2.9 KiB
PHP

<?php
namespace Chill\CustomFieldsBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
use Chill\CustomFieldsBundle\Entity\CustomField;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class CustomFieldType extends AbstractType
{
/**
*
* @var CustomFieldProvider
*/
private $customFieldProvider;
private $culture = 'fr';
public function __construct(CustomFieldProvider $compiler)
{
$this->customFieldProvider = $compiler;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$customFieldsList = array();
foreach ($this->customFieldProvider->getAllFields() as $key => $field) {
$customFieldsList[$key] = $field->getName();
}
$builder
->add('name', 'translatable_string')
->add('active')
->add('customFieldsGroup', 'entity', array(
'class' => 'ChillCustomFieldsBundle:CustomFieldsGroup',
'property' => 'name['.$this->culture.']'
))
->add('ordering', 'number')
->add('type', 'hidden', array('data' => $options['type']))
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event)
{
$customField = $event->getData();
$form = $event->getForm();
// check if the customField object is "new"
// If no data is passed to the form, the data is "null".
// This should be considered a new "customField"
if (!$customField || null === $customField->getId()) {
$form->add('slug', 'text');
}
});
$builder->add(
$this->customFieldProvider
->getCustomFieldByType($options['type'])
->buildOptionsForm(
$builder
->create('options', null, array('compound' => true))
)
);
}
/**
* @param OptionsResolverInterface $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Chill\CustomFieldsBundle\Entity\CustomField'
));
$resolver->setRequired(array('type'))
->addAllowedValues(array('type' =>
array_keys($this->customFieldProvider->getAllFields())
));
}
/**
* @return string
*/
public function getName()
{
return 'custom_field_choice';
}
}