Silverstripe不同语言的页面关系

发布于 2024-10-15 16:42:57 字数 420 浏览 8 评论 0原文

我创建了一个链接数据对象来自动让用户创建对前端中不同页面的引用。我在前端使用两种语言:德语和英语。在弹出窗口中,我创建了一个下拉列表来选择页面

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

,但我只在下拉列表中获取德语页面。尝试将管理语言更改为英语,但没有任何变化。该数据库似乎只返回德语页面......

有任何线索吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫之都 2024-10-22 16:42:57

编辑:我做了一些更多的挖掘并找到了如何做到这一点。您需要在获取 SiteTree 对象之前调用“disable_locale_filter”:

Translatable::disable_locale_filter();

然后在检索它们后调用“enable_locale_filter”:

Translatable::enable_locale_filter();

这些是我将保留在此处的其他方法,因为我认为它们仍然有用...

我相信您可能必须使用 Translatable::get_by_locale() 来执行此操作 - 我假设您只希望人们能够选择要链接到其语言的页面?

也许是这样的?

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

    }

}

编辑:请参阅下面的注释,但另一种选择是使用 Translatable::get_current_locale() 函数在站点树中查找该语言环境的所有页面...如果用户正在查看英文页面,则应将语言环境设置为英语等...

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

}

您还可以从当前页面获取区域设置,例如

$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.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文