I18n Translations of forms with the extension of the doctrine of knp

I am using knct Doctrine Translitable "to translate entities. So far it’s working fine. Now I’ve come to the point where I would like to have a general solution that works in any number of languages. So I would like to use the built-in form (Collections) that Translatables will handle for Entity. Now everything works as it should, except that translatable_id is not set to add a new translation. Has anyone tried to do this? I'm just wondering if there is an easier way to do this to avoid complications.

So far so good, here go My types to understand architecture better.

// Main type that has a linkTranslationType with the translations class linkType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name', 'text', array( 'label' => 'Name' )) ->add('translations', 'collection', array( 'type' => new linkTranslationType(), 'label' => false, 'allow_add' => true, 'allow_delete' => true )); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'MyBundle\Entity\Link' )); } } 

This is the LinkTranslationType type, which displays as a "string" for each language: en_EN Anchor http // url / en

 class linkTranslationType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('locale', 'text',array( 'label' => 'Anchor' )) ->add('linkText', 'text',array( 'label' => 'Anchor' )) ->add('linkUrl', 'text', array( 'label' => 'Url' )) ; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'MyBundle\Entity\LinkTranslation' )); } } 

So, as an example, let's try to add this new entry: en_EN Anchor http // url / en

I get:

 id translatable_id linkText linkUrl locale 7 NULL Anchor http//url/en en_EN 

I tried to figure out how translatable_id works, but still did not manage to examine the whole source. Finally, I also tried setTranslatableId, with no luck. (Update: in the comments)

So far, I could:

  • # 1 Insert a new link, but not translations (they are saved with NULL as traslatable_id)
  • # 2 Save existing translation link works fine

Some other notes to add some context:

1 I tried:

 $link = new Link(); if ($form->isValid() ) { $link->mergeNewTranslations(); // but this also does assigm the Id to the translations } 

2 To save the existing translation , I just passed the existing Link object to the form designer

3 I know that I could quote and assign Translatable elements to a parent entity

But this is a hack that I do not want to do if I have a better option:

 // persist($link); and flush() foreach ($link->getTranslations() as $linkTranslation) { $linkTranslation->setTranslatable($link); $em->persist($linkTranslation); } $em->flush(); 

So, of course, this is not the type of answer I'm looking for :)

+5
source share
1 answer

You must add the 'by_reference'=>false option to the translations field, and then add these methods to your Link object (see https://symfony.com/doc/current/form/form_collections.html )

 public function addTranslation(LinkTranslation $t) { $t->setTranslatable($this); $this->getTranslations()->add($t); } public function removeTranslation(LinkTranslation $t) { $this->getTranslations()->remove($t); } 
0
source

Source: https://habr.com/ru/post/1206182/


All Articles