Skip to content

Commit d67dca7

Browse files
IBX-6856: Added view variable mime types for ezimage field type (#55)
* IBX-6856: Added view variable mime types for ezimage field type https://issues.ibexa.co/browse/IBX-6856 * Added mime_types view variable * added `image_extensions` to the FieldType forms --------- Co-authored-by: konradoboza <[email protected]>
1 parent d300e3a commit d67dca7

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

src/lib/Form/Type/FieldType/ImageAssetFieldType.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Form\FormBuilderInterface;
2323
use Symfony\Component\Form\FormInterface;
2424
use Symfony\Component\Form\FormView;
25+
use Symfony\Component\Mime\MimeTypesInterface;
2526
use Symfony\Component\OptionsResolver\OptionsResolver;
2627
use Symfony\Component\Validator\Constraints as Assert;
2728

@@ -36,16 +37,18 @@ class ImageAssetFieldType extends AbstractType
3637
/** @var \Ibexa\ContentForms\ConfigResolver\MaxUploadSize */
3738
private $maxUploadSize;
3839

39-
/**
40-
* @param \Ibexa\Contracts\Core\Repository\ContentService $contentService
41-
* @param \Ibexa\Core\FieldType\ImageAsset\AssetMapper $mapper
42-
* @param \Ibexa\ContentForms\ConfigResolver\MaxUploadSize $maxUploadSize
43-
*/
44-
public function __construct(ContentService $contentService, AssetMapper $mapper, MaxUploadSize $maxUploadSize)
45-
{
40+
private MimeTypesInterface $mimeTypes;
41+
42+
public function __construct(
43+
ContentService $contentService,
44+
AssetMapper $mapper,
45+
MaxUploadSize $maxUploadSize,
46+
MimeTypesInterface $mimeTypes
47+
) {
4648
$this->contentService = $contentService;
4749
$this->maxUploadSize = $maxUploadSize;
4850
$this->assetMapper = $mapper;
51+
$this->mimeTypes = $mimeTypes;
4952
}
5053

5154
public function getName()
@@ -110,6 +113,15 @@ public function buildView(FormView $view, FormInterface $form, array $options)
110113
}
111114
}
112115

116+
$mimeTypes = $this->assetMapper
117+
->getAssetFieldDefinition()
118+
->getFieldSettings()['mimeTypes'] ?? [];
119+
120+
if (!empty($mimeTypes)) {
121+
$view->vars['mime_types'] = $mimeTypes;
122+
$view->vars['image_extensions'] = $this->getMimeTypesExtensions($mimeTypes);
123+
}
124+
113125
$view->vars['max_file_size'] = $this->getMaxFileSize();
114126
}
115127

@@ -136,6 +148,21 @@ private function getMaxFileSize(): float
136148

137149
return (float)$this->maxUploadSize->get();
138150
}
151+
152+
/**
153+
* @param array<string> $mimeTypes
154+
*
155+
* @return array<string, array<string>>
156+
*/
157+
private function getMimeTypesExtensions(array $mimeTypes): array
158+
{
159+
$extensions = [];
160+
foreach ($mimeTypes as $mimeType) {
161+
$extensions[$mimeType] = $this->mimeTypes->getExtensions($mimeType);
162+
}
163+
164+
return $extensions;
165+
}
139166
}
140167

141168
class_alias(ImageAssetFieldType::class, 'EzSystems\EzPlatformContentForms\Form\Type\FieldType\ImageAssetFieldType');

src/lib/Form/Type/FieldType/ImageFieldType.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@
1515
use Symfony\Component\Form\FormBuilderInterface;
1616
use Symfony\Component\Form\FormInterface;
1717
use Symfony\Component\Form\FormView;
18+
use Symfony\Component\Mime\MimeTypesInterface;
1819
use Symfony\Component\OptionsResolver\OptionsResolver;
1920

2021
/**
2122
* Form Type representing ezimage field type.
2223
*/
2324
class ImageFieldType extends AbstractType
2425
{
26+
private MimeTypesInterface $mimeTypes;
27+
28+
public function __construct(MimeTypesInterface $mimeTypes)
29+
{
30+
$this->mimeTypes = $mimeTypes;
31+
}
32+
2533
public function getName()
2634
{
2735
return $this->getBlockPrefix();
@@ -58,6 +66,8 @@ public function buildView(FormView $view, FormInterface $form, array $options)
5866
{
5967
$view->vars += [
6068
'is_alternative_text_required' => $options['is_alternative_text_required'],
69+
'mime_types' => $options['mime_types'],
70+
'image_extensions' => $this->getMimeTypesExtensions($options['mime_types']),
6171
];
6272
}
6373

@@ -66,9 +76,28 @@ public function configureOptions(OptionsResolver $resolver)
6676
$resolver->setDefaults([
6777
'translation_domain' => 'ibexa_content_forms_fieldtype',
6878
'is_alternative_text_required' => false,
79+
'mime_types' => [],
80+
'image_extensions' => [],
6981
]);
7082

7183
$resolver->setAllowedTypes('is_alternative_text_required', 'bool');
84+
$resolver->setAllowedTypes('mime_types', ['array']);
85+
$resolver->setAllowedTypes('image_extensions', ['array']);
86+
}
87+
88+
/**
89+
* @param array<string> $mimeTypes
90+
*
91+
* @return array<string, array<string>>
92+
*/
93+
private function getMimeTypesExtensions(array $mimeTypes): array
94+
{
95+
$extensions = [];
96+
foreach ($mimeTypes as $mimeType) {
97+
$extensions[$mimeType] = $this->mimeTypes->getExtensions($mimeType);
98+
}
99+
100+
return $extensions;
72101
}
73102
}
74103

0 commit comments

Comments
 (0)