Jump to:

23486 Posts in 18995 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » many_many relations when translating a page

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 658 Views
  • Felicitas
    Avatar
    Community Member
    16 Posts

    many_many relations when translating a page Link to this post

    When I translate a page the many-many relations from the original page are not copied to the translated page. I have to select the related items again on the translated page.

    Is there a way to automate this?

    I was looking at the code on this website (concerning relationships when duplicating a page), but I don't know if the same is possible when translating a page?

    Could someone start me off in the right direction (I use Silverstripe 2.4.7)?

  • Felicitas
    Avatar
    Community Member
    16 Posts

    Re: many_many relations when translating a page Link to this post

    Same question here and here ...
    But no solution?

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: many_many relations when translating a page Link to this post

    The second link from max5k seems to give a solution...

  • Felicitas
    Avatar
    Community Member
    16 Posts

    Re: many_many relations when translating a page Link to this post

    Yes, but the solution of max5k can be used when duplicating pages. But I don't know how and where to implement his code when translating pages? In Translatable.php?

  • Felicitas
    Avatar
    Community Member
    16 Posts

    Re: many_many relations when translating a page Link to this post

    It works.

    Added the following code at the end of the function createTranslation($locale) in Translatable.php

    foreach( $originalPage->many_many() as $key => $className ){
    $newTranslation->{$key}()->addMany($originalPage->{$key}()->getIdList());
    }

    The many-many relations from the original page are now copied to the translated page.

    In combination with the TranslatableDataObject from Uncle Cheese this works great for me!

  • yorkie
    Avatar
    Community Member
    2 Posts

    Re: many_many relations when translating a page Link to this post

    I had a similar problem and I solved it by adding an onAfterWrite method to my object and using Felicitas little code snippet. This way I don't have to worry about touching other code. I am not happy with the way I get the original page as it is a hack to have to use the language locale in the code.

    WARNING: I am still testing the code, seems to work well though

    public function onAfterWrite()
    {
       parent::onAfterWrite();

    //Todo, there must be a better way to get the original page
       $originalPage = $this->getTranslation('de_DE');

       if($this->ID == $originalPage->ID) return;

       foreach ($originalPage->many_many() as $key => $className)
       {
          $this->{$key}()->addMany($originalPage->{$key}()->getIdList());
       }
    }

  • yorkie
    Avatar
    Community Member
    2 Posts

    Re: many_many relations when translating a page Link to this post

    A quick correction, the code should be:

    public function onAfterWrite()
    {
       parent::onAfterWrite();

    //Todo, there must be a better way to get the original page
       $originalPage = $this->getTranslation('de_DE');

       if($!$originalPage) return;

       foreach ($originalPage->many_many() as $key => $className)
       {
          $this->{$key}()->addMany($originalPage->{$key}()->getIdList());
       }
    }

    Sorry

    658 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.