mirror of
				https://gitlab.com/Chill-Projet/chill-bundles.git
				synced 2025-10-29 00:14:58 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			75 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| 
 | |
| 
 | |
| namespace Chill\CustomFieldsBundle\CustomFields;
 | |
| 
 | |
| use Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface;
 | |
| use Chill\CustomFieldsBundle\Entity\CustomField;
 | |
| use Symfony\Component\Form\FormBuilderInterface;
 | |
| use Symfony\Component\HttpFoundation\RequestStack;
 | |
| 
 | |
| /**
 | |
|  * 
 | |
|  *
 | |
|  * @author Julien Fastré <julien.fastre@champs-libres.coop>
 | |
|  */
 | |
| class CustomFieldText implements CustomFieldInterface
 | |
| {
 | |
|     
 | |
|     private $requestStack;
 | |
|     
 | |
|     public function __construct(RequestStack $requestStack)
 | |
|     {
 | |
|         $this->requestStack = $requestStack;
 | |
|     }
 | |
|     
 | |
|     const MAX_LENGTH = 'maxLength';
 | |
|     
 | |
|     /**
 | |
|      * Create a form according to the maxLength option
 | |
|      * 
 | |
|      * if maxLength < 256 THEN the form type is 'text'
 | |
|      * if not, THEN the form type is textarea
 | |
|      * 
 | |
|      * @param FormBuilderInterface $builder
 | |
|      * @param CustomField $customField
 | |
|      */
 | |
|     public function buildForm(FormBuilderInterface $builder, CustomField $customField)
 | |
|     {
 | |
|         $type = ($customField->getOptions()[self::MAX_LENGTH] < 256) ? 'text' 
 | |
|               : 'textarea';
 | |
|         
 | |
|         $builder->add($customField->getSlug(), $type, array(
 | |
|             'label' => $customField->getName()[$this->requestStack->getCurrentRequest()->getLocale()]
 | |
|         ));
 | |
|     }
 | |
| 
 | |
|     public function render($value, CustomField $customField)
 | |
|     {
 | |
|         
 | |
|     }
 | |
| 
 | |
|     public function serialize($value, CustomField $customField)
 | |
|     {
 | |
|         return $value;
 | |
|     }
 | |
| 
 | |
|     public function deserialize($serialized, CustomField $customField)
 | |
|     {
 | |
|         return $serialized;
 | |
|     }
 | |
| 
 | |
|     public function getName()
 | |
|     {
 | |
|         return 'text field';
 | |
|     }
 | |
| 
 | |
|     public function buildOptionsForm(FormBuilderInterface $builder)
 | |
|     {
 | |
|        return $builder
 | |
|              ->add(self::MAX_LENGTH, 'integer', array('empty_data' => 256))
 | |
|           ;
 | |
|     }
 | |
| }
 |