Using SilverStripe 2.4.7.
I’ve done some searching but I can’t seem to be able to find an answer to this. I want to include a checkbox on a popup window in dataobjectmanager but not for every user.
I have two separate pages, one for one user and another for the other, and I only want the checkbox on one. I thought an if statement would suffice, quick and simple right?
public function getCMSFields()
{
$categories = array("Morning","Afternoon", "Evening", "Night");
return new FieldSet(
new TextField('Title'),
new DatePickerField('Date', 'Date'),
new ImageField('Photo', 'Photo'),
new MoneyField('AdultPrice', 'Adult Price'),
new MoneyField('ChildPrice', 'Child Price'),
new DropdownField('Category', 'Choose a Category', $categories)
);
This is my attempt at the if statement approach
if($this->ClassName == 'Movie'){
$films= DataObject::get('Films');
if (!empty($films)) {
// create an array('ID'=>'Name')
$map = $films->toDropdownMap('ID', 'Name');
$fieldset->push(new CheckboxSetField(
$name = "Films",
$title = "Select Films",
$source = $map
));
}
}
Basically this works if I use it within getCMSFields_forPopup, but not in just getCMSFields, but changes my checkboxsetfield to a dropdown.
Edit
I have found that my approach would not work due to the fact that the DOM Popup cannot have the classname of the page which contains the DOM (DataObjectManager). This is a simple inheritance issue and I can’t believe I didn’t see it before. See the answer below for details of how I solved my original query.
In the end the answer was quite simple. I did the following and I hope someone finds this useful.
Create the checkboxset on the page as normal
The if statement with Permission::check(“ADMIN”) checks if the admin is logged in and only shows the checkboxset if they are.
You will also need to include the $fieldset->push within this method to add it to the cms. The ADMIN can be changed to any of your user groups which you created in the security panel so this is an adaptable approach.
I found this the best way for me but if someone can improve on it/offer a better solution I’d love to hear about it.