From f2ba285349b7ae08df23b9338570c58a9871fce3 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sat, 29 Jul 2023 13:37:08 +0200 Subject: [PATCH] add doc about filterHandler --- docs/crud/configuration.md | 6 +++--- docs/utils/file_handler.md | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/docs/crud/configuration.md b/docs/crud/configuration.md index 7c5bb2b..3a8e818 100644 --- a/docs/crud/configuration.md +++ b/docs/crud/configuration.md @@ -72,9 +72,9 @@ Override a view. | `form_widget` | `@Core/admin/crud/_form_widget.html.twig` | Template to render a form widget | | `form_translations` | `@Core/admin/crud/_form_translations.html.twig` | Template to render a the translation field | -| Entity (context) | View | Description | -| ------ | ---- | ----------- | -| `show_entity` | `@Core/admin/crud/_show.html.twig` | | Template to render the entity | +| Entity (context) | View | Description | +| ------ | ---- | ----------- | +| `show_entity` | `@Core/admin/crud/_show.html.twig` | Template to render the entity | ## setViewDatas diff --git a/docs/utils/file_handler.md b/docs/utils/file_handler.md index b9b0151..03f6303 100644 --- a/docs/utils/file_handler.md +++ b/docs/utils/file_handler.md @@ -21,14 +21,14 @@ public function upload(Request $request, FileUploadHandler $fileUpload) $keepOriginalFilename = false; $fileUpload->handleForm( - $form->get('image')->getData(), // Symfony\Component\HttpFoundation\File\UploadedFile or null - $fileDirectory, + uploadedFile: $form->get('image')->getData(), // Symfony\Component\HttpFoundation\File\UploadedFile or null + path: $fileDirectory, // optional - function ($filename) use ($entity, $fileDirectory) { + afterUploadCallback: function ($filename) use ($entity, $fileDirectory) { $entity->setImage($fileDirectory.$filename); }, // optional - $keepOriginalFilename + keepOriginalFilename: $keepOriginalFilename ); // ... @@ -36,3 +36,13 @@ public function upload(Request $request, FileUploadHandler $fileUpload) } } ``` + +If you need to generate custom filenames, `FileUploadHandler` allows you to define a generator: + +``` +use Symfony\Component\HttpFoundation\File\UploadedFile; + +$fileUpload->setFilenameGenerator(function(UploadedFile $file) { + return sprintf('%d.%s', mt_rand(), $file->guessExtension()); +}); +```