Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Form/Type/TranslationsFormsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* locale_labels: array<string, string>|null,
* theming_granularity: string,
* form_options: array<string, mixed>,
* form_type: string,
* form_type: class-string<\Symfony\Component\Form\FormTypeInterface<mixed>>,
* ...
* }
*
Expand Down
10 changes: 6 additions & 4 deletions src/Form/Type/TranslationsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,14 @@ private function buildKnp(FormBuilderInterface $builder, array $options): void
])->all();

foreach ($options['enabled_locales'] as $locale) {
$required = \in_array($locale, $options['required_locales'], true);

$localeFormBuilder = $builder->create($locale, FormType::class, [
'data_class' => $options['translation_class'],
'setter' => static fn (...$args) => self::knpLocaleSetter($locale, ...$args), // @phpstan-ignore argument.unpackNonIterable, argument.type
'setter' => static fn (...$args) => self::knpLocaleSetter($locale, $required, ...$args), // @phpstan-ignore argument.unpackNonIterable, argument.type
// LocaleExtension options process
'label' => $options['locale_labels'][$locale] ?? null,
'required' => \in_array($locale, $options['required_locales'], true),
'required' => $required,
'block_name' => ('field' === $options['theming_granularity']) ? 'locale' : null,
]);

Expand Down Expand Up @@ -234,13 +236,13 @@ private function buildGedmo(FormBuilderInterface $builder, array $options): void
* @param Collection<int, \Stub\KnpTranslation> $translationColl
* @param ?\Stub\KnpTranslation $translation
*/
private static function knpLocaleSetter(string $locale, Collection $translationColl, ?object $translation): void
private static function knpLocaleSetter(string $locale, bool $required, Collection $translationColl, ?object $translation): void
{
if (null === $translation) {
return;
}

if ($translation->isEmpty()) {
if ($translation->isEmpty() && !$required) {
$translationColl->removeElement($translation);

return;
Expand Down
4 changes: 4 additions & 0 deletions tests/Form/Type/GedmoTranslationsTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
namespace A2lix\TranslationFormBundle\Tests\Form\Type;

use A2lix\AutoFormBundle\Form\Type\AutoType;
use A2lix\TranslationFormBundle\Form\Extension\LocaleExtension;
use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use A2lix\TranslationFormBundle\LocaleProvider\SimpleLocaleProvider;
use A2lix\TranslationFormBundle\Tests\Fixtures\Entity\Product;
use A2lix\TranslationFormBundle\Tests\Fixtures\Entity\ProductTranslation;
use A2lix\TranslationFormBundle\Tests\Form\TypeTestCase;
Expand All @@ -27,6 +29,8 @@
* @internal
*/
#[CoversClass(TranslationsType::class)]
#[CoversClass(LocaleExtension::class)]
#[CoversClass(SimpleLocaleProvider::class)]
#[AllowMockObjectsWithoutExpectations] // https://github.com/symfony/symfony/issues/62669
final class GedmoTranslationsTypeTest extends TypeTestCase
{
Expand Down
4 changes: 4 additions & 0 deletions tests/Form/Type/KnpTranslationsTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
namespace A2lix\TranslationFormBundle\Tests\Form\Type;

use A2lix\AutoFormBundle\Form\Type\AutoType;
use A2lix\TranslationFormBundle\Form\Extension\LocaleExtension;
use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use A2lix\TranslationFormBundle\LocaleProvider\SimpleLocaleProvider;
use A2lix\TranslationFormBundle\Tests\Fixtures\Entity\Company;
use A2lix\TranslationFormBundle\Tests\Fixtures\Entity\CompanyTranslation;
use A2lix\TranslationFormBundle\Tests\Form\TypeTestCase;
Expand All @@ -27,6 +29,8 @@
* @internal
*/
#[CoversClass(TranslationsType::class)]
#[CoversClass(LocaleExtension::class)]
#[CoversClass(SimpleLocaleProvider::class)]
#[AllowMockObjectsWithoutExpectations] // https://github.com/symfony/symfony/issues/62669
final class KnpTranslationsTypeTest extends TypeTestCase
{
Expand Down
6 changes: 6 additions & 0 deletions tests/Form/Type/TranslationsFormsTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace A2lix\TranslationFormBundle\Tests\Form\Type;

use A2lix\TranslationFormBundle\Form\Extension\LocaleExtension;
use A2lix\TranslationFormBundle\Form\Type\TranslationsFormsType;
use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use A2lix\TranslationFormBundle\LocaleProvider\SimpleLocaleProvider;
use A2lix\TranslationFormBundle\Tests\Fixtures\Entity\Company;
use A2lix\TranslationFormBundle\Tests\Fixtures\Entity\CompanyMediaLocale;
use A2lix\TranslationFormBundle\Tests\Fixtures\Form\CompanyMediaType;
Expand All @@ -27,6 +30,9 @@
* @internal
*/
#[CoversClass(TranslationsFormsType::class)]
#[CoversClass(TranslationsType::class)]
#[CoversClass(LocaleExtension::class)]
#[CoversClass(SimpleLocaleProvider::class)]
#[AllowMockObjectsWithoutExpectations] // https://github.com/symfony/symfony/issues/62669
final class TranslationsFormsTypeTest extends TypeTestCase
{
Expand Down