Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » toDropdownMap question

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

Page: 1
Go to End
Author Topic: 3108 Views
  • joelg
    Avatar
    Community Member
    129 Posts

    toDropdownMap question Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: toDropdownMap question Link to this post

    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

  • joelg
    Avatar
    Community Member
    129 Posts

    Re: toDropdownMap question Link to this post

    Thanks Willr

    I'll give it a try.

  • Katy
    Avatar
    Community Member
    4 Posts

    Re: toDropdownMap question Link to this post

    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.

  • Mo
    Avatar
    Community Member
    504 Posts

    Re: toDropdownMap question Link to this post

    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

  • Katy
    Avatar
    Community Member
    4 Posts

    Re: toDropdownMap question Link to this post

    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.

  • phillprice
    Avatar
    Community Member
    6 Posts

    Re: toDropdownMap question Link to this post

    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

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: toDropdownMap question Link to this post

    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

    3108 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.