Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

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.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Show CMS field only on top level pages


Go to End


7 Posts   2593 Views

Avatar
NickJacobs

Community Member, 148 Posts

21 January 2010 at 8:57am

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?

Avatar
Willr

Forum Moderator, 5523 Posts

21 January 2010 at 11:39am

Well you could have a condition in the getCMSFields - if($this->ParentID == 0) since if its a top level page, it has no parent :).

Avatar
NickJacobs

Community Member, 148 Posts

21 January 2010 at 11:53am

Edited: 21/01/2010 11:53am

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? :)

Avatar
Willr

Forum Moderator, 5523 Posts

21 January 2010 at 12:27pm

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.

Avatar
NickJacobs

Community Member, 148 Posts

21 January 2010 at 1:57pm

Edited: 21/01/2010 1:59pm

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.......

Avatar
Willr

Forum Moderator, 5523 Posts

21 January 2010 at 2:28pm

$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.

Avatar
NickJacobs

Community Member, 148 Posts

21 January 2010 at 2:37pm

Great thanks Will!
return $this->Level(1)->SectionNavImg(); works

knew it would be something simple :)