douwen3500 2011-02-03 10:38 采纳率: 0%
浏览 41
已采纳

Silverstripe页面关系用不同的语言

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?

  • 写回答

1条回答 默认 最新

  • dsfw2154 2011-02-14 21:36
    关注

    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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据