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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Customising the CMS /

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

toDropdownMap question

Go to End

8 Posts   4300 Views


Community Member, 134 Posts

22 March 2010 at 2:27am


I have a simple dropdown with a toDropdownMap-function. It looks like this:

DataObject::get('Employee')->toDropdownMap('ID','NickName', 'Choose employee')

Now, would it be possible, to show two or more Fields in the dropdown, like this?

DataObject::get('Employee')->toDropdownMap('ID','FirstName' . 'Surname', 'Choose employee').

So, instead of showing a nickname, I want to show firstname and surname...




Forum Moderator, 5523 Posts

22 March 2010 at 11:39am

I'm pretty sure it will also check for methods called 'Nickname' on the object which can return and combination of fields.

If you had a function Nickname() { return $this->FirstName . ' '. $this->Surname; } (or if that doesn't work - getNickname()) then it should work. Though I am pretty sure the member object has a built in method like Fullname or Name which returns first name + surname so perhaps try those first before implementing your own


Community Member, 134 Posts

22 March 2010 at 11:46am

Thanks Willr

I'll give it a try.


Community Member, 4 Posts

26 November 2010 at 11:58am

joelg - did you ever solve this problem? I've tried to add a method to ->todropdownmap('ID', 'Title') to add two fields instead of 'Title', but I can't get it to work.


Community Member, 541 Posts

30 November 2010 at 5:47am

If you are generating this for a list of Members (or a class that extends Member) then there is a method called "getName" that you can use to get First and Last names. So for example:


If you are using your own custom class, then WillR's technique should work fine.

PS: I have only tested this in 2.4.x, so can't comment on earlier versions.



Community Member, 4 Posts

30 November 2010 at 6:33am

Thanks for your reply. I'm trying to do this for two fields in another data object, so cannot use the inbuilt functions for "member".

I've solved it in the short-term by creating a new field combining the content of the two fields I want to display. But this isn't a very elegant solution.


Community Member, 6 Posts

26 March 2011 at 1:04am

Katy -
You add the function to the class you're getting rather than where you're getting it.

So in the case Joelg says it would be in the Employee Class itself

Carbon Crayon

Community Member, 598 Posts

31 March 2011 at 12:05am

Also, ->toDropdownMap is now Deprecated, use ->map('ID', 'MyTitleFunction', '--empty--');

Have a look at:

That gives you a full tutorial on doing this stuff :)