translatable-dataobject
translatable-dataobject copied to clipboard
translating treedropdownfield
Used this module on a dataobject that has a linktopage (treedropdownfield with sitetree). When page is translated and link is adjusted to another page. The link in de default language is also pointing to the new link (which is a translated page).
Is there a fix or workaround for this.
I presume that only extra columns are created for textfields?
You have a has_one
to a Page from your DataObject, right? Do you need different Pages depending on the locale? Or do you just want to get the correct translation of the attached page?
Example, assuming Languages EN and FR. There's a Page called MyPageEN
which has a translation MyPageFR
.
MyDataObject
has_one: MyPageEN
So if you're in EN, you want to get MyPageEN
and if you're in FR, you want to get MyPageFR
. Is that correct?
It's a sitetree link (linktopageID in the database) to a form in this case. I guess it should be treated like a normal database-field translation (fieldname_locale) in the database. This way, i can have a different link for every translated version of the page. At this moment, no extra database columns are created for the has_one. So changing the sitetree link in a translated version of the page (which is necessary because the form page is different too because of the locale) results in a changed link for the page in the default language too.
But in the same project i also use a sitetreelink in a dataobject calltoactionbutton. So clicking that button redirects the user to a specific page.
Hope i explained it a bit clearly :)
Yeah, relations aren't supported by the module at the moment. Whenever I had a scenario like yours, I solved it like this:
A) Only allow editing the relation in the master language. Example:
// only add the dropdown-field when in the default-locale
if(Translatable::default_locale() == Translatable::get_current_locale()) {
$fields->addFieldToTab('Root.Main', TreeDropdownField::create('linktopage'));
}
B) Write a special getter that returns the page in the current locale (if translated).
public function LinkedPage(){
if($this->linktopageID){
$master = $this->linktopage()->Master();
$currLocale = Translatable::get_current_locale();
if($master->hasTranslation($currLocale)){
return $master->getTranslation($currLocale);
}
return $master;
}
return null;
}
Then in your code and templates use LinkedPage()
(or $LinkedPage
in template) instead of linktopage
.
I hope this is an acceptable workaround. I'll implement proper has_one
relations for the module when I find some time for it.
Gonna try this. Thanks for looking into it!
@guyvanbael Have you been able to solve this issue?
No, i ended up changing the field to a norma textfield, so the link can be entered there. I know it's not a great solution... :)
@bummzack Any plans on implementer relations in the near future? :)
@Sanderha I'm afraid not. Pull requests are welcome though!
faild for me, too. Musn't it be: return $master->getTranslation($currLocale)**->Link()**;
@dacar If you just want the link… yes. But LinkedPage
would return the Page
. So to get the link, you would do: <% $LinkedPage.Link %>
Similar situation here. Though not tested yet, I think i will use something like this (in a DataExtension for Page or SiteTree), so i won't have to write special functions for every DataObject and every page link. Maybe this helps somebody.
public function LocalizedMaster($fallbackToDefault=false) {
$master = $this->owner->Master();
$currLocale = Translatable::get_current_locale();
if($master && $master->hasTranslation($currLocale)){
return $master->getTranslation($currLocale);
}
if($fallbackToDefault) {
return $this->owner;
}
}
and in Template use
$LinkedPage.LocalizedMaster.Link
and/or
$OtherLinkedPage.LocalizedMaster(true).Link
and/or
<% loop $ManyOtherLinkedPages %>
<li><a href="$LocalizedMaster.Link">$LocalizedMaster.MenuTitle</a></li>
<% end_loop %>
or in Model:
$page = $this->LinkedPage()->LocalizedMaster();