문제

I have created a Link DataObject to automatically let users create a reference to a different page in the Frontend. I use two languages in the frontend, German and English. In the popup I create a dropdown to select the pages

public function getCMSFields_forPopup()
{
    return new FieldSet(
        new TextField('Titel'),
        new TextField('URL', 'Externer Link'),
        new SimpleTreeDropdownField('PageLinkID', 'Interner Link', 'SiteTree')
    );
}

But I only get the German pages in the dropdown. Tried to change the admin language to English but no change. The database seems to only return the German pages...

Any clue?

도움이 되었습니까?

해결책

Edit: I did some more digging and found out how to do this. You need to call "disable_locale_filter" before you get your SiteTree objects:

Translatable::disable_locale_filter();

Then call "enable_locale_filter" once you've retrieved them:

Translatable::enable_locale_filter();

These are other approaches which I'll leave here as I think they are still useful...

I believe you may have to do this using Translatable::get_by_locale() - I assume you only want people to be able to select a page to link to within their language??

Perhaps something like this?

public function getCMSFields_forPopup()
{
    $member = Member::currentUser();
    if($member && $member->Locale) {

        $pagesByLocale = Translatable::get_by_locale('SiteTree', $member->Locale);
        $pagesByLocale = $pagesByLocale->map('ID', 'Title', '(Select one)', true);

        return new FieldSet(
            new TextField('Title'),
            new TextField('URL', 'Externer Link'),
            new DropdownField('PageLinkID', 'Interner Link', $pagesByLocale);
        );

    } else {

        // Handle non-member

    }

}

Edit: see comments below but another option is to use the Translatable::get_current_locale() function to find all pages in the Site Tree for that locale... if the user is viewing an english page then the locale should be set to english etc...

public function getCMSFields_forPopup()
{
    $pagesByLocale = Translatable::get_by_locale('SiteTree', Translatable::get_current_locale());
    $pagesByLocale = $pagesByLocale->map('ID', 'Title', '(Select one)', true);

    return new FieldSet(
        new TextField('Title'),
        new TextField('URL', 'Externer Link'),
        new DropdownField('PageLinkID', 'Interner Link', $pagesByLocale);
    );

}

You can also get the locale from the current page e.g.

$this->Locale; // From within the model
$this->dataRecord->Locale; // from within the controller
Director::get_current_page()->Locale; // If you're outside the context of the page altogether i.e. code inside your DataObject.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top