Skip to content

Commit

Permalink
Extracted language switch from ProductCatalog
Browse files Browse the repository at this point in the history
  • Loading branch information
mikadamczyk committed Oct 17, 2024
1 parent 44441cf commit 51a1eb5
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/bundle/Resources/config/services/forms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,5 @@ services:
arguments:
$siteAccessResolver: '@Ibexa\AdminUi\Siteaccess\NonAdminSiteaccessResolver'
$siteAccessNameGenerator: '@Ibexa\AdminUi\Siteaccess\SiteAccessNameGenerator'

Ibexa\AdminUi\Form\Type\LanguageSwitchType: ~
31 changes: 31 additions & 0 deletions src/lib/Form/Data/AbstractLanguageSwitchData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\AdminUi\Form\Data;

use Ibexa\Contracts\Core\Repository\Values\Content\Language;

abstract class AbstractLanguageSwitchData
{
private ?Language $language;

public function __construct(?Language $language = null)
{
$this->language = $language;
}

public function getLanguage(): ?Language
{
return $this->language;
}

public function setLanguage(?Language $language): void
{
$this->language = $language;
}
}
58 changes: 58 additions & 0 deletions src/lib/Form/Type/LanguageSwitchType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\AdminUi\Form\Type;

use Ibexa\AdminUi\Form\Data\AbstractLanguageSwitchData;
use Ibexa\AdminUi\Form\Type\Language\ConfiguredLanguagesChoiceType;
use Ibexa\Contracts\Core\Repository\LanguageService;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ChoiceList;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class LanguageSwitchType extends AbstractType
{
private LanguageService $languageService;

public function __construct(LanguageService $languageService)
{
$this->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');
}
}

0 comments on commit 51a1eb5

Please sign in to comment.