From 78cffbeb14211326210df4cc078389527f594310 Mon Sep 17 00:00:00 2001 From: Mikolaj Adamczyk Date: Thu, 17 Oct 2024 15:02:17 +0200 Subject: [PATCH] Moved language switcher from ibexa/product-catalog (#1374) --- .../Resources/config/services/forms.yaml | 2 + .../Form/Data/AbstractLanguageSwitchData.php | 31 ++++++++++ src/lib/Form/Type/LanguageSwitchType.php | 58 +++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 src/lib/Form/Data/AbstractLanguageSwitchData.php create mode 100644 src/lib/Form/Type/LanguageSwitchType.php diff --git a/src/bundle/Resources/config/services/forms.yaml b/src/bundle/Resources/config/services/forms.yaml index a6f0893fec..bdd11af7d7 100644 --- a/src/bundle/Resources/config/services/forms.yaml +++ b/src/bundle/Resources/config/services/forms.yaml @@ -415,3 +415,5 @@ services: arguments: $siteAccessResolver: '@Ibexa\AdminUi\Siteaccess\NonAdminSiteaccessResolver' $siteAccessNameGenerator: '@Ibexa\AdminUi\Siteaccess\SiteAccessNameGenerator' + + Ibexa\AdminUi\Form\Type\LanguageSwitchType: ~ diff --git a/src/lib/Form/Data/AbstractLanguageSwitchData.php b/src/lib/Form/Data/AbstractLanguageSwitchData.php new file mode 100644 index 0000000000..4901b9c922 --- /dev/null +++ b/src/lib/Form/Data/AbstractLanguageSwitchData.php @@ -0,0 +1,31 @@ +language = $language; + } + + public function getLanguage(): ?Language + { + return $this->language; + } + + public function setLanguage(?Language $language): void + { + $this->language = $language; + } +} diff --git a/src/lib/Form/Type/LanguageSwitchType.php b/src/lib/Form/Type/LanguageSwitchType.php new file mode 100644 index 0000000000..f59b669956 --- /dev/null +++ b/src/lib/Form/Type/LanguageSwitchType.php @@ -0,0 +1,58 @@ +languageService = $languageService; + } + + public function buildForm(FormBuilderInterface $builder, array $options): void + { + $languages = $options['languages']; + + $builder->add( + 'language', + ConfiguredLanguagesChoiceType::class, + [ + 'choice_loader' => ChoiceList::lazy( + $this, + function () use ($languages): iterable { + return $this->languageService->loadLanguageListByCode($languages); + }, + $languages + ), + ] + ); + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'csrf_protection' => false, + 'data_class' => AbstractLanguageSwitchData::class, + 'method' => Request::METHOD_GET, + ]); + + $resolver->setRequired('languages'); + } +}