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   696 Views

Avatar
Felicitas

Community Member, 16 Posts

1 June 2012 at 11:46pm

Edited: 01/06/2012 11:48pm

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

Community Member, 16 Posts

2 June 2012 at 2:08am

Edited: 02/06/2012 2:09am

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

Forum Moderator, 1799 Posts

2 June 2012 at 4:34am

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

Avatar
Felicitas

Community Member, 16 Posts

2 June 2012 at 5:34pm

Edited: 02/06/2012 5:37pm

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

Community Member, 16 Posts

2 June 2012 at 11:55pm

Edited: 10/06/2012 3:06am

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

Community Member, 2 Posts

13 June 2012 at 7:56pm

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

Community Member, 2 Posts

13 June 2012 at 8:09pm

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