add declaration for customizables classes refs #265

This commit is contained in:
Julien Fastré 2014-11-04 15:16:38 +01:00
parent 474318e006
commit 475926ca53
6 changed files with 65 additions and 8 deletions

View File

@ -67,3 +67,7 @@ swiftmailer:
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
chill_custom_fields:
customizables_entities:
- { class: Chill\CustomFieldsBundle\Entity\BlopEntity, name: blop_entity }

View File

@ -62,7 +62,7 @@ class CustomFieldsGroupController extends Controller
*/
private function createCreateForm(CustomFieldsGroup $entity)
{
$form = $this->createForm(new CustomFieldsGroupType(), $entity, array(
$form = $this->createForm('custom_fields_group', $entity, array(
'action' => $this->generateUrl('customfieldsgroup_create'),
'method' => 'POST',
));
@ -142,7 +142,7 @@ class CustomFieldsGroupController extends Controller
*/
private function createEditForm(CustomFieldsGroup $entity)
{
$form = $this->createForm(new CustomFieldsGroupType(), $entity, array(
$form = $this->createForm('custom_fields_group', $entity, array(
'action' => $this->generateUrl('customfieldsgroup_update', array('id' => $entity->getId())),
'method' => 'PUT',
));

View File

@ -24,5 +24,12 @@ class ChillCustomFieldsExtension extends Extension
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
//add at least a blank array at 'customizable_entities' options
$customizable_entities = (isset($config['customizables_entities'])
&& count($config['customizables_entities']) > 0)
? $config['customizables_entities'] : array();
$container->setParameter('chill_custom_fields.customizable_entities', $customizable_entities);
}
}

View File

@ -18,11 +18,22 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('cl_custom_fields');
$rootNode = $treeBuilder->root('chill_custom_fields');
$classInfo = "The class which may receive custom fields";
$nameInfo = "The name which will appears in the user interface. May be translatable";
// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
$rootNode
->children()
->arrayNode('customizables_entities')
->prototype('array')
->children()
->scalarNode('class')->isRequired()->info($classInfo)->end()
->scalarNode('name') ->isRequired()->info($nameInfo) ->end()
->end()
->end()
->end()
;
return $treeBuilder;
}

View File

@ -5,18 +5,44 @@ namespace Chill\CustomFieldsBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Translation\TranslatorInterface;
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
$customizableEntites = array();
foreach($this->customizableEntities as $key => $definition) {
$customizableEntites[$definition['class']] = $this->translator->trans($definition['name']);
}
$builder
->add('name')
->add('entity')
->add('entity', 'choice', array(
'choices' => $customizableEntites
))
;
}
@ -35,6 +61,6 @@ class CustomFieldsGroupType extends AbstractType
*/
public function getName()
{
return 'cl_customfieldsbundle_customfieldsgroup';
return 'custom_fields_group';
}
}

View File

@ -11,9 +11,18 @@ services:
class: Chill\CustomFieldsBundle\Form\CustomFieldType
arguments:
- "@chill.custom_field_compiler"
tags:
- { name: 'form.type', alias: 'custom_field_choice' }
chill.custom_field.custom_fields_group_type:
class: Chill\CustomFieldsBundle\Form\CustomFieldsGroupType
arguments:
- %chill_custom_fields.customizable_entities%
- "@translator"
tags:
- { name: 'form.type', alias: 'custom_fields_group' }
chill.custom_field.custom_field_type:
class: Chill\CustomFieldsBundle\Form\Type\CustomFieldType
arguments: