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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

If statement in getCMSFields


Go to End


5 Posts   1143 Views

Avatar
Stef87

Community Member, 66 Posts

1 September 2012 at 2:25am

I apologise if this is really obvious and a stupid question but I'm a little stumped. I'm trying to add a field that is only visible on one page so I am using an if statement in the getCMSFields function of my page.

My question is is this even possible? Will it be called, etc? The reason I ask is that my if statement doesn't work unless I change getCMSFields to getCMSFields_forPopup which causes a whole new set of problems.

Please help!

Thanks in advance.

P.S. I'm using SilverStripe 2.4.7.

 if ($this->ClassName == 'Movies') {
			
			$movies= DataObject::get('Movie');

	        if (! empty($stores)) {

				$map = $movies->toDropdownMap('ID', 'Name');
			
				$checkBox = new CheckboxSetField(
					$name = "Moviess",
					$title = "Select Movies",
					$source =  $map
				);

				$fieldset->push($checkBox);
			}
		} 

Avatar
Willr

Forum Moderator, 5523 Posts

2 September 2012 at 2:03pm

You should be able to use an if perfectly fine. Though it would be better design to move the functionality to your Movies.php class.

<?php

class Movies extends Page {
..

function getCMSFields() {
$fields = parent::getCMSFields();
$movies= DataObject::get('Movie');

if (! empty($movies)) {
$map = $movies->toDropdownMap('ID', 'Name');
$fieldset->push(new CheckboxSetField( "Movies", "Select Movies", $map));
}

return $fields;
}

Avatar
Stef87

Community Member, 66 Posts

3 September 2012 at 9:44pm

Edited: 03/09/2012 9:45pm

Thanks for your reply willr. I am using a Dataobject with a dataobjectmanager in movies. Can I still use this method? I'm relatively new to SilverStripe so my knowledge is limited but I can't see how it'll work that way.

I'm using 2.4.7, I just realised I never specified that earlier.

Avatar
Willr

Forum Moderator, 5523 Posts

4 September 2012 at 5:54pm

You should be able to use the same setup whether you have dataobjects or pages. Though getCMSFields may be getCMSFields_forPopup if you're using the popup to edit content.

Avatar
Stef87

Community Member, 66 Posts

6 September 2012 at 8:13pm

Edited: 06/09/2012 8:39pm

Ok that makes sense. Thank you. It's still not working exactly as I want it to but I have a better understanding of what is going on. I think my confusion lies with DataObjectManager though as it creates the popup for editing the data and is created within getCMSFields of Movies. If I understand correctly then I need to use getCMSFields_ForPopup in Movies instead?