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.

53 line
1.9KB

  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\Crud\FileManagerType;
  6. use Symfony\Component\Form\Extension\Core\Type\TextType;
  7. /**
  8. * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  9. */
  10. final class FileManagerField implements FieldInterface
  11. {
  12. use FieldTrait;
  13. public const OPTION_MAX_LENGTH = 'maxLength';
  14. public const OPTION_RENDER_AS_HTML = 'renderAsHtml';
  15. public static function new(string $propertyName, ?string $label = null): self
  16. {
  17. return (new self())
  18. ->setProperty($propertyName)
  19. ->setLabel($label)
  20. ->setTemplatePath('@LcSov/adminlte/crud/field/file.html.twig')
  21. /*->addWebpackEncoreEntries('adminlte-field-filemanager')*/
  22. ->setCustomOption('managerDir', 'file')
  23. ->setCustomOption('type', 'file')
  24. ->setFormType(FileManagerType::class)
  25. ->addCssClass('field-text')
  26. ->setCustomOption(self::OPTION_MAX_LENGTH, null)
  27. ->setCustomOption(self::OPTION_RENDER_AS_HTML, false);
  28. }
  29. public function setMaxLength(int $length): self
  30. {
  31. if ($length < 1) {
  32. throw new \InvalidArgumentException(sprintf('The argument of the "%s()" method must be 1 or higher (%d given).', __METHOD__, $length));
  33. }
  34. $this->setCustomOption(self::OPTION_MAX_LENGTH, $length);
  35. return $this;
  36. }
  37. public function renderAsHtml(bool $asHtml = true): self
  38. {
  39. $this->setCustomOption(self::OPTION_RENDER_AS_HTML, $asHtml);
  40. return $this;
  41. }
  42. }