azaw / easyadmin-az-fields
Custom EasyAdmin fields by AZ
v1.1.0
2025-10-04 09:47 UTC
Requires
- php: >=8.0
- ext-fileinfo: *
- doctrine/orm: ^2.5
- easycorp/easyadmin-bundle: ^4.0
- symfony/form: ^5.4 || ^6.0 || ^7.0
- symfony/framework-bundle: ^5.4 || ^6.0 || ^7.0
- symfony/http-foundation: ^5.4 || ^6.0 || ^7.0
Requires (Dev)
- symfony/var-dumper: ^5.4 || ^6.0 || ^7.0
This package is auto-updated.
Last update: 2025-10-04 09:49:25 UTC
README
Available fields
Start up
I have no idea what I did wrong (yet) but in my DashboardController I had to add:
public function configureAssets(): Assets { return parent::configureAssets() ->addJsFile('/bundles/easyadminazfields/js/coordinates_field.js') ->addJsFile('/bundles/easyadminazfields/vendor/cropperjs/cropper.min.js') ->addCssFile('/bundles/easyadminazfields/vendor/cropperjs/cropper.min.css') ->addJsFile('/bundles/easyadminazfields/js/cropper_field.js') ->addCssFile('/bundles/easyadminazfields/css/cropper_field.css'); } public function configureCrud(): Crud { return parent::configureCrud() ->addFormTheme('@EasyAdminAzFields/crop_field.html.twig') ->addFormTheme('@EasyAdminAzFields/coordinates_field.html.twig'); }
CropField
Example CropDataTransformer
namespace App\DataTransformer; use App\Entity\File; use App\Interfaces\FileManagerInterface; use App\Service\TokenGenerator; use EasyAdminAzFields\Contracts\CropDataTransformerInterface; use EasyAdminAzFields\Dto\CropperValueDto; use RuntimeException; use Symfony\Component\HttpFoundation\File\UploadedFile; class FileDataTransformer implements CropDataTransformerInterface { public function __construct( private readonly FileManagerInterface $fileManager ) { } public function transform(mixed $value): CropperValueDto { $dto = new CropperValueDto(); if (!$value instanceof File) { return $dto->setOldImage($value); } return $dto ->setOldImage($value->getUrl()); } public function reverseTransform(mixed $value): ?string { if (!$value instanceof CropperValueDto) { return null; } if (!$value->getImage()) { return $value->getOldImage(); } $tempPath = $value->getImage(); $uploaded = new UploadedFile($tempPath, 'tempfile', null, null, true); $extension = $uploaded->guessExtension(); // Your file name $randomFileName = "random" . rand(); // Full path where you want to upload the file $uploadPath = "/{$randomFileName}.{$extension}"; // Saves file $uploaded = $this->fileManager->save($uploadPath, $uploaded->getContent()); if (!$uploaded) { throw new RuntimeException("Failed to upload the file"); } // The image path which will be passed to entity // You can return whatever you want it will be set into your entity return $this->fileManager->getFullPath($uploadPath); } }
Need more help how to use it?
Need more data than just single field? Enjoy my form for entity "File"
The entity has 3 fields: name
, alt
and url
namespace App\Form; use App\DataTransformer\FileDataTransformer; use App\Entity\File; use Doctrine\ORM\EntityManagerInterface; use EasyAdminAzFields\Dto\CropperSettingsDto; use EasyAdminAzFields\Form\CropType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\DataTransformerInterface; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\OptionsResolver\OptionsResolver; class FileForm extends AbstractType { public function __construct( private readonly EntityManagerInterface $em, private readonly FileDataTransformer $fileDataTransformer, ) { } public function buildForm(FormBuilderInterface $builder, array $options): void { $builder ->add('name', TextType::class, [ 'label' => 'Name', ]) ->add('alt', TextType::class, [ 'label' => 'Alt', 'required' => false, ]) ->add('url', CropType::class, [ 'label' => 'File', CropType::OPTION_DATA_TRANSFORMER => $options[CropType::OPTION_DATA_TRANSFORMER], CropType::OPTION_CROPPER_SETTINGS => $options[CropType::OPTION_CROPPER_SETTINGS], ])->addEventListener( FormEvents::SUBMIT, function (FormEvent $event) { $data = $event->getData(); if (!$data instanceof File) { return; } if ($data->getUrl()) { return; } $event->setData(null); $this->em->detach($data); } ); } public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'class' => File::class, 'data_class' => File::class, 'query_builder' => null, CropType::OPTION_CROPPER_SETTINGS => new CropperSettingsDto(), CropType::OPTION_DATA_TRANSFORMER => $this->fileDataTransformer, ])->setAllowedTypes( CropType::OPTION_CROPPER_SETTINGS, [ CropperSettingsDto::class ] )->setAllowedTypes( CropType::OPTION_DATA_TRANSFORMER, [ DataTransformerInterface::class ] ); } }
Field definition in CrudController
AssociationField::new('backgroundImage', "") ->setRequired(false) ->setFormType(FileForm::class) ->setFormTypeOption( CropType::OPTION_CROPPER_SETTINGS, new CropperSettingsDto() ->setAspectRatio(16 / 10) ),
CoordinatesField
Add coordinates to your entity (YourEntity)
use EasyAdminAzFields\Entity\Coordinates; #[ORM\Entity(repositoryClass: YourEntityRepository::class)] class YourEntity { #[ORM\Embedded(class: Coordinates::class)] private ?Coordinates $coordinates; public function __construct() { $this->coordinates = new Coordinates(); } public function getCoordinates(): ?Coordinates { return $this->coordinates; } public function setCoordinates(?Coordinates $coordinates): static { $this->coordinates = $coordinates; return $this; } }
Use field in CRUD
use EasyAdminAzFields\Form\CoordinatesField; CoordinatesField::new('coordinates', "Geo location") ->hideOnIndex(),