21310 Posts in 5739 Topics by 2604 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 352 Views |
-
many_many relations when translating a page

1 June 2012 at 11:46pm Last edited: 1 June 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 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)?
-
Re: many_many relations when translating a page

2 June 2012 at 2:08am Last edited: 2 June 2012 2:09am
-
Re: many_many relations when translating a page

2 June 2012 at 4:34am
The second link from max5k seems to give a solution...
-
Re: many_many relations when translating a page

2 June 2012 at 5:34pm Last edited: 2 June 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?
-
Re: many_many relations when translating a page

2 June 2012 at 11:55pm Last edited: 10 June 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 TranslatableDataObject from Uncle Cheese this works great for me!
-
Re: many_many relations when translating a page

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());
}
} -
Re: many_many relations when translating a page

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
| 352 Views | ||
|
Page:
1
|
Go to Top |


