mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 22:04:23 +00:00
Add column option, definition in config & adapt form
Exemple for configuration : ``` chill_custom_fields: customizables_entities: - class: Chill\ReportBundle\Entity\Report name: ReportEntity options: summary_fields: {key: abc, form_type: text, form_options: { required: false }} ```
This commit is contained in:
parent
960ba13555
commit
0c2b5f3fb4
@ -22,6 +22,8 @@ class Configuration implements ConfigurationInterface
|
||||
|
||||
$classInfo = "The class which may receive custom fields";
|
||||
$nameInfo = "The name which will appears in the user interface. May be translatable";
|
||||
$optionsInfo = "Options available for custom fields groups referencing this class";
|
||||
$prototypeTypeInfo = "The name of the form to append";
|
||||
$customizableEntitiesInfo = "A list of customizable entities";
|
||||
|
||||
$rootNode
|
||||
@ -33,9 +35,24 @@ class Configuration implements ConfigurationInterface
|
||||
->children()
|
||||
->scalarNode('class')->isRequired()->info($classInfo)->end()
|
||||
->scalarNode('name') ->isRequired()->info($nameInfo) ->end()
|
||||
->arrayNode('options')
|
||||
->info($optionsInfo)
|
||||
->defaultValue(array())
|
||||
->useAttributeAsKey('key')
|
||||
->prototype('array')
|
||||
->children()
|
||||
->scalarNode('form_type')
|
||||
->isRequired()
|
||||
->info($prototypeTypeInfo)
|
||||
->end()
|
||||
->variableNode('form_options')
|
||||
->defaultValue(array())
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
;
|
||||
|
||||
return $treeBuilder;
|
||||
|
@ -45,6 +45,12 @@ class CustomFieldsGroup
|
||||
* @var \Doctrine\Common\Collections\Collection
|
||||
*/
|
||||
private $customFields;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $options = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@ -161,5 +167,29 @@ class CustomFieldsGroup
|
||||
{
|
||||
return $this->entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* get options array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* set options array
|
||||
*
|
||||
* @param array $options
|
||||
* @return \Chill\CustomFieldsBundle\Entity\CustomFieldsGroup
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
$this->options = $options;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -6,6 +6,8 @@ use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
|
||||
|
||||
class CustomFieldsGroupType extends AbstractType
|
||||
@ -32,18 +34,50 @@ class CustomFieldsGroupType extends AbstractType
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
//prepare translation
|
||||
$customizableEntites = array();
|
||||
$entities = array();
|
||||
$customizableEntities = array();
|
||||
|
||||
foreach($this->customizableEntities as $key => $definition) {
|
||||
$customizableEntites[$definition['class']] = $this->translator->trans($definition['name']);
|
||||
$entities[$definition['class']] = $this->translator->trans($definition['name']);
|
||||
$customizableEntities[$definition['class']] = $definition;
|
||||
}
|
||||
|
||||
$builder
|
||||
->add('name', 'translatable_string')
|
||||
->add('entity', 'choice', array(
|
||||
'choices' => $customizableEntites
|
||||
'choices' => $entities
|
||||
))
|
||||
;
|
||||
|
||||
$builder->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event)
|
||||
use ($customizableEntities, $builder){
|
||||
$form = $event->getForm();
|
||||
$group = $event->getData();
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -13,6 +13,8 @@ Chill\CustomFieldsBundle\Entity\CustomFieldsGroup:
|
||||
entity:
|
||||
type: string
|
||||
length: 255
|
||||
options:
|
||||
type: json_array
|
||||
oneToMany:
|
||||
customFields:
|
||||
orderBy: { 'ordering': 'ASC' }
|
||||
|
22
Resources/migrations/Version20150224164531.php
Normal file
22
Resources/migrations/Version20150224164531.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Application\Migrations;
|
||||
|
||||
use Doctrine\DBAL\Migrations\AbstractMigration;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
|
||||
/**
|
||||
* Add an option column to customfieldsgroup
|
||||
*/
|
||||
class Version20150224164531 extends AbstractMigration
|
||||
{
|
||||
public function up(Schema $schema)
|
||||
{
|
||||
$this->addSql('ALTER TABLE customfieldsgroup ADD options JSON DEFAULT \'{}\'::json');
|
||||
}
|
||||
|
||||
public function down(Schema $schema)
|
||||
{
|
||||
$this->addSql('ALTER TABLE CustomFieldsGroup DROP options');
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user