Skip to content

Commit

Permalink
Merge pull request #34 from netgen/nggw_120
Browse files Browse the repository at this point in the history
NGGW-120: Implement language code for selection options
  • Loading branch information
emodric authored Jun 20, 2023
2 parents b5b802e + d06f2dd commit 551340b
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 11 deletions.
1 change: 1 addition & 0 deletions bundle/Core/FieldType/EnhancedSelection/FormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
)
Expand Down
22 changes: 21 additions & 1 deletion bundle/Core/FieldType/EnhancedSelection/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
Expand Down Expand Up @@ -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'],
];
}
Expand Down
19 changes: 13 additions & 6 deletions bundle/Form/Type/FieldType/EnhancedSelectionFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions bundle/Form/Type/FieldType/OptionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions bundle/Resources/config/field_types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
2 changes: 2 additions & 0 deletions bundle/Resources/translations/content_type.en.yaml
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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' %}

<tr class="multientry-item">
{{ form_widget(form) }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
{{- form_widget(form.identifier) -}}
</td>

<td>
{{- form_errors(form.language_code) -}}
{{- form_widget(form.language_code) -}}
</td>

<td>
{{- form_errors(form.priority) -}}
{{- form_widget(form.priority) -}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<tr>
<th>{{ 'field_definition.sckenhancedselection.settings.options.name'|trans }}</th>
<th>{{ 'field_definition.sckenhancedselection.settings.options.identifier'|trans }}</th>
<th>{{ 'field_definition.sckenhancedselection.settings.options.language_code'|trans }}</th>
<th colspan="2">{{ 'field_definition.sckenhancedselection.settings.options.priority'|trans }}</th>
</tr>
</thead>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
25 changes: 24 additions & 1 deletion tests/Core/FieldType/EnhancedSelection/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand All @@ -267,6 +278,7 @@ public function testValidateFieldSettingsWithInvalidOptionsInFieldSettings(): vo
[
'name' => false,
'identifier' => false,
'language_code' => false,
'priority' => 'test',
],
],
Expand Down Expand Up @@ -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,
[
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function testToStorageFieldDefinition(): void
[
'name' => 'name',
'identifier' => 'id',
'language_code' => 'cro-HR',
'priority' => 10,
],
],
Expand All @@ -79,7 +80,7 @@ public function testToFieldDefinition(): void
$fieldDefinition = new FieldDefinition();
$storageDefinition = new StorageFieldDefinition();
$storageDefinition->dataText5 = '<?xml version="1.0"?>
<content><options><option id="3" name="None" identifier="none" priority="0"/><option id="1" name="Include" identifier="include" priority="1"/><option id="2" name="Exclude" identifier="exclude" priority="2"/></options><multiselect>1</multiselect><expanded>1</expanded><delimiter><![CDATA[]]></delimiter><query><![CDATA[]]></query></content>';
<content><options><option id="3" name="None" identifier="none" langauge_code="cro-HR" priority="0"/><option id="1" name="Include" identifier="include" langauge_code="cro-HR" priority="1"/><option id="2" name="Exclude" identifier="exclude" langauge_code="cro-HR" priority="2"/></options><multiselect>1</multiselect><expanded>1</expanded><delimiter><![CDATA[]]></delimiter><query><![CDATA[]]></query></content>';

$this->converter->toFieldDefinition($storageDefinition, $fieldDefinition);
}
Expand Down

0 comments on commit 551340b

Please sign in to comment.