diff --git a/src/lib/Form/Type/FieldType/ImageAssetFieldType.php b/src/lib/Form/Type/FieldType/ImageAssetFieldType.php index 40ff8c21..0fc17965 100644 --- a/src/lib/Form/Type/FieldType/ImageAssetFieldType.php +++ b/src/lib/Form/Type/FieldType/ImageAssetFieldType.php @@ -22,6 +22,7 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; +use Symfony\Component\Mime\MimeTypesInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Validator\Constraints as Assert; @@ -36,16 +37,18 @@ class ImageAssetFieldType extends AbstractType /** @var \Ibexa\ContentForms\ConfigResolver\MaxUploadSize */ private $maxUploadSize; - /** - * @param \Ibexa\Contracts\Core\Repository\ContentService $contentService - * @param \Ibexa\Core\FieldType\ImageAsset\AssetMapper $mapper - * @param \Ibexa\ContentForms\ConfigResolver\MaxUploadSize $maxUploadSize - */ - public function __construct(ContentService $contentService, AssetMapper $mapper, MaxUploadSize $maxUploadSize) - { + private MimeTypesInterface $mimeTypes; + + public function __construct( + ContentService $contentService, + AssetMapper $mapper, + MaxUploadSize $maxUploadSize, + MimeTypesInterface $mimeTypes + ) { $this->contentService = $contentService; $this->maxUploadSize = $maxUploadSize; $this->assetMapper = $mapper; + $this->mimeTypes = $mimeTypes; } public function getName() @@ -116,6 +119,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) if (!empty($mimeTypes)) { $view->vars['mime_types'] = $mimeTypes; + $view->vars['image_extensions'] = $this->getMimeTypesExtensions($mimeTypes); } $view->vars['max_file_size'] = $this->getMaxFileSize(); @@ -144,6 +148,21 @@ private function getMaxFileSize(): float return (float)$this->maxUploadSize->get(); } + + /** + * @param array $mimeTypes + * + * @return array> + */ + private function getMimeTypesExtensions(array $mimeTypes): array + { + $extensions = []; + foreach ($mimeTypes as $mimeType) { + $extensions[$mimeType] = $this->mimeTypes->getExtensions($mimeType); + } + + return $extensions; + } } class_alias(ImageAssetFieldType::class, 'EzSystems\EzPlatformContentForms\Form\Type\FieldType\ImageAssetFieldType'); diff --git a/src/lib/Form/Type/FieldType/ImageFieldType.php b/src/lib/Form/Type/FieldType/ImageFieldType.php index 82e7df0a..9f0c25ab 100644 --- a/src/lib/Form/Type/FieldType/ImageFieldType.php +++ b/src/lib/Form/Type/FieldType/ImageFieldType.php @@ -15,6 +15,7 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; +use Symfony\Component\Mime\MimeTypesInterface; use Symfony\Component\OptionsResolver\OptionsResolver; /** @@ -22,6 +23,13 @@ */ class ImageFieldType extends AbstractType { + private MimeTypesInterface $mimeTypes; + + public function __construct(MimeTypesInterface $mimeTypes) + { + $this->mimeTypes = $mimeTypes; + } + public function getName() { return $this->getBlockPrefix(); @@ -59,6 +67,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) $view->vars += [ 'is_alternative_text_required' => $options['is_alternative_text_required'], 'mime_types' => $options['mime_types'], + 'image_extensions' => $this->getMimeTypesExtensions($options['mime_types']), ]; } @@ -68,10 +77,27 @@ public function configureOptions(OptionsResolver $resolver) 'translation_domain' => 'ibexa_content_forms_fieldtype', 'is_alternative_text_required' => false, 'mime_types' => [], + 'image_extensions' => [], ]); $resolver->setAllowedTypes('is_alternative_text_required', 'bool'); $resolver->setAllowedTypes('mime_types', ['array']); + $resolver->setAllowedTypes('image_extensions', ['array']); + } + + /** + * @param array $mimeTypes + * + * @return array> + */ + private function getMimeTypesExtensions(array $mimeTypes): array + { + $extensions = []; + foreach ($mimeTypes as $mimeType) { + $extensions[$mimeType] = $this->mimeTypes->getExtensions($mimeType); + } + + return $extensions; } }