Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » Mapping Two Fields using toDropDownMap ??

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

Page: 1
Go to End
Author Topic: 1833 Views
  • ciaranhickey
    Avatar
    Community Member
    17 Posts

    Mapping Two Fields using toDropDownMap ?? Link to this post

    Hi,

    I'm trying to figure out how to show two fields side by side in a single, mapped dropdown. For example in the code below I want the dropdown to display the combination of FirstName + LastName (and Map it to that Author's ID). I can work with a single field (either FirstName OR LastName) but can't seem to concatenate them both to be displayed in the Dropdown...

    //This works fine but I need to include the LastName also
    $authorData = DataObject::get('Author');
    if($authorData->exists()) {
       $AuthorSource = $authorData->toDropDownMap('ID', 'FirstName', 'Select Author');
    }

    I tried using a function eg.

    $authorData->toDropDownMap('ID', 'getFullName', 'Select Author');

    and the function

    function getFullName(){
          
    $authorData = DataObject::get('Author');
    return $authorData->FirstName.", ".$authorData->LastName;

    }

    But I had no luck with this. Would appreciate any help on this. It's driving me mad.

    Regards,
    Ciarán

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: Mapping Two Fields using toDropDownMap ?? Link to this post

    Well an you could also create an extra column on your DataObject that contains the full name and fill it with an onBeforeWrite instead of manually filling it in. Thats a quick in-elegant fix. My only other idea is even more complex. You could output the first name and last name to arrays and marry them back up, but I don't know if you can Map and array to drop down

  • Rossel
    Avatar
    Community Member
    18 Posts

    Re: Mapping Two Fields using toDropDownMap ?? Link to this post

    I know this is very late but I recently just ran into the same issue. So my solution may help for those that find this page.

    You can reference a function, as you know, in your toDropDownMap call instead of a field.

    $member = $member->toDropdownMap('ID', 'getName', '(Select one)', true);

    Either create a function in your custom class. Or use these ones that already exist in the Member class:
    getTitle will return 'Surname, FirstName'
    getName will return 'FirstName Surname'

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