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

getCMSFields conditionally showing field in modeladmin


Go to End


1693 Views

Avatar
Graphicator

Community Member, 62 Posts

19 August 2012 at 11:30am

I was looking a while for the answer to this but couldn't find it.

I have a DataObject "Testimonial" that is a part of a Page class "BookPage", and I've also added testimonial as a ModelAdmin to list all the testimonials and the books they are under. I used the ModelAdmin to switch Books that the testimonial belongs in, with a "BookPage" dropdown....

	public function getCMSFields(){
		$fields = new FieldList();
		...
		$fields->push( new DropdownField(
		    'BookPageID',
		    'Please choose a Book',
		    $this->getBookSelection() ) );

...
	function getBookSelection()
	{
	    if($Pages = DataObject::get('BookPage'))
	    {
	    	return $Pages->map('ID', 'Title', 'Please Select');
	    }
	    else
	    {
	    	return array('No Objects found');
	    }
	} 

I'd like to be able to show the dropdown menu only when I'm adding or editing a testimonial from the modelAdmin (/admin/testimonial/Testimonial/EditForm/field/Testimonial/item/2/edit) ,
but not when I'm accessing the testimonial from editing the pages (/admin/pages/edit/EditForm/field/Testimonials/item/new).