diff --git a/bundle/Core/FieldType/EnhancedSelection/FormMapper.php b/bundle/Core/FieldType/EnhancedSelection/FormMapper.php index 948203c..32568ab 100644 --- a/bundle/Core/FieldType/EnhancedSelection/FormMapper.php +++ b/bundle/Core/FieldType/EnhancedSelection/FormMapper.php @@ -30,6 +30,7 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data): vo [ 'required' => $data->fieldDefinition->isRequired, 'label' => $data->fieldDefinition->getName(), + 'language_code' => $data->field->languageCode, 'field_definition' => $data->fieldDefinition, ] ) diff --git a/bundle/Core/FieldType/EnhancedSelection/Type.php b/bundle/Core/FieldType/EnhancedSelection/Type.php index 9dcce4f..5b94f55 100644 --- a/bundle/Core/FieldType/EnhancedSelection/Type.php +++ b/bundle/Core/FieldType/EnhancedSelection/Type.php @@ -267,9 +267,29 @@ public function validateFieldSettings($fieldSettings): array } } + if (!isset($option['language_code'])) { + $validationErrors[] = new ValidationError( + "'%setting%' setting value item must have a 'language_code' property", + null, + [ + '%setting%' => $name, + ] + ); + } else { + if (!is_string($option['language_code'])) { + $validationErrors[] = new ValidationError( + "'%setting%' setting value item's 'language_code' property must be of string value", + null, + [ + '%setting%' => $name, + ] + ); + } + } + if (!isset($option['priority'])) { $validationErrors[] = new ValidationError( - "'%setting%' setting value item must have an 'priority' property", + "'%setting%' setting value item must have a 'priority' property", null, [ '%setting%' => $name, diff --git a/bundle/Core/Persistence/Legacy/Content/FieldValue/Converter/EnhancedSelectionConverter.php b/bundle/Core/Persistence/Legacy/Content/FieldValue/Converter/EnhancedSelectionConverter.php index 2195618..7b72c53 100644 --- a/bundle/Core/Persistence/Legacy/Content/FieldValue/Converter/EnhancedSelectionConverter.php +++ b/bundle/Core/Persistence/Legacy/Content/FieldValue/Converter/EnhancedSelectionConverter.php @@ -48,6 +48,7 @@ public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageField $optionNode->setAttribute('id', (string) ($key + 1)); $optionNode->setAttribute('name', (string) $option['name']); $optionNode->setAttribute('identifier', (string) $option['identifier']); + $optionNode->setAttribute('language_code', (string) $option['language_code']); $optionNode->setAttribute('priority', (string) $option['priority']); } } @@ -99,6 +100,7 @@ public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefin 'id' => (int) $option['id'], 'name' => (string) $option['name'], 'identifier' => (string) $option['identifier'], + 'language_code' => (string) $option['language_code'], 'priority' => (int) $option['priority'], ]; } diff --git a/bundle/Form/Type/FieldType/EnhancedSelectionFieldType.php b/bundle/Form/Type/FieldType/EnhancedSelectionFieldType.php index 040baa1..52403ed 100644 --- a/bundle/Form/Type/FieldType/EnhancedSelectionFieldType.php +++ b/bundle/Form/Type/FieldType/EnhancedSelectionFieldType.php @@ -24,28 +24,35 @@ public function __construct(FieldTypeService $fieldTypeService) public function configureOptions(OptionsResolver $resolver): void { - $resolver->setRequired(['field_definition']); + $resolver->setRequired(['field_definition', 'language_code']); + $resolver->setAllowedTypes('field_definition', FieldDefinition::class); + $resolver->setAllowedTypes('language_code', 'string'); } public function buildForm(FormBuilderInterface $builder, array $options): void { /** @var \Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition $fieldDefinition */ $fieldDefinition = $options['field_definition']; - $options = $fieldDefinition->fieldSettings['options']; + $fieldOptions = $fieldDefinition->fieldSettings['options']; + + /** @var string $languageCode */ + $languageCode = $options['language_code']; usort( - $options, + $fieldOptions, static fn (array $option1, array $option2): int => $option2['priority'] <=> $option1['priority'] ); $choices = []; - foreach ($options as $option) { - if ($option['identifier'] === '' && $fieldDefinition->isRequired) { + foreach ($fieldOptions as $fieldOption) { + if ($fieldOption['identifier'] === '' && $fieldDefinition->isRequired) { continue; } - $choices[$option['name']] = $option['identifier']; + if ($fieldOption['language_code'] === $languageCode || $fieldOption['language_code'] === '') { + $choices[$fieldOption['name']] = $fieldOption['identifier']; + } } $builder diff --git a/bundle/Form/Type/FieldType/OptionType.php b/bundle/Form/Type/FieldType/OptionType.php index c54482f..3bf26b2 100644 --- a/bundle/Form/Type/FieldType/OptionType.php +++ b/bundle/Form/Type/FieldType/OptionType.php @@ -4,15 +4,33 @@ namespace Netgen\Bundle\EnhancedSelectionBundle\Form\Type\FieldType; +use Generator; +use Ibexa\Contracts\Core\Repository\LanguageService; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\NumberType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; final class OptionType extends AbstractType { + private LanguageService $languageService; + + public function __construct(LanguageService $languageService) + { + $this->languageService = $languageService; + } + public function buildForm(FormBuilderInterface $builder, array $options): void { + $languageCodeChoices = (function (): Generator { + yield 'field_definition.sckenhancedselection.settings.options.all_languages' => ''; + + foreach ($this->languageService->loadLanguages() as $language) { + yield $language->getName() => $language->languageCode; + } + })(); + $builder ->add( 'name', @@ -31,6 +49,15 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'label' => 'field_definition.sckenhancedselection.settings.options.identifier', ] ) + ->add( + 'language_code', + ChoiceType::class, + [ + 'required' => false, + 'label' => 'field_definition.sckenhancedselection.settings.options.language_code', + 'choices' => $languageCodeChoices, + ] + ) ->add( 'priority', NumberType::class, diff --git a/bundle/Resources/config/field_types.yaml b/bundle/Resources/config/field_types.yaml index fc9873c..f8b6145 100644 --- a/bundle/Resources/config/field_types.yaml +++ b/bundle/Resources/config/field_types.yaml @@ -44,3 +44,10 @@ services: - "@ibexa.api.service.field_type" tags: - { name: form.type } + + netgen.enhanced_selection.field_type.form.option_type: + class: Netgen\Bundle\EnhancedSelectionBundle\Form\Type\FieldType\OptionType + arguments: + - "@ibexa.api.service.language" + tags: + - { name: form.type } diff --git a/bundle/Resources/translations/content_type.en.yaml b/bundle/Resources/translations/content_type.en.yaml index edbf267..a408f10 100644 --- a/bundle/Resources/translations/content_type.en.yaml +++ b/bundle/Resources/translations/content_type.en.yaml @@ -1,6 +1,8 @@ field_definition.sckenhancedselection.settings.options: 'Options' +field_definition.sckenhancedselection.settings.options.all_languages: 'All languages' field_definition.sckenhancedselection.settings.options.name: 'Name' field_definition.sckenhancedselection.settings.options.identifier: 'Identifier' +field_definition.sckenhancedselection.settings.options.language_code: 'Language code' field_definition.sckenhancedselection.settings.options.priority: 'Priority' field_definition.sckenhancedselection.settings.options.add_option: 'Add option' field_definition.sckenhancedselection.settings.is_multiple: 'Multiple' diff --git a/bundle/Resources/views/field_definition/edit/macros.html.twig b/bundle/Resources/views/field_definition/edit/macros.html.twig index 914b162..f046685 100644 --- a/bundle/Resources/views/field_definition/edit/macros.html.twig +++ b/bundle/Resources/views/field_definition/edit/macros.html.twig @@ -1,5 +1,5 @@ {% macro option_prototype(form) %} - {% form_theme form '@NetgenEnhancedSelection/field_definition/edit/option_widget.twig' %} + {% form_theme form '@NetgenEnhancedSelection/field_definition/edit/option_widget.html.twig' %} {{ form_widget(form) }} diff --git a/bundle/Resources/views/field_definition/edit/option_widget.twig b/bundle/Resources/views/field_definition/edit/option_widget.html.twig similarity index 79% rename from bundle/Resources/views/field_definition/edit/option_widget.twig rename to bundle/Resources/views/field_definition/edit/option_widget.html.twig index a696d28..8b5c544 100644 --- a/bundle/Resources/views/field_definition/edit/option_widget.twig +++ b/bundle/Resources/views/field_definition/edit/option_widget.html.twig @@ -11,6 +11,11 @@ {{- form_widget(form.identifier) -}} + + {{- form_errors(form.language_code) -}} + {{- form_widget(form.language_code) -}} + + {{- form_errors(form.priority) -}} {{- form_widget(form.priority) -}} diff --git a/bundle/Resources/views/field_definition/edit/sckenhancedselection.html.twig b/bundle/Resources/views/field_definition/edit/sckenhancedselection.html.twig index 544ce87..388abf6 100644 --- a/bundle/Resources/views/field_definition/edit/sckenhancedselection.html.twig +++ b/bundle/Resources/views/field_definition/edit/sckenhancedselection.html.twig @@ -13,6 +13,7 @@ {{ 'field_definition.sckenhancedselection.settings.options.name'|trans }} {{ 'field_definition.sckenhancedselection.settings.options.identifier'|trans }} + {{ 'field_definition.sckenhancedselection.settings.options.language_code'|trans }} {{ 'field_definition.sckenhancedselection.settings.options.priority'|trans }} diff --git a/bundle/Resources/views/sckenhancedselection_content_field.html.twig b/bundle/Resources/views/sckenhancedselection_content_field.html.twig index 5342889..a5ad616 100644 --- a/bundle/Resources/views/sckenhancedselection_content_field.html.twig +++ b/bundle/Resources/views/sckenhancedselection_content_field.html.twig @@ -14,7 +14,7 @@ {% set options = [] %} {% for option in available_options %} - {% if option.identifier in identifiers %} + {% if option.identifier in identifiers and option.language_code in [field.languageCode, ''] %} {% set options = options|merge([option.name]) %} {% endif %} {% endfor %} diff --git a/tests/Core/FieldType/EnhancedSelection/TypeTest.php b/tests/Core/FieldType/EnhancedSelection/TypeTest.php index 632fd29..953069b 100644 --- a/tests/Core/FieldType/EnhancedSelection/TypeTest.php +++ b/tests/Core/FieldType/EnhancedSelection/TypeTest.php @@ -241,7 +241,15 @@ public function testValidateFieldSettingsWithMissingOptionsInFieldSettings(): vo ); $validationError3 = new ValidationError( - "'%setting%' setting value item must have an 'priority' property", + "'%setting%' setting value item must have a 'language_code' property", + null, + [ + '%setting%' => 'options', + ] + ); + + $validationError4 = new ValidationError( + "'%setting%' setting value item must have a 'priority' property", null, [ '%setting%' => 'options', @@ -258,6 +266,9 @@ public function testValidateFieldSettingsWithMissingOptionsInFieldSettings(): vo self::assertSame($validationError3->getTarget(), $errors[2]->getTarget()); self::assertSame((string) $validationError3->getTranslatableMessage(), (string) $errors[2]->getTranslatableMessage()); + + self::assertSame($validationError4->getTarget(), $errors[3]->getTarget()); + self::assertSame((string) $validationError4->getTranslatableMessage(), (string) $errors[3]->getTranslatableMessage()); } public function testValidateFieldSettingsWithInvalidOptionsInFieldSettings(): void @@ -267,6 +278,7 @@ public function testValidateFieldSettingsWithInvalidOptionsInFieldSettings(): vo [ 'name' => false, 'identifier' => false, + 'language_code' => false, 'priority' => 'test', ], ], @@ -301,6 +313,14 @@ public function testValidateFieldSettingsWithInvalidOptionsInFieldSettings(): vo ); $validationError4 = new ValidationError( + "'%setting%' setting value item's 'language_code' property must be of string value", + null, + [ + '%setting%' => 'options', + ] + ); + + $validationError5 = new ValidationError( "'%setting%' setting value item's 'priority' property must be of numeric value", null, [ @@ -321,6 +341,9 @@ public function testValidateFieldSettingsWithInvalidOptionsInFieldSettings(): vo self::assertSame($validationError4->getTarget(), $errors[3]->getTarget()); self::assertSame((string) $validationError4->getTranslatableMessage(), (string) $errors[3]->getTranslatableMessage()); + + self::assertSame($validationError5->getTarget(), $errors[4]->getTarget()); + self::assertSame((string) $validationError5->getTranslatableMessage(), (string) $errors[4]->getTranslatableMessage()); } public function testAcceptValueWithSingle(): void diff --git a/tests/Core/Persistence/Legacy/Content/FieldValue/Converter/EnhancedSelectionConverterTest.php b/tests/Core/Persistence/Legacy/Content/FieldValue/Converter/EnhancedSelectionConverterTest.php index 7f2ac56..a9dd7d7 100644 --- a/tests/Core/Persistence/Legacy/Content/FieldValue/Converter/EnhancedSelectionConverterTest.php +++ b/tests/Core/Persistence/Legacy/Content/FieldValue/Converter/EnhancedSelectionConverterTest.php @@ -63,6 +63,7 @@ public function testToStorageFieldDefinition(): void [ 'name' => 'name', 'identifier' => 'id', + 'language_code' => 'cro-HR', 'priority' => 10, ], ], @@ -79,7 +80,7 @@ public function testToFieldDefinition(): void $fieldDefinition = new FieldDefinition(); $storageDefinition = new StorageFieldDefinition(); $storageDefinition->dataText5 = ' - 11'; + 11'; $this->converter->toFieldDefinition($storageDefinition, $fieldDefinition); }