Search code examples
phpsymfonybundlesymfony-3.3a2lix-translation

A2LiX Translation on change language tab get language in form


I need language on change of A2LiX Translation tabs. If I click on "FR" then need to get Fr and on click of "NL" need Nl language in form builder ('Need Language Here').

For querybuilder in one field. Is it possible to get the language in form type when tab change ?

enter image description here

My Form class 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';
    }
}

Solution

  • You can create your custom TranslationFormType and then pass the language of loop as current language, Please check as below.

    /**
     * @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']
                    ]
                );
            }
        }
    }
    

    and you can use currentLanguage to filter data in query_builder in your form_type.

    Hope it will help you.