mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
make changes according to refs #242
This commit is contained in:
parent
cd2a7bff53
commit
2254acd8ee
@ -3,7 +3,13 @@
|
|||||||
namespace CL\CustomFieldsBundle;
|
namespace CL\CustomFieldsBundle;
|
||||||
|
|
||||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||||
|
use CL\CustomFieldsBundle\DependencyInjection\CustomFieldCompilerPass;
|
||||||
|
|
||||||
class CLCustomFieldsBundle extends Bundle
|
class CLCustomFieldsBundle extends Bundle
|
||||||
{
|
{
|
||||||
|
public function build(\Symfony\Component\DependencyInjection\ContainerBuilder $container)
|
||||||
|
{
|
||||||
|
parent::build($container);
|
||||||
|
$container->addCompilerPass(new CustomFieldCompilerPass());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,7 +62,7 @@ class CustomFieldController extends Controller
|
|||||||
*/
|
*/
|
||||||
private function createCreateForm(CustomField $entity)
|
private function createCreateForm(CustomField $entity)
|
||||||
{
|
{
|
||||||
$form = $this->createForm(new CustomFieldType(), $entity, array(
|
$form = $this->createForm('custom_field_choice', $entity, array(
|
||||||
'action' => $this->generateUrl('customfield_create'),
|
'action' => $this->generateUrl('customfield_create'),
|
||||||
'method' => 'POST',
|
'method' => 'POST',
|
||||||
));
|
));
|
||||||
@ -142,7 +142,7 @@ class CustomFieldController extends Controller
|
|||||||
*/
|
*/
|
||||||
private function createEditForm(CustomField $entity)
|
private function createEditForm(CustomField $entity)
|
||||||
{
|
{
|
||||||
$form = $this->createForm(new CustomFieldType(), $entity, array(
|
$form = $this->createForm('custom_field_choice', $entity, array(
|
||||||
'action' => $this->generateUrl('customfield_update', array('id' => $entity->getId())),
|
'action' => $this->generateUrl('customfield_update', array('id' => $entity->getId())),
|
||||||
'method' => 'PUT',
|
'method' => 'PUT',
|
||||||
));
|
));
|
||||||
|
@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace CL\CustomFieldsBundle\CustomFields;
|
||||||
|
|
||||||
|
use CL\CustomFieldsBundle\CustomFields\CustomFieldInterface;
|
||||||
|
use CL\CustomFieldsBundle\Entity\CustomField;
|
||||||
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use CL\CustomFieldsBundle\Form\AdressType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description of CustomFieldAddress
|
||||||
|
*
|
||||||
|
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||||
|
*/
|
||||||
|
class CustomFieldAddress implements CustomFieldInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
public $om;
|
||||||
|
|
||||||
|
public function __construct(EntityManagerInterface $om)
|
||||||
|
{
|
||||||
|
$this->om = $om;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function buildFormType(FormBuilderInterface $builder, CustomField $customField)
|
||||||
|
{
|
||||||
|
switch ($customField->getRelation())
|
||||||
|
{
|
||||||
|
case CustomField::ONE_TO_ONE :
|
||||||
|
$builder->build(
|
||||||
|
$builder->create($customField->getSlug(),
|
||||||
|
new AddressType()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case CustomField::ONE_TO_MANY :
|
||||||
|
$builder->build(
|
||||||
|
$builder->create($customField->getSlug(),
|
||||||
|
new AddressType(),
|
||||||
|
array(
|
||||||
|
'multiple' => true
|
||||||
|
))
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return 'CF address';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render($value, CustomField $customField)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function transformFromEntity($value, CustomField $customField)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function transformToEntity($value, CustomField $customField)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description of CustomFieldChoiceWithOther
|
||||||
|
*
|
||||||
|
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||||
|
*/
|
||||||
|
class CustomFieldChoiceWithOther
|
||||||
|
{
|
||||||
|
//put your code here
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace CL\CustomFieldsBundle\CustomFields;
|
||||||
|
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Receive all service tagged with 'chill.custom_field'
|
||||||
|
*
|
||||||
|
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||||
|
*/
|
||||||
|
class CustomFieldCompiler implements ContainerAwareInterface
|
||||||
|
{
|
||||||
|
private $servicesByType = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var \Symfony\Component\DependencyInjection\ContainerInterface
|
||||||
|
*/
|
||||||
|
private $container;
|
||||||
|
|
||||||
|
public function addCustomField($serviceName, $type)
|
||||||
|
{
|
||||||
|
$this->servicesByType[$type] = $serviceName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $type
|
||||||
|
* @return CustomFieldInterface
|
||||||
|
*/
|
||||||
|
public function getCustomFieldByType($type)
|
||||||
|
{
|
||||||
|
if (isset($this->servicesByType[$type])) {
|
||||||
|
return $this->servicesByType[$type];
|
||||||
|
} else {
|
||||||
|
throw new \LogicException('the custom field with type '.$type.' '
|
||||||
|
. 'is not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setContainer(ContainerInterface $container = null)
|
||||||
|
{
|
||||||
|
$this->container = $container;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAllFields()
|
||||||
|
{
|
||||||
|
return $this->servicesByType;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace CL\CustomFieldsBundle\CustomFields;
|
||||||
|
|
||||||
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
use CL\CustomFieldsBundle\Entity\CustomField;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||||
|
*/
|
||||||
|
interface CustomFieldInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param \CL\CustomFieldsBundle\CustomField\FormBuilderInterface $builder
|
||||||
|
* @param \CL\CustomFieldsBundle\CustomField\CustomField $customField
|
||||||
|
* @return \Symfony\Component\Form\FormTypeInterface the form type
|
||||||
|
*/
|
||||||
|
public function buildFormType(FormBuilderInterface $builder, CustomField $customField);
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param type $value
|
||||||
|
* @param \CL\CustomFieldsBundle\CustomField\CustomField $customField
|
||||||
|
*/
|
||||||
|
public function transformToEntity($value, CustomField $customField);
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param type $value
|
||||||
|
* @param \CL\CustomFieldsBundle\CustomField\CustomField $customField
|
||||||
|
*/
|
||||||
|
public function transformFromEntity($value, CustomField $customField);
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param type $value
|
||||||
|
* @param \CL\CustomFieldsBundle\CustomField\CustomField $customField
|
||||||
|
*/
|
||||||
|
public function render($value, CustomField $customField);
|
||||||
|
|
||||||
|
public function getName();
|
||||||
|
}
|
45
src/CL/CustomFieldsBundle/CustomFields/CustomFieldText.php
Normal file
45
src/CL/CustomFieldsBundle/CustomFields/CustomFieldText.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace CL\CustomFieldsBundle\CustomFields;
|
||||||
|
|
||||||
|
use CL\CustomFieldsBundle\CustomFields\CustomFieldInterface;
|
||||||
|
use CL\CustomFieldsBundle\Entity\CustomField;
|
||||||
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||||
|
*/
|
||||||
|
class CustomFieldText implements CustomFieldInterface
|
||||||
|
{
|
||||||
|
public function buildFormType(FormBuilderInterface $builder, CustomField $customField)
|
||||||
|
{
|
||||||
|
$builder->add($customField->getSlug(), 'text', array(
|
||||||
|
'label' => $customField->getLabel()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render($value, CustomField $customField)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function transformFromEntity($value, CustomField $customField)
|
||||||
|
{
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function transformToEntity($value, CustomField $customField)
|
||||||
|
{
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return 'text field';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace CL\CustomFieldsBundle\DependencyInjection;
|
||||||
|
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||||
|
use Symfony\Component\DependencyInjection\Reference;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||||
|
*/
|
||||||
|
class CustomFieldCompilerPass implements CompilerPassInterface
|
||||||
|
{
|
||||||
|
public function process(ContainerBuilder $container)
|
||||||
|
{
|
||||||
|
if (!$container->hasDefinition('chill.custom_field_compiler')) {
|
||||||
|
throw new \LogicException('service chill.custom_field_compiler '
|
||||||
|
. 'is not defined.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$definition = $container->getDefinition(
|
||||||
|
'chill.custom_field_compiler'
|
||||||
|
);
|
||||||
|
|
||||||
|
$taggedServices = $container->findTaggedServiceIds(
|
||||||
|
'chill.custom_field'
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ($taggedServices as $id => $tagAttributes) {
|
||||||
|
foreach ($tagAttributes as $attributes) {
|
||||||
|
$definition->addMethodCall(
|
||||||
|
'addCustomField',
|
||||||
|
array(new Reference($id), $attributes["type"])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -25,7 +25,7 @@ class BlopEntity
|
|||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $customField;
|
private $customField = array();
|
||||||
|
|
||||||
private $adress;
|
private $adress;
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ class BlopEntity
|
|||||||
*
|
*
|
||||||
* @return BlopEntity
|
* @return BlopEntity
|
||||||
*/
|
*/
|
||||||
public function setCustomField($customField)
|
public function setCustomField(array $customField)
|
||||||
{
|
{
|
||||||
$this->customField = $customField;
|
$this->customField = $customField;
|
||||||
|
|
||||||
@ -126,6 +126,7 @@ class BlopEntity
|
|||||||
*/
|
*/
|
||||||
public function getCustomField()
|
public function getCustomField()
|
||||||
{
|
{
|
||||||
|
|
||||||
return $this->customField;
|
return $this->customField;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,8 @@ class CustomField
|
|||||||
*/
|
*/
|
||||||
private $label;
|
private $label;
|
||||||
|
|
||||||
|
private $slug;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
@ -27,6 +29,17 @@ class CustomField
|
|||||||
*/
|
*/
|
||||||
private $active;
|
private $active;
|
||||||
|
|
||||||
|
private $options = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $relation = 1;
|
||||||
|
|
||||||
|
const ONE_TO_ONE = 1;
|
||||||
|
const ONE_TO_MANY = 2;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get id
|
* Get id
|
||||||
@ -38,6 +51,11 @@ class CustomField
|
|||||||
return $this->id;
|
return $this->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getSlug()
|
||||||
|
{
|
||||||
|
return $this->slug;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set label
|
* Set label
|
||||||
*
|
*
|
||||||
@ -49,9 +67,18 @@ class CustomField
|
|||||||
{
|
{
|
||||||
$this->label = $label;
|
$this->label = $label;
|
||||||
|
|
||||||
|
if ($this->slug === NULL) {
|
||||||
|
$this->slug = preg_replace('/[^A-Za-z0-9-]+/', '-', $label);
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getOptions()
|
||||||
|
{
|
||||||
|
return $this->options;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get label
|
* Get label
|
||||||
*
|
*
|
||||||
@ -86,6 +113,19 @@ class CustomField
|
|||||||
return $this->type;
|
return $this->type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getRelation()
|
||||||
|
{
|
||||||
|
return $this->relation;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setRelation($relation)
|
||||||
|
{
|
||||||
|
$this->relation = $relation;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set active
|
* Set active
|
||||||
*
|
*
|
||||||
|
@ -6,18 +6,23 @@ use Symfony\Component\Form\AbstractType;
|
|||||||
use Symfony\Component\Form\FormBuilderInterface;
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal Ne fonctionne pas encore
|
||||||
|
*/
|
||||||
class AdressType extends AbstractType
|
class AdressType extends AbstractType
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @param FormBuilderInterface $builder
|
|
||||||
* @param array $options
|
// /**
|
||||||
*/
|
// * @param FormBuilderInterface $builder
|
||||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
// * @param array $options
|
||||||
{
|
// */
|
||||||
$builder
|
// public function buildForm(FormBuilderInterface $builder, array $options)
|
||||||
->add('data')
|
// {
|
||||||
;
|
// $builder
|
||||||
}
|
// ->add('data')
|
||||||
|
// ;
|
||||||
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param OptionsResolverInterface $resolver
|
* @param OptionsResolverInterface $resolver
|
||||||
@ -25,15 +30,21 @@ class AdressType extends AbstractType
|
|||||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||||
{
|
{
|
||||||
$resolver->setDefaults(array(
|
$resolver->setDefaults(array(
|
||||||
'data_class' => 'CL\CustomFieldsBundle\Entity\Adress'
|
'data_class' => 'CL\CustomFieldsBundle\Entity\Adress',
|
||||||
|
'class' => 'CL\CustomFieldsBundle\Entity\Adress'
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getParent()
|
||||||
|
{
|
||||||
|
return 'entity';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getName()
|
public function getName()
|
||||||
{
|
{
|
||||||
return 'cl_customfieldsbundle_adress';
|
return 'adress';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,8 @@ class BlopEntityType extends AbstractType
|
|||||||
$builder
|
$builder
|
||||||
->add('field1')
|
->add('field1')
|
||||||
->add('field2')
|
->add('field2')
|
||||||
->add('adress', new AdressType())
|
//->add('adress', new AdressType())
|
||||||
->add('customField',new CustomFieldType($entityManager))
|
->add('customField', 'custom_field')
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,19 +5,49 @@ namespace CL\CustomFieldsBundle\Form;
|
|||||||
use Symfony\Component\Form\AbstractType;
|
use Symfony\Component\Form\AbstractType;
|
||||||
use Symfony\Component\Form\FormBuilderInterface;
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||||
|
use CL\CustomFieldsBundle\CustomFields\CustomFieldCompiler;
|
||||||
|
use CL\CustomFieldsBundle\Entity\CustomField;
|
||||||
|
|
||||||
class CustomFieldType extends AbstractType
|
class CustomFieldType extends AbstractType
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var \CL\CustomFieldsBundle\CustomFields\CustomFieldCompiler
|
||||||
|
*/
|
||||||
|
private $customFieldCompiler;
|
||||||
|
|
||||||
|
|
||||||
|
public function __construct(CustomFieldCompiler $compiler)
|
||||||
|
{
|
||||||
|
$this->customFieldCompiler = $compiler;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @param FormBuilderInterface $builder
|
* @param FormBuilderInterface $builder
|
||||||
* @param array $options
|
* @param array $options
|
||||||
*/
|
*/
|
||||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
$customFieldsList = array();
|
||||||
|
|
||||||
|
foreach ($this->customFieldCompiler->getAllFields() as $key => $field) {
|
||||||
|
$customFieldsList[$key] = $field->getName();
|
||||||
|
}
|
||||||
|
|
||||||
$builder
|
$builder
|
||||||
->add('label')
|
->add('label')
|
||||||
->add('type')
|
->add('type', 'choice', array(
|
||||||
|
'choices' => $customFieldsList,
|
||||||
|
'expanded' => false,
|
||||||
|
'multiple' => false
|
||||||
|
))
|
||||||
->add('active')
|
->add('active')
|
||||||
|
->add('relation', 'choice', array(
|
||||||
|
'choices' => array(
|
||||||
|
CustomField::ONE_TO_ONE => 'one to one ',
|
||||||
|
CustomField::ONE_TO_MANY => 'one to many'
|
||||||
|
)
|
||||||
|
))
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,6 +66,6 @@ class CustomFieldType extends AbstractType
|
|||||||
*/
|
*/
|
||||||
public function getName()
|
public function getName()
|
||||||
{
|
{
|
||||||
return 'cl_customfieldsbundle_customfield';
|
return 'custom_field_choice';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace CL\CustomFieldsBundle\Form\DataTransformer;
|
||||||
|
|
||||||
|
use Symfony\Component\Form\DataTransformerInterface;
|
||||||
|
use CL\CustomFieldsBundle\CustomFields\CustomFieldInterface;
|
||||||
|
use CL\CustomFieldsBundle\Entity\CustomField;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||||
|
*/
|
||||||
|
class CustomFieldDataTransformer implements DataTransformerInterface
|
||||||
|
{
|
||||||
|
private $customFieldDefinition;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var \CL\CustomFieldsBundle\Entity\CustomField
|
||||||
|
*/
|
||||||
|
private $customField;
|
||||||
|
|
||||||
|
public function __construct(CustomFieldInterface $customFieldDefinition,
|
||||||
|
CustomField $customField)
|
||||||
|
{
|
||||||
|
$this->customFieldDefinition = $customFieldDefinition;
|
||||||
|
$this->customField = $customField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function reverseTransform($value)
|
||||||
|
{
|
||||||
|
return $this->customFieldDefinition->transformFromEntity($value,
|
||||||
|
$this->customField);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function transform($value)
|
||||||
|
{
|
||||||
|
return $this->customFieldDefinition->transformToEntity($value,
|
||||||
|
$this->customField);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -16,20 +16,30 @@ use Symfony\Component\Form\FormBuilderInterface;
|
|||||||
use CL\CustomFieldsBundle\Form\DataTransformer\JsonCustomFieldToArrayTransformer;
|
use CL\CustomFieldsBundle\Form\DataTransformer\JsonCustomFieldToArrayTransformer;
|
||||||
use Doctrine\Common\Persistence\ObjectManager;
|
use Doctrine\Common\Persistence\ObjectManager;
|
||||||
use CL\CustomFieldsBundle\Form\AdressType;
|
use CL\CustomFieldsBundle\Form\AdressType;
|
||||||
|
use CL\CustomFieldsBundle\CustomFields\CustomFieldCompiler;
|
||||||
|
use CL\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
|
||||||
|
|
||||||
class CustomFieldType extends AbstractType
|
class CustomFieldType extends AbstractType
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ObjectManager
|
* @var ObjectManager
|
||||||
*/
|
*/
|
||||||
private $om;
|
private $om;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var CustomFieldCompiler
|
||||||
|
*/
|
||||||
|
private $customFieldCompiler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ObjectManager $om
|
* @param ObjectManager $om
|
||||||
*/
|
*/
|
||||||
public function __construct(ObjectManager $om)
|
public function __construct(ObjectManager $om, CustomFieldCompiler $compiler)
|
||||||
{
|
{
|
||||||
$this->om = $om;
|
$this->om = $om;
|
||||||
|
$this->customFieldCompiler = $compiler;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||||
@ -39,45 +49,70 @@ class CustomFieldType extends AbstractType
|
|||||||
->findAll();
|
->findAll();
|
||||||
|
|
||||||
foreach ($customFields as $cf) {
|
foreach ($customFields as $cf) {
|
||||||
if($cf->getType() === 'ManyToOne(Adress)') {
|
|
||||||
$builder->add($cf->getLabel(), 'entity', array(
|
|
||||||
'class' => 'CLCustomFieldsBundle:Adress',
|
|
||||||
'property' => 'data'
|
|
||||||
));
|
|
||||||
} else if ($cf->getType() === 'ManyToOnePersist(Adress)') {
|
|
||||||
$builder->add($cf->getLabel(), new AdressType());
|
|
||||||
} else if($cf->getType() === 'ManyToMany(Adress)') {
|
|
||||||
|
|
||||||
$adress = $this->om
|
$builder->add(
|
||||||
->getRepository('CLCustomFieldsBundle:Adress')
|
$builder->create(
|
||||||
->findAll();
|
$cf->getSlug(),
|
||||||
|
$this->customFieldCompiler
|
||||||
|
->getCustomFieldByType($cf->getType())
|
||||||
|
->buildFormType($builder, $cf),
|
||||||
|
array('mapped' => true)
|
||||||
|
)
|
||||||
|
->addModelTransformer(new CustomFieldDataTransformer(
|
||||||
|
$this->customFieldCompiler
|
||||||
|
->getCustomFieldByType($cf->getType()),
|
||||||
|
$cf)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
$adressId = array_map(
|
// if($cf->getType() === 'ManyToOne(Adress)') {
|
||||||
function($e) { return $e->getId(); },
|
// $builder->add($cf->getLabel(), 'entity', array(
|
||||||
$adress);
|
// 'class' => 'CLCustomFieldsBundle:Adress',
|
||||||
|
// 'property' => 'data'
|
||||||
$adressLabel = array_map(
|
// ));
|
||||||
function($e) { return (string) $e; },
|
// } else if ($cf->getType() === 'ManyToOnePersist(Adress)') {
|
||||||
$adress);
|
// $builder->add($cf->getLabel(), new AdressType());
|
||||||
|
// } else if($cf->getType() === 'ManyToMany(Adress)') {
|
||||||
$addressChoices = array_combine($adressId, $adressLabel);
|
//
|
||||||
|
// $adress = $this->om
|
||||||
|
// ->getRepository('CLCustomFieldsBundle:Adress')
|
||||||
$builder->add($cf->getLabel(), 'choice', array(
|
// ->findAll();
|
||||||
'choices' => $addressChoices,
|
//
|
||||||
'multiple' => true
|
// $adressId = array_map(
|
||||||
));
|
// function($e) { return $e->getId(); },
|
||||||
}
|
// $adress);
|
||||||
else {
|
//
|
||||||
$builder->add($cf->getLabel(), $cf->getType());
|
// $adressLabel = array_map(
|
||||||
}
|
// function($e) { return (string) $e; },
|
||||||
|
// $adress);
|
||||||
|
//
|
||||||
|
// $addressChoices = array_combine($adressId, $adressLabel);
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// $builder->add($cf->getLabel(), 'choice', array(
|
||||||
|
// 'choices' => $addressChoices,
|
||||||
|
// 'multiple' => true
|
||||||
|
// ));
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// $builder->add($cf->getLabel(), $cf->getType());
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
$builder->addViewTransformer(new JsonCustomFieldToArrayTransformer($this->om));
|
//$builder->addViewTransformer(new JsonCustomFieldToArrayTransformer($this->om));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setDefaultOptions(\Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver)
|
||||||
|
{
|
||||||
|
$resolver
|
||||||
|
//->addAllowedTypes(array('context' => 'string'))
|
||||||
|
//->setRequired(array('context'))
|
||||||
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getName()
|
public function getName()
|
||||||
{
|
{
|
||||||
return 'custom_field';
|
return 'custom_field';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -11,9 +11,15 @@ CL\CustomFieldsBundle\Entity\CustomField:
|
|||||||
label:
|
label:
|
||||||
type: string
|
type: string
|
||||||
length: 255
|
length: 255
|
||||||
|
slug:
|
||||||
|
type: string
|
||||||
|
length: 255
|
||||||
type:
|
type:
|
||||||
type: string
|
type: string
|
||||||
length: 255
|
length: 255
|
||||||
|
relation:
|
||||||
|
type: integer
|
||||||
|
default: 1
|
||||||
active:
|
active:
|
||||||
type: boolean
|
type: boolean
|
||||||
lifecycleCallbacks: { }
|
lifecycleCallbacks: { }
|
||||||
|
@ -2,6 +2,34 @@ parameters:
|
|||||||
# cl_custom_fields.example.class: CL\CustomFieldsBundle\Example
|
# cl_custom_fields.example.class: CL\CustomFieldsBundle\Example
|
||||||
|
|
||||||
services:
|
services:
|
||||||
# cl_custom_fields.example:
|
chill.custom_field_compiler:
|
||||||
# class: %cl_custom_fields.example.class%
|
class: CL\CustomFieldsBundle\CustomFields\CustomFieldCompiler
|
||||||
# arguments: [@service_id, "plain_value", %parameter%]
|
call:
|
||||||
|
- [setContainer, ["@service_container"]]
|
||||||
|
|
||||||
|
chill.custom_field.custom_field_choice_type:
|
||||||
|
class: CL\CustomFieldsBundle\Form\CustomFieldType
|
||||||
|
arguments:
|
||||||
|
- "@chill.custom_field_compiler"
|
||||||
|
tags:
|
||||||
|
- { name: 'form.type', alias: 'custom_field_choice' }
|
||||||
|
|
||||||
|
chill.custom_field.custom_field_type:
|
||||||
|
class: CL\CustomFieldsBundle\Form\Type\CustomFieldType
|
||||||
|
arguments:
|
||||||
|
- "@doctrine.orm.entity_manager"
|
||||||
|
- "@chill.custom_field_compiler"
|
||||||
|
tags:
|
||||||
|
- { name: 'form.type', alias: 'custom_field' }
|
||||||
|
|
||||||
|
chill.custom_field.text:
|
||||||
|
class: CL\CustomFieldsBundle\CustomFields\CustomFieldText
|
||||||
|
tags:
|
||||||
|
- { name: 'chill.custom_field', type: 'text' }
|
||||||
|
|
||||||
|
chill.custom_field.address:
|
||||||
|
class: CL\CustomFieldsBundle\CustomFields\CustomFieldAddress
|
||||||
|
arguments:
|
||||||
|
- "@doctrine.orm.entity_manager"
|
||||||
|
tags:
|
||||||
|
- { name: 'chill.custom_field', type: 'address' }
|
||||||
|
@ -3,7 +3,13 @@
|
|||||||
{% block body -%}
|
{% block body -%}
|
||||||
<h1>BlopEntity edit</h1>
|
<h1>BlopEntity edit</h1>
|
||||||
|
|
||||||
{{ form(edit_form) }}
|
{{ form_start(edit_form) }}
|
||||||
|
|
||||||
|
{{ form_row(edit_form.customField.label0) }}
|
||||||
|
|
||||||
|
{{ form_rest(edit_form) }}
|
||||||
|
|
||||||
|
{{ form_end(edit_form) }}
|
||||||
|
|
||||||
<ul class="record_actions">
|
<ul class="record_actions">
|
||||||
<li>
|
<li>
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
<td><a href="{{ path('blopentity_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td>
|
<td><a href="{{ path('blopentity_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td>
|
||||||
<td>{{ entity.field1 }}</td>
|
<td>{{ entity.field1 }}</td>
|
||||||
<td>{{ entity.field2 }}</td>
|
<td>{{ entity.field2 }}</td>
|
||||||
<td>{{ entity.customField }}</td>
|
<td>{{ dump(entity.customField) }}</td>
|
||||||
<td>
|
<td>
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Customfield</th>
|
<th>Customfield</th>
|
||||||
<td>{{ entity.customField }}</td>
|
<td>{{ dump(entity.customField) }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user