Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

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

many_many relations when translating a page


Reply

7 Posts   672 Views

Avatar
Felicitas

1 June 2012 at 11:46pm (Last edited: 1 June 2012 11:48pm), Community Member, 16 Posts

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 [url=http://nerdpress.org/2012/03/07/silverstripe-has-many-and-many-many-relations-when-duplicating-pages/]this website[/url] (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)?

Avatar
Felicitas

2 June 2012 at 2:08am (Last edited: 2 June 2012 2:09am), Community Member, 16 Posts

Same question [url=http://www.silverstripe.org/customising-the-cms/show/15598]here[/url] and [url=http://www.silverstripe.org/data-model-questions/show/17342#post308134]here[/url] ...
But no solution?

Avatar
swaiba

2 June 2012 at 4:34am Forum Moderator, 1796 Posts

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

Avatar
Felicitas

2 June 2012 at 5:34pm (Last edited: 2 June 2012 5:37pm), Community Member, 16 Posts

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?

Avatar
Felicitas

2 June 2012 at 11:55pm (Last edited: 10 June 2012 3:06am), Community Member, 16 Posts

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 [URL=http://www.leftandmain.com/silverstripe-tips/2012/04/03/translatabledataobject-insanely-simple-translation/]TranslatableDataObject[/URL] from Uncle Cheese this works great for me!

Avatar
yorkie

13 June 2012 at 7:56pm Community Member, 2 Posts

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());
   }
}

Avatar
yorkie

13 June 2012 at 8:09pm Community Member, 2 Posts

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