Im new at Silverstripe so please be gentle.
I would like to create a CheckboxSetField list under a tab in my Facilities page. The list would be made from the child pages available under my Services section.
I think I need to use the $many_many relationship explained in the tutorial (https://www.silverstripe.org/learn/lessons/working-with-data-relationships-many-many), but UncleCheese is generating his Categories CheckboxSetField list from a GridField.
I made a static list based off of (http://api.silverstripe.org/3.1/class-CheckboxSetField.html) that works, but the array needs to be pulled from the Service section child page list.
$fields->addFieldsToTab('Root.ServicesAvailable', array (
HeaderField::create('InfoHeader', 'Services Available'),
new CheckboxSetField(
$name = "services",
$title = "Services offered by this facility:",
$source = array(
"1" => "Technology",
"2" => "Gardening",
"3" => "Cooking",
"4" => "Sports"
),
$value = "1"
)
));
So far I have:
FacilityHolder.php
<?php
class FacilityHolder extends Page {
private static $has_many = array (
'Locations' => 'Location',
'Services' => 'ServicePage',
);
}
class FacilityHolder_Controller extends Page_Controller {
private static $allowed_actions = array (
'show'
);
public function show(SS_HTTPRequest $request) {
$location = Location::get()->byID($request->param('ID'));
if(!$location) {
return $this->httpError(404, 'Could not be found.');
}
return array (
'Location' => $location,
'Title' => $location->Title
);
}
public function GetLocation() {
return Location::get();
}
public function ServicesAvailable() {
return Location::get()
->filter(array(
'services' => true
));
}
}
ServicePage.php
<?php
class ServicePage extends Page {
private static $db = array (
'ServTeaser' => 'Text',
);
private static $has_one = array (
'Icon' => 'Image',
'FacilityHolder' => 'FacilityHolder'
);
private static $belongs_many_many = array (
'Services' => 'ServicePage',
);
private static $can_be_root = false;
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', TextareaField::create('ServTeaser', 'Short Teaser'), 'Content');
$fields->addFieldToTab('Root.Main', $icon = UploadField::create('Icon', 'Icon'), 'Content');
$icon->getValidator()->setAllowedExtensions(array('png','gif','jpg','jpeg','svg'));
$icon->setFolderName('icons');
return $fields;
}
}
class ServicePage_Controller extends Page_Controller {
}
Location.php
private static $has_one = array(
'Category' => 'LocationCategory',
'FactSheet' => 'File',
'FacilityHolder' => 'FacilityHolder'
);
private static $many_many = array (
'Services' => 'ServicePage',
);
// create and populate Services Available tab
$fields->addFieldsToTab('Root.ServicesAvailable', array (
HeaderField::create('InfoHeader', 'Services Available'),
new CheckboxSetField(
$name = "services",
$title = "Services offered by this facility:",
$source = array(
"1" => "Technology",
"2" => "Gardening",
"3" => "Cooking",
"4" => "Sports"
),
$value = "1"
)
));
Thanks for looking.