Skip to content

Commit

Permalink
IBX-154: Allowed to define if the alternative text for image field is…
Browse files Browse the repository at this point in the history
… required (2.5) (#348)
  • Loading branch information
adamwojs authored Jun 25, 2021
1 parent 4840872 commit 56b8070
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions bundle/Resources/translations/ezrepoforms_content_type.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@
<target>Maximum file size (MB)</target>
<note>key: field_definition.ezimage.max_file_size</note>
</trans-unit>
<trans-unit id="d2474547af649be4af884989e0af3e003125291e" resname="field_definition.ezimage.is_alternative_text_required">
<source>Alternative text is required</source>
<target state="new">Alternative text is required</target>
<note>key: field_definition.ezimage.is_alternative_text_required</note>
</trans-unit>
<trans-unit id="8a95f664895f9c72e5ca52ea43908ad3d54c8966" resname="field_definition.ezinteger.default_value">
<source>Default value</source>
<target>Default value</target>
Expand Down
9 changes: 9 additions & 0 deletions lib/FieldType/Mapper/ImageFormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
]);
}

Expand All @@ -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(
Expand All @@ -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))
Expand Down
18 changes: 16 additions & 2 deletions lib/Form/Type/FieldType/ImageFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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');
}
}

0 comments on commit 56b8070

Please sign in to comment.