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

Get value of DropdownField not the ID


Go to End


3 Posts   2229 Views

Avatar
oldsql

Community Member, 6 Posts

23 March 2014 at 1:21pm

Hey,

in the page.php I have this code:

	private static $db = array(
		....
                'Glasart' => 'Text'
	);

and this:

public function getCMSFields() {
        $fields = parent::getCMSFields();
         ....
        $options = array("Kaffeeglas", "Weinglas", "Cocktailglas", "Bierglas", "Sektglas", "Wasserglas", "Sonstige"); 
        $fields->addFieldToTab("Root.Main", new DropdownField("Glasart", "Glasart", $options), 'Content');
         ....
        return $fields;

and in the Page.ss:

       <h1>$Title - $Glasart</h1>

But in the frontend I only see the ID of the selected value, but I want the value, for example "Kaffeglas".
Any ideas ? :-)

Avatar
Willr

Forum Moderator, 5523 Posts

23 March 2014 at 5:54pm

Your array should look something like this then
array(
"Kaffeeglas" => "Kaffeeglas",
"Weinglas" => "Weinglas"
..
);

If you don't want to write the same thing twice, you could use array_combine to generate that for you ($a = array_combine($a, $a))

http://nz1.php.net/array-combine

Avatar
oldsql

Community Member, 6 Posts

25 March 2014 at 12:06am

Thanks! That worked just fine :-)