2

我需要更改 A2LiX 翻译选项卡的语言。如果我单击“FR”,则需要获取 Fr,单击“NL”需要表单生成器中的 Nl 语言(“此处需要语言”)。

对于一个字段中的查询构建器。选项卡更改时是否可以获取表单类型的语言?

在此处输入图像描述

我的表单类 MixCampaignTranslationType :

<?php

namespace BackEndBundle\Form;

use BackEndBundle\Validator\Constraints\CampaignSlugDuplicate;
use BackEndBundle\Validator\Constraints\MaxMixCampaign;
use Doctrine\ORM\EntityRepository;
use SurveyBundle\Entity\Survey;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Type;

class MixCampaignTranslationType extends AbstractType
{
    public $campaignId = null;

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

        $builder
            ->add('surveys', EntityType::class, [
                'class'    => Survey::class,
                'required' => false,
                'multiple' => true,
                'mapped'   => true,
                'attr'     => [
                    'class' => 'select2',
                ],
                'constraints' => new MaxMixCampaign(),
            ])
            ->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');
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => 'SurveyBundle\Entity\MixCampaignTranslation',
            'locale'     => 'fr',
            'campaignId' => null
        ]);
    }

    public function getName()
    {
        return 'back_end_bundle_mix_campaign_translation_type';
    }
}
4

1 回答 1

2

您可以创建自定义 TranslationFormType,然后将循环语言作为当前语言传递,请检查如下。

/**
 * @param \Symfony\Component\Form\FormBuilderInterface $builder
 * @param array                                        $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->addEventSubscriber($this->translationsListener);
    $formsOptions = $this->translationForm->getFormsOptions($options);
    foreach ($options['locales'] as $locale) {
        if (isset($formsOptions[$locale])) {
            $builder->add($locale, $options['form_type'], [
                    'currentLanguage' => $locale,
                    'campaignId' => $formsOptions[$locale]['campaignId']
                ]
            );
        }
    }
}

您可以使用 currentLanguage 过滤 .query_builder 中的数据form_type

希望它会帮助你。

于 2018-03-08T07:32:05.233 回答