+ {% if show_preview %}
+
+ {% set images = field ? field.formattedValue : null %}
+ {% if images is not null %}
+ {% if images is not iterable %}{% set images = [images] %}{% endif %}
+ {% for image in images %}
+ {% if image is not empty %}
+ {% set _lightbox_id = 'ea-lightbox-' ~ form.vars.id ~ '-' ~ loop.index %}
+
+
+
+
+
 }})
+
+ {% endif %}
+ {% endfor %}
+ {% endif %}
+
+ {% endif %}
+ {{ block('ea_fileupload_widget') }}
+
+{% endblock ea_imageupload_widget %}
+
{% block TODO_ea_fileupload_widget %}
{% set placeholder = '' %}
{% set title = '' %}
diff --git a/tests/Functional/Apps/DefaultApp/src/Controller/Synthetic/ImageFieldNoPreviewCrudController.php b/tests/Functional/Apps/DefaultApp/src/Controller/Synthetic/ImageFieldNoPreviewCrudController.php
new file mode 100644
index 0000000000..a8b9557a18
--- /dev/null
+++ b/tests/Functional/Apps/DefaultApp/src/Controller/Synthetic/ImageFieldNoPreviewCrudController.php
@@ -0,0 +1,30 @@
+
+ */
+class ImageFieldNoPreviewCrudController extends AbstractCrudController
+{
+ public static function getEntityFqcn(): string
+ {
+ return FieldTestEntity::class;
+ }
+
+ public function configureFields(string $pageName): iterable
+ {
+ yield IdField::new('id')->hideOnForm();
+ yield ImageField::new('imageField')
+ ->setBasePath('uploads/')
+ ->setUploadDir('public/uploads/')
+ ->setUploadedFileNamePattern('[randomhash].[extension]')
+ ->setRequired(false)
+ ->showPreview(false);
+ }
+}
diff --git a/tests/Functional/Fields/Media/ImageFieldTest.php b/tests/Functional/Fields/Media/ImageFieldTest.php
index 44e07c1345..97a796ecde 100644
--- a/tests/Functional/Fields/Media/ImageFieldTest.php
+++ b/tests/Functional/Fields/Media/ImageFieldTest.php
@@ -3,6 +3,7 @@
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Fields\Media;
use EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\AbstractFieldFunctionalTest;
+use EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\DefaultApp\Controller\Synthetic\ImageFieldNoPreviewCrudController;
class ImageFieldTest extends AbstractFieldFunctionalTest
{
@@ -47,15 +48,17 @@ public function testImageFieldInForm(): void
$form = $crawler->filter('form[name="FieldTestEntity"]');
static::assertCount(1, $form, 'Form should exist');
- // image field may be rendered as a file input or as a container with file input
- // check that the image field container or input exists
- $imageFieldContainer = $crawler->filter('.field-image');
- $imageFieldInput = $crawler->filter('input[type="file"][name*="imageField"]');
+ // image field should be wrapped in the ea-imageupload container
+ $imageUploadContainer = $crawler->filter('.ea-imageupload');
+ static::assertGreaterThan(0, $imageUploadContainer->count(), 'Image upload container (.ea-imageupload) should exist');
- static::assertTrue(
- $imageFieldContainer->count() > 0 || $imageFieldInput->count() > 0,
- 'Image field should exist in form'
- );
+ // the preview container should be present (for JS to populate on file selection)
+ $previewContainer = $crawler->filter('[data-ea-imageupload-preview]');
+ static::assertGreaterThan(0, $previewContainer->count(), 'Preview container should exist in new form');
+
+ // a file input should exist inside the container
+ $imageFieldInput = $crawler->filter('.ea-imageupload input[type="file"]');
+ static::assertGreaterThan(0, $imageFieldInput->count(), 'File input should exist inside image upload container');
}
public function testImageFieldWithNullValue(): void
@@ -83,18 +86,63 @@ public function testImageFieldEdit(): void
$crawler = $this->client->request('GET', $this->generateEditFormUrl($entity->getId()));
- // the edit form should load successfully and contain an image field
$form = $crawler->filter('form[name="FieldTestEntity"]');
static::assertCount(1, $form, 'Edit form should exist');
- // check that image field exists in form
- $imageFieldContainer = $crawler->filter('.field-image');
- $imageFieldInput = $crawler->filter('input[type="file"][name*="imageField"]');
+ $imageUploadContainer = $crawler->filter('.ea-imageupload');
+ static::assertGreaterThan(0, $imageUploadContainer->count(), 'Image upload container should exist in edit form');
+
+ $previewContainer = $crawler->filter('[data-ea-imageupload-preview]');
+ static::assertGreaterThan(0, $previewContainer->count(), 'Preview container should exist in edit form');
+
+ $previewImage = $previewContainer->filter('.ea-lightbox-thumbnail img');
+ static::assertGreaterThan(0, $previewImage->count(), 'Preview should show existing image thumbnail');
+ static::assertStringContainsString('original-image.jpg', $previewImage->attr('src'));
+
+ $lightboxDiv = $previewContainer->filter('.ea-lightbox');
+ static::assertGreaterThan(0, $lightboxDiv->count(), 'Lightbox div should exist for the preview image');
+ }
+
+ public function testImageFieldNewFormHasEmptyPreviewContainer(): void
+ {
+ $crawler = $this->client->request('GET', $this->generateNewFormUrl());
+
+ $previewContainer = $crawler->filter('[data-ea-imageupload-preview]');
+ static::assertGreaterThan(0, $previewContainer->count(), 'Preview container should exist in new form');
+
+ $previewImages = $previewContainer->filter('.ea-lightbox-thumbnail');
+ static::assertCount(0, $previewImages, 'No image thumbnails should exist in new form preview');
+ }
+
+ public function testImageFieldShowPreviewFalseHidesPreview(): void
+ {
+ $noPreviewController = ImageFieldNoPreviewCrudController::class;
+
+ $crawler = $this->client->request('GET', $this->generateNewFormUrl(controllerFqcn: $noPreviewController));
+
+ $imageUploadContainer = $crawler->filter('.ea-imageupload');
+ static::assertGreaterThan(0, $imageUploadContainer->count(), 'Image upload container should still exist');
+
+ $previewContainer = $crawler->filter('[data-ea-imageupload-preview]');
+ static::assertCount(0, $previewContainer, 'Preview container should not exist when showPreview(false)');
+ }
+
+ public function testImageFieldShowPreviewFalseHidesPreviewOnEdit(): void
+ {
+ $noPreviewController = ImageFieldNoPreviewCrudController::class;
+
+ $entity = $this->createFieldTestEntity([
+ 'imageField' => 'hidden-preview.jpg',
+ ]);
+
+ $crawler = $this->client->request('GET', $this->generateEditFormUrl($entity->getId(), controllerFqcn: $noPreviewController));
+
+ $previewContainer = $crawler->filter('[data-ea-imageupload-preview]');
+ static::assertCount(0, $previewContainer, 'Preview container should not exist on edit when showPreview(false)');
- static::assertTrue(
- $imageFieldContainer->count() > 0 || $imageFieldInput->count() > 0,
- 'Image field should exist in edit form'
- );
+ // the file input should still work
+ $fileInput = $crawler->filter('.ea-imageupload input[type="file"]');
+ static::assertGreaterThan(0, $fileInput->count(), 'File input should still exist when preview is disabled');
}
public function testImageFieldWithDifferentExtensions(): void
diff --git a/tests/Unit/Field/ImageFieldTest.php b/tests/Unit/Field/ImageFieldTest.php
index 80a5f1cd63..9809b6aed7 100644
--- a/tests/Unit/Field/ImageFieldTest.php
+++ b/tests/Unit/Field/ImageFieldTest.php
@@ -7,7 +7,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
-use EasyCorp\Bundle\EasyAdminBundle\Form\Type\FileUploadType;
+use EasyCorp\Bundle\EasyAdminBundle\Form\Type\ImageUploadType;
use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Validator\Constraints\NotNull;
@@ -40,10 +40,27 @@ public function testDefaultOptions(): void
self::assertNull($fieldDto->getCustomOption(ImageField::OPTION_BASE_PATH));
self::assertNull($fieldDto->getCustomOption(ImageField::OPTION_UPLOAD_DIR));
self::assertSame('[name].[extension]', $fieldDto->getCustomOption(ImageField::OPTION_UPLOADED_FILE_NAME_PATTERN));
- self::assertSame(FileUploadType::class, $fieldDto->getFormType());
+ self::assertSame(ImageUploadType::class, $fieldDto->getFormType());
self::assertStringContainsString('field-image', $fieldDto->getCssClass());
}
+ public function testDefaultShowPreview(): void
+ {
+ $field = ImageField::new('image');
+ $fieldDto = $this->configure($field);
+
+ self::assertTrue($fieldDto->getCustomOption(ImageField::OPTION_SHOW_PREVIEW));
+ }
+
+ public function testShowPreviewFalse(): void
+ {
+ $field = ImageField::new('image');
+ $field->showPreview(false);
+ $fieldDto = $this->configure($field);
+
+ self::assertFalse($fieldDto->getCustomOption(ImageField::OPTION_SHOW_PREVIEW));
+ }
+
public function testDefaultFileConstraints(): void
{
$field = ImageField::new('image');