Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translating treedropdownfield #24

Open
guyvanbael opened this issue Apr 13, 2015 · 11 comments
Open

translating treedropdownfield #24

guyvanbael opened this issue Apr 13, 2015 · 11 comments

Comments

@guyvanbael
Copy link

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?

@bummzack
Copy link
Owner

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?

@guyvanbael
Copy link
Author

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

@bummzack
Copy link
Owner

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.

@guyvanbael
Copy link
Author

Gonna try this. Thanks for looking into it!

@bummzack
Copy link
Owner

@guyvanbael Have you been able to solve this issue?

@guyvanbael
Copy link
Author

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

@sanderha
Copy link

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

@bummzack
Copy link
Owner

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

@dacar
Copy link

dacar commented Jul 18, 2016

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

@bummzack
Copy link
Owner

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

@derralf
Copy link

derralf commented Jul 25, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants