You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

A2LiX Translation切换标签时在表单构建器中获取当前语言

How to Fetch Active Language on A2LiX Translation Tab Switch for Symfony Form Query Builder

Absolutely, you can grab the current language when switching A2LiX Translation tabs and use it in your form's query builder—here's how to implement this smoothly with your existing MixCampaignTranslationType:

1. How A2LiX Passes the Active Locale

First, know that A2LiX TranslationFormBundle automatically injects the matching locale into each translation form instance when you switch tabs. Looking at your configureOptions method, you already have a default locale set to fr, and this value will update dynamically to match the active tab (e.g., nl when you click the NL tab).

2. Integrate the Language into Your Query Builder

You can use this locale directly in your surveys field's query builder. First, format it to your required case (like Fr for FR tab, Nl for NL tab) with ucfirst(), then reference it in the query logic.

Here's your updated buildForm method with the query builder added:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    if (null !== $options['campaignId']) {
        $this->campaignId = $options['campaignId'];
    }

    // Convert the locale to your desired case (e.g., 'fr' → 'Fr', 'nl' → 'Nl')
    $currentLang = ucfirst($options['locale']);

    $builder
        ->add('surveys', EntityType::class, [
            'class' => Survey::class,
            'required' => false,
            'multiple' => true,
            'mapped' => true,
            'attr' => [
                'class' => 'select2',
            ],
            'constraints' => new MaxMixCampaign(),
            // Add your query builder here using the active language
            'query_builder' => function (EntityRepository $er) use ($currentLang) {
                return $er->createQueryBuilder('s')
                    // Example: Filter surveys by the current language
                    ->where('s.language = :language')
                    ->setParameter('language', $currentLang);
                // Adjust the WHERE clause to match your Survey entity's field name
            },
        ])
        // Rest of your fields stay the same
        ->add('startDate', DateTimeType::class, [
            'widget' => 'single_text',
            'format' => 'yyyy-MM-dd',
            'constraints' => new NotBlank(),
            'attr' => [
                'class' => 'datepicker',
            ],
        ])
        ->add('endDate', DateTimeType::class, [
            'widget' => 'single_text',
            'format' => 'yyyy-MM-dd',
            'constraints' => new NotBlank(),
            'attr' => [
                'class' => 'datepicker',
            ],
        ])
        ->add('slug', TextType::class, [
            'constraints' => [new NotBlank(), new Type('string'), new CampaignSlugDuplicate($this->campaignId, $options['locale'], 'mix')],
        ])
        ->add('isClosed');
}

3. Double-Check Your A2LiX Setup

Make sure your main form (where you include the TranslationsType) isn't overriding the locale option incorrectly. The locale should be auto-passed by A2LiX to each translation form instance. Your main form code should look something like this:

// In your MixCampaignType (the parent form)
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        // ... other fields
        ->add('translations', TranslationsType::class, [
            'entry_type' => MixCampaignTranslationType::class,
            'entry_options' => [
                'campaignId' => $options['campaignId'],
                // Don't hardcode locale here—A2LiX handles it per tab
            ],
        ]);
}

4. Adjust for Case Sensitivity

If your Survey entity stores languages in lowercase (like fr instead of Fr), just remove the ucfirst() call and use $options['locale'] directly in the query builder.

Quick Recap:

  • Every time you switch A2LiX tabs, the form re-renders with the correct locale in $options['locale'].
  • The query builder will dynamically filter surveys based on the active tab's language, so your select list updates automatically when switching between FR and NL.

内容的提问来源于stack exchange,提问作者Shefali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:44:16