Skip to content

Commit

Permalink
added image_extensions to the FieldType forms
Browse files Browse the repository at this point in the history
  • Loading branch information
konradoboza committed Dec 12, 2023
1 parent 0634b25 commit e601590
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/lib/Form/Type/FieldType/ImageAssetFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -144,6 +148,21 @@ private function getMaxFileSize(): float

return (float)$this->maxUploadSize->get();
}

/**
* @param array<string> $mimeTypes
*
* @return array<string, array<string>>
*/
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');
26 changes: 26 additions & 0 deletions src/lib/Form/Type/FieldType/ImageFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@
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;

/**
* Form Type representing ezimage field type.
*/
class ImageFieldType extends AbstractType
{
private MimeTypesInterface $mimeTypes;

public function __construct(MimeTypesInterface $mimeTypes)
{
$this->mimeTypes = $mimeTypes;
}

public function getName()
{
return $this->getBlockPrefix();
Expand Down Expand Up @@ -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']),
];
}

Expand All @@ -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<string> $mimeTypes
*
* @return array<string, array<string>>
*/
private function getMimeTypesExtensions(array $mimeTypes): array
{
$extensions = [];
foreach ($mimeTypes as $mimeType) {
$extensions[$mimeType] = $this->mimeTypes->getExtensions($mimeType);
}

return $extensions;
}
}

Expand Down

0 comments on commit e601590

Please sign in to comment.