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.

Form Questions /

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

Mapping Two Fields using toDropDownMap ??


Go to End


3 Posts   3479 Views

Avatar
ciaranhickey

Community Member, 17 Posts

26 January 2010 at 2:43am

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

Avatar
zenmonkey

Community Member, 545 Posts

29 January 2010 at 7:34am

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

Avatar
Rossel

Community Member, 18 Posts

8 March 2012 at 4:35pm

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'