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.

General Questions

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

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

If statement in getCMSFields


Reply

5 Posts   593 Views

Avatar
Stef87

1 September 2012 at 2:25am Community Member, 66 Posts

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

2 September 2012 at 2:03pm Forum Moderator, 5511 Posts

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

3 September 2012 at 9:44pm (Last edited: 3 September 2012 9:45pm), Community Member, 66 Posts

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

4 September 2012 at 5:54pm Forum Moderator, 5511 Posts

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

6 September 2012 at 8:13pm (Last edited: 6 September 2012 8:39pm), Community Member, 66 Posts

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?