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

adding CheckboxSetField on new Page to CMS - tab does not appear


Go to End


3 Posts   1903 Views

Avatar
acktivate

Community Member, 11 Posts

18 December 2013 at 12:57pm

I am running 3.1.2 and trying to add a group of checkboxes to the CMS. I've added the form to manage the checkboxes to the CMS and that seems to work but I am unable t

I found an example on the internet from 2011 based on the 2.x framework and can't seem to get it working for 3.x. Feature.php and FeatureAdmin.php seem to work but I can't get the list of checkbox values to appear on a tab in the CMS labeled FeaturePage.php - the tab won't appear.

suggestions?

My code is below from mysite/code

FeaturePage.php
<?php
class FeaturePage extends Page {

// pages can have many features
static $many_many = array(
'Features' => 'Feature'
);

function getCMSFields() {
$fields = parent::getCMSFields();
$features=Feature::get();
$map = $features->toDropdownMap('ID', 'Name', '(Select one)', true);
$fields->addFieldToTab('Root.Features', new CheckboxSetField($name = 'Features',$title = 'Select Features',$source = $map));
}
}

class FeaturePage_Controller extends Page_Controller {
}

FeatureAdmin.php
<?php
class FeatureAdmin extends ModelAdmin {

private static $managed_models = array(
'Feature'
);

private static $url_segment = 'features';
private static $menu_title = 'Features';
}

Feature.php
<?php
class Feature extends DataObject {

private static $db = array(
'Name' => 'Varchar(255)',
'Description' => 'HTMLText'
);

public function getCMSFields() {

$fields = new FieldList();

$fields->push(new TextField('Name', 'Name of the feature'));
$fields->push(new TextareaField('Description', 'Short description'));

return $fields;
}
}

Avatar
martimiz

Forum Moderator, 1391 Posts

6 January 2014 at 11:04pm

Hi,
Sorry for the late reply, I hope you did already find an answer to your question, but just in case: two things actuallywrong with your code:

- Your pages getCMSFields didn't return any fields
- toDropdownMap() doesn't exist in v3.1, use map().

FeaturePage:

<?php
class FeaturePage extends Page {

	// pages can have many features
	private static $many_many = array(
		'Features' => 'Feature'
	);

	public function getCMSFields() {
		$fields = parent::getCMSFields();
	
		$features=Feature::get();
		$map = $features->map('ID', 'Name', '(Select one)', true);
		
		$fields->addFieldToTab(
		       'Root.Features', 
		       new CheckboxSetField($name = 'Features',$title = 'Select Features',$source = $map));
	
		return $fields;
	}
}

class FeaturePage_Controller extends Page_Controller {
}

Also in Feature I would add the reverse relation, to make it possible to get the pages belonging to a certain feature:

	private static $belongs_many_many = array(
	    'FeaturePages' => 'FeaturePage'
	);

Martine

Avatar
acktivate

Community Member, 11 Posts

10 January 2014 at 1:09pm

Edited: 12/01/2014 7:01am

Hi Martine -

No worries. Your info helped solved the problem but it is not functioning as intended. I fixed the intended functionality by using a Data Extension. It is now working correctly. Thank you.