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 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决