From 56b80703a1f2189b3175128eef017102e2c3c659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Fri, 25 Jun 2021 10:38:52 +0200 Subject: [PATCH] IBX-154: Allowed to define if the alternative text for image field is required (2.5) (#348) --- .../ezrepoforms_content_type.en.xlf | 5 +++++ lib/FieldType/Mapper/ImageFormMapper.php | 9 +++++++++ lib/Form/Type/FieldType/ImageFieldType.php | 18 ++++++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/bundle/Resources/translations/ezrepoforms_content_type.en.xlf b/bundle/Resources/translations/ezrepoforms_content_type.en.xlf index 538f51c82..6bff1f9dd 100644 --- a/bundle/Resources/translations/ezrepoforms_content_type.en.xlf +++ b/bundle/Resources/translations/ezrepoforms_content_type.en.xlf @@ -271,6 +271,11 @@ Maximum file size (MB) key: field_definition.ezimage.max_file_size + + Alternative text is required + Alternative text is required + key: field_definition.ezimage.is_alternative_text_required + Default value Default value diff --git a/lib/FieldType/Mapper/ImageFormMapper.php b/lib/FieldType/Mapper/ImageFormMapper.php index 9e4acb655..db97fb2db 100644 --- a/lib/FieldType/Mapper/ImageFormMapper.php +++ b/lib/FieldType/Mapper/ImageFormMapper.php @@ -15,6 +15,7 @@ use EzSystems\RepositoryForms\FieldType\FieldValueFormMapperInterface; use EzSystems\RepositoryForms\Form\Type\FieldType\ImageFieldType; use EzSystems\RepositoryForms\ConfigResolver\MaxUploadSize; +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\IntegerType; use Symfony\Component\Form\FormInterface; use Symfony\Component\OptionsResolver\Exception\AccessException; @@ -54,6 +55,12 @@ public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, Field 'max' => $this->maxUploadSize->get(MaxUploadSize::MEGABYTES), ], 'disabled' => $isTranslation, + ]) + ->add('isAlternativeTextRequired', CheckboxType::class, [ + 'required' => false, + 'property_path' => 'validatorConfiguration[AlternativeTextValidator][required]', + 'label' => /** @Desc("Alternative text is required") */ 'field_definition.ezimage.is_alternative_text_required', + 'disabled' => $isTranslation, ]); } @@ -62,6 +69,7 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data) $fieldDefinition = $data->fieldDefinition; $formConfig = $fieldForm->getConfig(); $fieldType = $this->fieldTypeService->getFieldType($fieldDefinition->fieldTypeIdentifier); + $isAlternativeTextRequired = $fieldDefinition->validatorConfiguration['AlternativeTextValidator']['required'] ?? false; $fieldForm ->add( @@ -72,6 +80,7 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data) [ 'required' => $fieldDefinition->isRequired, 'label' => $fieldDefinition->getName(), + 'is_alternative_text_required' => $isAlternativeTextRequired, ] ) ->addModelTransformer(new ImageValueTransformer($fieldType, $data->value, Value::class)) diff --git a/lib/Form/Type/FieldType/ImageFieldType.php b/lib/Form/Type/FieldType/ImageFieldType.php index 452b5d8df..1c691ea7f 100644 --- a/lib/Form/Type/FieldType/ImageFieldType.php +++ b/lib/Form/Type/FieldType/ImageFieldType.php @@ -8,6 +8,8 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; /** @@ -38,13 +40,25 @@ public function buildForm(FormBuilderInterface $builder, array $options) TextType::class, [ 'label' => /** @Desc("Alternative text") */ 'content.field_type.ezimage.alternative_text', - 'required' => false, + 'required' => $options['is_alternative_text_required'], ] ); } + public function buildView(FormView $view, FormInterface $form, array $options) + { + $view->vars += [ + 'is_alternative_text_required' => $options['is_alternative_text_required'], + ]; + } + public function configureOptions(OptionsResolver $resolver) { - $resolver->setDefaults(['translation_domain' => 'ezrepoforms_fieldtype']); + $resolver->setDefaults([ + 'translation_domain' => 'ezrepoforms_fieldtype', + 'is_alternative_text_required' => false, + ]); + + $resolver->setAllowedTypes('is_alternative_text_required', 'bool'); } }