dtn51137 2015-04-10 03:05
浏览 38
已采纳

SilverStripe - 使用函数获取页面的表单值

In SilverStripe 3.1 I'm trying to get the value of my Hello Bar selector to be accessible by pages site wide.

I've created the dropdown field to select the contents on the HomePage.php so I'm having no problem referencing the fields value on the home page. The value of the dropdown will inform the if block to run and what to populate the hello bar with.

Page.php

..//

public function HelloBarSelector() {
    $Selector = HomePage::get()->HelloBarSelect;
    return $Selector;
}

public function ShowHelloBar($itemID = 1) {
    $HelloBars = HelloBar::get()->byID($itemID);
    $HelloBars = $HelloBars->HelloBarText;
    return $HelloBars;
}

..//

Includes/HelloBar.ss

<% if $HelloBarSelector %>
<section class="hello">
    <p class="hello__text">$ShowHelloBar($HelloBarSelector)</p>
</section>
<% end_if %>

HomePage.php

..//

public function getCMSFields(){
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.HelloBar', GridField::create(
        'HelloBars',
        'Hello Bar Text', 
        $this->HelloBars(),
        GridFieldConfig_RecordEditor::create()
    ));
    $fields->addFieldToTab('Root.HelloBar', DropdownField::create(
            'HelloBarSelect',
            'Choose Your Hello Bar (this will be sitewide)',
            HelloBar::get()->map('ID', 'HelloBarText')
        )
        ->setEmptyString('(none)'));
    return $fields;
}

..//

I have no issues accessing the value with $HelloBarSelect on the home page and all works as expected. It seems the problem is accessing the $HelloBarSelect with my function.

  • 写回答

2条回答 默认 最新

  • douwan2664 2015-04-10 09:51
    关注

    HomePage::get() will return a DataList of HomePage DataObjects so you cant access the HelloBarSelect.

    HomePage::get()->First() and HomePage::get_one() (assuming that you have only one homepage there) would return one DataOject. So if the field is right you can use HomePage::get()->First()->HelloBarText

    Tip:

    Use Debug::dump(HomePage::get()) to see what you are manipulating. Its always good do dump out data to see what you are working on.

    Also read: http://doc.silverstripe.org/en/developer_guides/model/ or watch these videos http://www.silverstripe.org/learn/lessons/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果