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

toDropdownMap question


Go to End


8 Posts   4772 Views

Avatar
joelg

Community Member, 134 Posts

22 March 2010 at 2:27am

Hi

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...

Thanks

Joel

Avatar
Willr

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

Avatar
joelg

Community Member, 134 Posts

22 March 2010 at 11:46am

Thanks Willr

I'll give it a try.

Avatar
Katy

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.

Avatar
Mo

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:

DataObject::get('Member')->toDropdownMap('ID','Name');

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.

Mo

Avatar
Katy

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.

Avatar
phillprice

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

Avatar
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: http://www.ssbits.com/tutorials/2010/dynamically-generating-a-dropdown-list/

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

Aram