translatable-dataobject icon indicating copy to clipboard operation
translatable-dataobject copied to clipboard

translating treedropdownfield

Open guyvanbael opened this issue 9 years ago • 11 comments

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?

guyvanbael avatar Apr 13 '15 13:04 guyvanbael

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?

bummzack avatar Apr 14 '15 09:04 bummzack

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 :)

guyvanbael avatar Apr 14 '15 09:04 guyvanbael

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.

bummzack avatar Apr 14 '15 11:04 bummzack

Gonna try this. Thanks for looking into it!

guyvanbael avatar Apr 15 '15 08:04 guyvanbael

@guyvanbael Have you been able to solve this issue?

bummzack avatar Oct 21 '15 09:10 bummzack

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... :)

guyvanbael avatar Oct 21 '15 10:10 guyvanbael

@bummzack Any plans on implementer relations in the near future? :)

sanderha avatar Feb 16 '16 13:02 sanderha

@Sanderha I'm afraid not. Pull requests are welcome though!

bummzack avatar Feb 16 '16 13:02 bummzack

faild for me, too. Musn't it be: return $master->getTranslation($currLocale)**->Link()**;

dacar avatar Jul 18 '16 09:07 dacar

@dacar If you just want the link… yes. But LinkedPage would return the Page. So to get the link, you would do: <% $LinkedPage.Link %>

bummzack avatar Jul 18 '16 09:07 bummzack

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

derralf avatar Jul 25 '16 16:07 derralf