HI, I have a site where each top level page has data associated which controls pages under it (styles etc). I'm looking for a way to specify a field in Page.php but only have it show up in the CMS if it is a top level page.....any ideas out here?
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
Well you could have a condition in the getCMSFields - if($this->ParentID == 0) since if its a top level page, it has no parent :).
Great thanks Will.....
the other issue Im having with this is retreiving an Image from the top level. ie I have the following function in Page_Controller:
function getSubNavBg() {
return $this->Level(1)->SectionNavImg;
}
where SectionNavImg is an Image
and in my template I'm trying to set a background image on a sub nav for a section using eg:
<style>
ul#SubNav {background:url($getSubNavBg.Filename) }
</style>
but it will not bring the filename through....am I missing something blindingly obvious? :)
Try
ul#SubNav {background:url({$SubNavBg.Filename}) }
If that doesn't work make sure $SubNavBg exists. Try a <% debug SubNavBg %> and see what it says.
Hi, doesn't seem to be any JOy here...I'll list all the relevant code in case we've missed something:
Page.php
class Page extends SiteTree {
public static $db = array(
'SectionColour' => 'Text',
'SectionHighlightColour' => 'Text'
);
public static $has_one = array(
'SectionNavImg' => 'Image'
);
function getCMSFields() {
$fields = parent::getCMSFields();
if($this->ParentID == 0){
$fields->addFieldToTab('Root.Content.Theme', new ColorField('SectionColour', 'Section Colour'));
$fields->addFieldToTab('Root.Content.Theme', new ColorField('SectionHighlightColour', 'Section Highlight Colour'));
$fields->addFieldToTab('Root.Content.Theme', new ImageField('SectionNavImg'));
}
return $fields;
}
}
class Page_Controller extends ContentController {
public function init() {
parent::init();
}
function SectionColour() {
return $this->Level(1)->SectionColour;
}
function HiglightColour() {
return $this->Level(1)->SectionHighlightColour;
}
function SubNavBg() {
return $this->Level(1)->SectionNavImg;
}
}
In my template $SectionColour returns the correct value from the top level page, but the Image ( using $SubNavBg.Filename ) will just not come through.......
$this->Level(1)->SectionNavImg
Probably because the image is a relationship you need to return the object so try either
return $this->Level(1)->SectionNavImg();
or
return $this->Level(1)->dbObject('SectionNavImg');
The first one should work though.
Great thanks Will!
return $this->Level(1)->SectionNavImg(); works
knew it would be something simple :)