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.

图片转代码服务由CSDN问答提供 功能建议

在SilverStripe 3.1中我试图获取我的Hello Bar选择器的值,以便网站范围内的页面可以访问。

我创建了下拉字段以选择 HomePage.php 上的内容,因此我在引用主页上的字段值时没有任何问题。 下拉列表的值将通知if块运行以及填充hello栏的内容。

Page.php

  ..// 
 
公共函数HelloBarSelector(){
 $ Selector = HomePage :: get() - &gt; HelloBarSelect; 
返回$ Selector; 
} 
 
公共函数ShowHelloBar  ($ itemID = 1){
 $ HelloBars = HelloBar :: get() - &gt; byID($ itemID); 
 $ HelloBars = $ HelloBars-&gt; HelloBarText; 
返回$ HelloBars; 
} 
  
 ..// 
   
 
 

<强>含/ HelloBar.ss

 <代码>&LT  ;%if if $ HelloBarSelector%&gt; 
&lt; section class =“hello”&gt; 
&lt; p class =“hello__text”&gt; $ ShowHelloBar($ HelloBarSelector)&lt; / p&gt; 
&lt; / section&gt; 
&lt;  ;%end_if%&gt; 
   
 
 

HomePage.php

  ../  / 
 
公共函数getCMSFields(){
 $ fields = parent :: getCMSFields(); 
 $ fields-&gt; addFieldToTab('Root.HelloBar',GridField :: create(
'HelloBars',
  'Hello Bar Text',
 $ this-&gt; H.  elloBars(),
 GridFieldConfig_RecordEditor :: create()
)); 
 $ fields-&gt; addFieldToTab('Root.HelloBar',DropdownField :: create(
'HelloBarSelect',
'选择你的Hello Bar  (这将是整个网站)',
 HelloBar :: get() - &gt; map('ID','HelloBarText')
)
  - &gt; setEmptyString('(none)')); 
返回 $ fields; 
} 
 
 ..// 
   
 
 

我在使用 $ HelloBarSelect 访问该值时没有任何问题 主页和所有工作按预期。 似乎问题是使用我的函数访问 $ HelloBarSelect

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题