diff --git a/src/Form/Type/DataSourceSynonymRowType.php b/src/Form/Type/DataSourceSynonymRowType.php index abef12de..c35ad81f 100644 --- a/src/Form/Type/DataSourceSynonymRowType.php +++ b/src/Form/Type/DataSourceSynonymRowType.php @@ -22,10 +22,12 @@ declare(strict_types=1); namespace App\Form\Type; +use App\Services\ElementTypes; use App\Settings\SystemSettings\LocalizationSettings; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; +use Symfony\Component\Form\Extension\Core\Type\EnumType; use Symfony\Component\Form\Extension\Core\Type\LocaleType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; @@ -38,6 +40,16 @@ use Symfony\Component\Validator\Constraints as Assert; */ class DataSourceSynonymRowType extends AbstractType { + + private const PREFFERED_TYPES = [ + ElementTypes::CATEGORY, + ElementTypes::STORAGE_LOCATION, + ElementTypes::FOOTPRINT, + ElementTypes::MANUFACTURER, + ElementTypes::SUPPLIER, + ElementTypes::PROJECT, + ]; + public function __construct( private readonly LocalizationSettings $localizationSettings, #[Autowire(param: 'partdb.locale_menu')] private readonly array $preferredLanguagesParam, @@ -47,19 +59,19 @@ class DataSourceSynonymRowType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options): void { $builder - ->add('dataSource', ChoiceType::class, [ - //'label' => 'settings.behavior.data_source_synonyms.row_type.form.datasource', + ->add('dataSource', EnumType::class, [ + 'class' => ElementTypes::class, 'label' => false, - 'choices' => $this->buildDataSourceChoices($options['data_sources']), + //'choices' => $this->buildDataSourceChoices($options['data_sources']), 'required' => true, 'constraints' => [ new Assert\NotBlank(), ], 'row_attr' => ['class' => 'mb-0'], - 'attr' => ['class' => 'form-select-sm'] + 'attr' => ['class' => 'form-select-sm'], + 'preferred_choices' => self::PREFFERED_TYPES ]) ->add('locale', LocaleType::class, [ - //'label' => 'settings.behavior.data_source_synonyms.row_type.form.locale', 'label' => false, 'required' => true, // Restrict to languages configured in the language menu: disable ChoiceLoader and provide explicit choices @@ -73,7 +85,6 @@ class DataSourceSynonymRowType extends AbstractType 'attr' => ['class' => 'form-select-sm'] ]) ->add('translation_singular', TextType::class, [ - //'label' => 'settings.behavior.data_source_synonyms.row_type.form.translation_singular', 'label' => false, 'required' => true, 'empty_data' => '', @@ -84,7 +95,6 @@ class DataSourceSynonymRowType extends AbstractType 'attr' => ['class' => 'form-select-sm'] ]) ->add('translation_plural', TextType::class, [ - //'label' => 'settings.behavior.data_source_synonyms.row_type.form.translation_plural', 'label' => false, 'required' => true, 'empty_data' => '', @@ -96,14 +106,6 @@ class DataSourceSynonymRowType extends AbstractType ]); } - private function buildDataSourceChoices(array $dataSources): array - { - $choices = []; - foreach ($dataSources as $key => $label) { - $choices[(string)$label] = (string)$key; - } - return $choices; - } /** * Returns only locales configured in the language menu (settings) or falls back to the parameter. @@ -140,10 +142,4 @@ class DataSourceSynonymRowType extends AbstractType { return array_values($this->preferredLanguagesParam); } - - public function configureOptions(OptionsResolver $resolver): void - { - $resolver->setRequired('data_sources'); - $resolver->setAllowedTypes('data_sources', 'array'); - } } diff --git a/src/Form/Type/DataSourceSynonymsCollectionType.php b/src/Form/Type/DataSourceSynonymsCollectionType.php index 5507d507..c8c44a50 100644 --- a/src/Form/Type/DataSourceSynonymsCollectionType.php +++ b/src/Form/Type/DataSourceSynonymsCollectionType.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Form\Type; +use App\Services\ElementTypes; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\Extension\Core\Type\CollectionType; @@ -12,7 +13,6 @@ use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\Intl\Locales; -use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Contracts\Translation\TranslatorInterface; @@ -44,7 +44,8 @@ class DataSourceSynonymsCollectionType extends AbstractType continue; } $out[] = [ - 'dataSource' => $dataSource, + //Convert string to enum value + 'dataSource' => ElementTypes::from($dataSource), 'locale' => $locale, 'translation_singular' => $translations['singular'] ?? '', 'translation_plural' => $translations['plural'] ?? '', @@ -78,13 +79,13 @@ class DataSourceSynonymsCollectionType extends AbstractType $translation_singular = $row['translation_singular'] ?? null; $translation_plural = $row['translation_plural'] ?? null; - if (!is_string($dataSource) || $dataSource === '' - || !is_string($locale) || $locale === '' + if ($dataSource === null || + !is_string($locale) || $locale === '' ) { continue; } - $out[$dataSource][$locale] = [ + $out[$dataSource->value][$locale] = [ 'singular' => is_string($translation_singular) ? $translation_singular : '', 'plural' => is_string($translation_plural) ? $translation_plural : '', ]; @@ -153,8 +154,8 @@ class DataSourceSynonymsCollectionType extends AbstractType $sortable = $rows; usort($sortable, static function ($a, $b) { - $aDs = (string)($a['dataSource'] ?? ''); - $bDs = (string)($b['dataSource'] ?? ''); + $aDs = $a['dataSource']?->value ?? ''; + $bDs = $b['dataSource']?->value ?? ''; $cmpDs = strcasecmp($aDs, $bDs); if ($cmpDs !== 0) { @@ -176,8 +177,6 @@ class DataSourceSynonymsCollectionType extends AbstractType public function configureOptions(OptionsResolver $resolver): void { - $resolver->setRequired(['data_sources']); - $resolver->setAllowedTypes('data_sources', 'array'); // Defaults for the collection and entry type $resolver->setDefaults([ @@ -189,14 +188,7 @@ class DataSourceSynonymsCollectionType extends AbstractType 'prototype' => true, 'empty_data' => [], 'entry_options' => ['label' => false], - 'error_translation_domain' => 'validators', ]); - - // Pass data_sources automatically to each row (DataSourceSynonymRowType) - $resolver->setNormalizer('entry_options', function (Options $options, $value) { - $value = is_array($value) ? $value : []; - return $value + ['data_sources' => $options['data_sources']]; - }); } public function getParent(): ?string diff --git a/src/Services/ElementTypes.php b/src/Services/ElementTypes.php index 0ac92226..6ce8f851 100644 --- a/src/Services/ElementTypes.php +++ b/src/Services/ElementTypes.php @@ -225,4 +225,5 @@ enum ElementTypes: string implements TranslatableInterface throw new EntityNotSupportedException(sprintf('No localized label for the element with type %s was found!', $className)); } + } diff --git a/src/Settings/SystemSettings/DataSourceSynonymsSettings.php b/src/Settings/SystemSettings/DataSourceSynonymsSettings.php index ab0cb053..9ebe557d 100644 --- a/src/Settings/SystemSettings/DataSourceSynonymsSettings.php +++ b/src/Settings/SystemSettings/DataSourceSynonymsSettings.php @@ -48,14 +48,6 @@ class DataSourceSynonymsSettings formType: DataSourceSynonymsCollectionType::class, formOptions: [ 'required' => false, - 'data_sources' => [ - 'category' => new TM("settings.behavior.data_source_synonyms.category"), - 'storagelocation' => new TM("settings.behavior.data_source_synonyms.storagelocation"), - 'footprint' => new TM("settings.behavior.data_source_synonyms.footprint"), - 'manufacturer' => new TM("settings.behavior.data_source_synonyms.manufacturer"), - 'supplier' => new TM("settings.behavior.data_source_synonyms.supplier"), - 'project' => new TM("settings.behavior.data_source_synonyms.project"), - ], ], )] #[Assert\Type('array')]