You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CollectionField.php 2.9KB

3 jaren geleden
3 jaren geleden
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Lc\SovBundle\Field;
  3. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
  5. use Lc\SovBundle\Form\Type\FileManagerType;
  6. use Lc\SovBundle\Form\Type\GalleryManagerType;
  7. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  8. class CollectionField implements FieldInterface
  9. {
  10. use FieldTrait;
  11. public const OPTION_ALLOW_ADD = 'allowAdd';
  12. public const OPTION_ALLOW_DELETE = 'allowDelete';
  13. public const OPTION_ENTRY_IS_COMPLEX = 'entryIsComplex';
  14. public const OPTION_ENTRY_TYPE = 'entryType';
  15. public const OPTION_SHOW_ENTRY_LABEL = 'showEntryLabel';
  16. public static function new(string $propertyName, ?string $label = null): self
  17. {
  18. return (new self())
  19. ->setProperty($propertyName)
  20. ->setLabel($label)
  21. ->setTemplatePath('@LcSov/tabler/crud/field/collection.html.twig')
  22. ->setFormType(CollectionType::class)
  23. ->addCssClass('field-collection')
  24. ->addJsFiles('bundles/lcsov/js/form-type-collection-array.js')
  25. ->setFormTypeOption('allow_add', true)
  26. ->setFormTypeOption('allow_delete', true)
  27. ->setFormTypeOption('entry_options', array('label' => false))
  28. ->setFormTypeOption('attr', array('class' => 'field-collection-group'))
  29. //Fixe le bug easyadmin lors de la gestion d'un champ de type array, laisser a false pour une entité
  30. ->setFormTypeOption('row_attr', array('data-reindex-key' => true));
  31. }
  32. public function allowAdd(bool $allow = true): self
  33. {
  34. $this->setCustomOption(self::OPTION_ALLOW_ADD, $allow);
  35. return $this;
  36. }
  37. public function allowDelete(bool $allow = true): self
  38. {
  39. $this->setCustomOption(self::OPTION_ALLOW_DELETE, $allow);
  40. return $this;
  41. }
  42. /**
  43. * Set this option to TRUE if the collection items are complex form types
  44. * composed of several form fields (EasyAdmin applies a special rendering to make them look better).
  45. */
  46. public function setEntryIsComplex(bool $isComplex): self
  47. {
  48. $this->setCustomOption(self::OPTION_ENTRY_IS_COMPLEX, $isComplex);
  49. return $this;
  50. }
  51. public function setEntryType(string $formTypeFqcn): self
  52. {
  53. $this->setCustomOption(self::OPTION_ENTRY_TYPE, $formTypeFqcn);
  54. return $this;
  55. }
  56. public function showEntryLabel(bool $showLabel = true): self
  57. {
  58. $this->setCustomOption(self::OPTION_SHOW_ENTRY_LABEL, $showLabel);
  59. return $this;
  60. }
  61. }