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.

Template Questions /

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

How do I get a drop-down list in the CMS editor?


Go to End


5 Posts   7692 Views

Avatar
ianpiper

Community Member, 32 Posts

25 March 2009 at 12:47am

I hope this isn't a FAQ - I have looked but failed to find it.

One of my fields in the CMS editor is presently a text field, but it contains a limited range of options - basically "left", "middle" or "right" relating to which column on the page will be used to display the information.

Is it possible to have the editor display a list of options as a pull-down menu? This will limit the risk of typos spoiling the output.

Thanks,

Ian.
--

Avatar
Double-A-Ron

Community Member, 607 Posts

25 March 2009 at 3:14pm

Sure you can. Simple example:

class RegionalPage extends Page {

	static $db = array(
		'PageRegion' => 'Int'
	);
	
	static $defaults = array(
		'PageRegion' => 0
	); 
 
	
	function getCMSFields() {
		$fields = parent::getCMSFields(); 
	
		$options = array("Rest of the World", "Asia");
		$fields->addFieldToTab("Root.Content.Main", new DropdownField("PageRegion", "Acessable to Region", $options), 'Content');  
		
		return $fields;
	} 	
}

Cheers
Aaron

Avatar
ianpiper

Community Member, 32 Posts

27 March 2009 at 1:10am

Fantastic. Thanks Aaron.

Avatar
ianpiper

Community Member, 32 Posts

27 March 2009 at 4:48am

Hmm. Having taken a look at this I am confused. It looks as though the example code is for an integer property. I want to pass back a string - basically one of several position choices of string: "left',"middle" or "right". I will then be picking this up in the template to identify a style that needs to be applied for the relevant choice.

I took a look at the docs for DropdownField, and found some sample code that uses an Enum to hold an array of string values
(here: http://doc.silverstripe.org/doku.php?id=recipes:extending_the_event_calendar&s=dropdownfield%20cms%20editor). But this doesn't seem to work either - throws this error when I try to build the database:

[User Error] Enum::__construct() The default value does not match any item in the enumeration

So I'm completely confused now. Can you throw some light on how to manage a drop-down list of string values?

Thanks,

Ian.
--

Avatar
ianpiper

Community Member, 32 Posts

27 March 2009 at 5:01am

<blush>Erm, user error I'm afraid...</blush>

I had quotes around the individual items in the Enum array instead of the collection of items. So SS was seeing the second array item as the default.

In case anyone is interested, the fixed version of my code is here:

class ContentBlockPage extends Page {
static $db = array(
'ColumnRef' => "Enum('columnLeft, columnMiddle, columnRight')",
'RowNumber' => 'Int',
'BlockColour' => "Enum('blue, bluegrey, brickred, mustardyellow, mossgreen')",
'TeaserText' => 'HTMLText'
);
static $has_one = array(
'TeaserImage' => 'Image'
);

static $defaults = array(
'ColumnRef' => 'columnLeft',
'RowNumber' => 1,
'BlockColour' => 'blue'
);

static $icon = "themes/tellura/images/treeicons/blockpage";

function getCMSFields() {
$fields = parent::getCMSFields();
$rowOptions = array(1, 2, 3, 4, 5, 6, 7, 8);
$fields->addFieldToTab("Root.Content.Main", new DropdownField('ColumnRef', 'Which column?', singleton('ContentBlockPage')->dbObject('ColumnRef')->enumValues()), 'Content');
$fields->addFieldToTab("Root.Content.Main", new DropdownField('RowNumber', 'Row number', $rowOptions), 'Content');
$fields->addFieldToTab("Root.Content.Main", new DropdownField('BlockColour', 'Header bar colour?', singleton('ContentBlockPage')->dbObject('BlockColour')->enumValues()), 'Content');
$fields->addFieldToTab("Root.Content.Main", new HTMLEditorField('TeaserText'), 'Content');
$fields->addFieldToTab("Root.Content.Images", new ImageField('TeaserImage'));
return $fields;
}

}