Reputation: 17
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.
Upvotes: 1
Views: 506
Reputation: 576
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/
Upvotes: 0
Reputation: 118
What does the ShowHelloBar()
function does?
Try just using $HelloBarSelector
instead of $ShowHelloBar($HelloBarSelector)
EDIT:
I see. Template functions doesn't take arguments, so in this case, $itemID will always be null.
Where are you getting $itemID
? If it's somewhere in the request/query then you have to get it from that.
e.g.
public function ShowHelloBar() {
//If you use the standard $Action/$ID/$OtherID handler
$itemID = $this->getRequest()->param('ID');
//If it's somewhere in $_GET like ?ID=3
$itemID = $this->getRequest()->getVar('ID');
$HelloBars = HelloBar::get()->byID($itemID);
$HelloBars = $HelloBars->HelloBarText;
return $HelloBars;
}
Upvotes: 0