public function preSetData(FormEvent $event)
    {
        try{
            $form = $event->getForm();
            $parentForm = $form->getParent();
            $parentData = $parentForm->getData();

            // First, we get the entity class
            $entityClass = get_class($parentData);

            // Then, we get the field list from the entity class metadata using the annotation reader
            $translatableFields = $this->getTranslatableFields($entityClass);

            //Case where there is data (edit);
            if ($parentData && method_exists($parentData, 'getId') && $parentData->getId()) {

                // We get the entity manager and its translations
                $em = $this->doctrineService->getManagerForClass($entityClass);
                
                if (!$em) {
                    $em = $this->doctrineService->getManager('default');
                }

                $repository = $em->getRepository($this->translation_class);
                $translations = $repository->findTranslations($parentData);

                // Iterating over the enabled locales
                foreach ($this->localeList as $locale) {
                    // Case of the default locale : do not display the form
                    if ($locale != $this->currentLocale) {

                        // We first add the fields without translation
                        $this->addTranslationlessFieldsForLocale($translatableFields, $parentForm, $form, $locale);

                        // If the locale has its translation
                        if (isset ($translations[$locale])) {
                            // then we get its data per field
                            // this way if a field has a translation it will automatically be filled
                            foreach($translations[$locale] as $field => $translation) {
                                if ($parentForm->has($field)) {
                                    // Here we have to first retrieve the field type in the parent form
                                    $fieldType = $parentForm->get($field)->getConfig()->getType()->getInnerType();
                                    // and then set the form type and the data
                                    $fieldAttr = $parentForm->get($field)->getConfig()->getOption('attr');
                                    $form->add(sprintf("%s-%s", $field, $locale), $fieldType, array('data' => $translation, 'required' => false, 'attr' => array_merge($fieldAttr, array('data-field-name' => $field, 'data-locale' => $locale))));
                                }
                            }
                        }
                    }
                }

                // end of the process
                return;
            }

            // Here case where there is an entity but empty one.
            $this->addTranslationlessFields($translatableFields, $parentForm, $form);

        } catch (\Exception $e) {
            // Case of a non entity object given to the parent form.
            // Unstranslatable case, throw exception
            $secondException = new \Exception("The object that was given to the form you wanted to translate isn't an entity one. Untranslatable in this case.", $e->getCode(), $e);
            throw $secondException;
        }
    }
public function preSetData(FormEvent $event)
    {
        try{
            $form = $event->getForm();
            $parentForm = $form->getParent();
            $parentData = $parentForm->getData();

            // First, we get the entity class
            $entityClass = get_class($parentData);

            // Then, we get the field list from the entity class metadata using the annotation reader
            $translatableFields = $this->getTranslatableFields($entityClass);

            //Case where there is data (edit);
            if ($parentData && method_exists($parentData, 'getId') && $parentData->getId()) {

                // We get the entity manager and its translations
                $em = $this->doctrineService->getManagerForClass($entityClass);
                
                if (!$em) {
                    $em = $this->doctrineService->getManager('default');
                }

                $repository = $em->getRepository($this->translation_class);
                $translations = $repository->findTranslations($parentData);

                // Iterating over the enabled locales
                foreach ($this->localeList as $locale) {
                    // Case of the default locale : do not display the form
                    if ($locale != $this->currentLocale) {

                        // We first add the fields without translation
                        $this->addTranslationlessFieldsForLocale($translatableFields, $parentForm, $form, $locale);

                        // If the locale has its translation
                        if (isset ($translations[$locale])) {
                            // then we get its data per field
                            // this way if a field has a translation it will automatically be filled
                            foreach($translations[$locale] as $field => $translation) {
                                if ($parentForm->has($field)) {
                                    // Here we have to first retrieve the field type in the parent form
                                    $fieldType = $parentForm->get($field)->getConfig()->getType()->getInnerType();
                                    // and then set the form type and the data
                                    $fieldAttr = $parentForm->get($field)->getConfig()->getOption('attr');
                                    $form->add(sprintf("%s-%s", $field, $locale), $fieldType, array('data' => $translation, 'required' => false, 'attr' => array_merge($fieldAttr, array('data-field-name' => $field, 'data-locale' => $locale))));
                                }
                            }
                        }
                    }
                }

                // end of the process
                return;
            }

            // Here case where there is an entity but empty one.
            $this->addTranslationlessFields($translatableFields, $parentForm, $form);

        } catch (\Exception $e) {
            // Case of a non entity object given to the parent form.
            // Unstranslatable case, throw exception
            $secondException = new \Exception("The object that was given to the form you wanted to translate isn't an entity one. Untranslatable in this case.", $e->getCode(), $e);
            throw $secondException;
        }
    }
public function preSetData ( FormEvent $ event ) { try { $ form = $ event -> getForm ( ) ; $ parentForm = $ form -> getParent ( ) ; $ parentData = $ parentForm -> getData ( ) ; // First, we get the entity class $ entityClass = get_class ( $ parentData ) ; // Then, we get the field list from the entity class metadata using the annotation reader $ translatableFields = $ this -> getTranslatableFields ( $ entityClass ) ; //Case where there is data (edit); if ( $ parentData && method_exists ( $ parentData , 'getId' ) && $ parentData -> getId ( ) ) { // We get the entity manager and its translations $ em = $ this -> doctrineService -> getManagerForClass ( $ entityClass ) ; if ( ! $ em ) { $ em = $ this -> doctrineService -> getManager ( 'default' ) ; } $ repository = $ em -> getRepository ( $ this -> translation_class ) ; $ translations = $ repository -> findTranslations ( $ parentData ) ; // Iterating over the enabled locales foreach ( $ this -> localeList as $ locale ) { // Case of the default locale : do not display the form if ( $ locale != $ this -> currentLocale ) { // We first add the fields without translation $ this -> addTranslationlessFieldsForLocale ( $ translatableFields , $ parentForm , $ form , $ locale ) ; // If the locale has its translation if ( isset ( $ translations [ $ locale ] ) ) { // then we get its data per field // this way if a field has a translation it will automatically be filled foreach ( $ translations [ $ locale ] as $ field => $ translation ) { if ( $ parentForm -> has ( $ field ) ) { // Here we have to first retrieve the field type in the parent form $ fieldType = $ parentForm -> get ( $ field ) -> getConfig ( ) -> getType ( ) -> getInnerType ( ) ; // and then set the form type and the data $ fieldAttr = $ parentForm -> get ( $ field ) -> getConfig ( ) -> getOption ( 'attr' ) ; $ form -> add ( sprintf ( "%s-%s" , $ field , $ locale ) , $ fieldType , array ( 'data' => $ translation , 'required' => false , 'attr' => array_merge ( $ fieldAttr , array ( 'data-field-name' => $ field , 'data-locale' => $ locale ) ) ) ) ; } } } } } // end of the process return ; } // Here case where there is an entity but empty one. $ this -> addTranslationlessFields ( $ translatableFields , $ parentForm , $ form ) ; } catch ( \ Exception $ e ) { // Case of a non entity object given to the parent form. // Unstranslatable case, throw exception $ secondException = new \ Exception ( "The object that was given to the form you wanted to translate isn't an entity one. Untranslatable in this case." , $ e -> getCode ( ) , $ e ) ; throw $ secondException ; } }
Pre-submit form handling

@param FormEvent $event
@throws \Exception in case the form the subscriber was set on isn't an entity one.
Pre - submit form handling