Set two fields using a map

In SilverStripe, I want to return two fields when I use map in DropdownField .

I have a Teacher data object with fields firstname and lastname . So in my DropdownField I want to combine these two fields and pass them to map() .

My current code is as follows:

  public function getCMSfields() { $fields = FieldList::create(TabSet::create('Root')); $fields->addFieldsToTab('Root.Main', array( DropdownField::create('TeacherID', 'Teacher')->setSource(Teacher::get()->map('ID', 'Firstname'))->setEmptyString('Select one') ); // etc... return $fields; } 

How can I combine firstname and lastname and pass it inside map() and return it to DropdownField .

+5
source share
1 answer

We can create get functions in our custom DataObject to return any content we like. These functions can be used in many places, including the map function.

Here's how to add the getFullName function to return the FullName string to our object:

 class Teacher extends DataObject { // ... public function getFullName() { return $this->FirstName . ' ' . $this->LastName; } } 

Then in our DropdownField we can get Teacher::get()->map('ID', 'FullName') like this:

 public function getCMSFields() { $fields = parent::getCMSFields(); $fields->addFieldsToTab('Root.Main', array( DropdownField::create('TeacherID', 'Teacher') ->setSource(Teacher::get()->map('ID', 'FullName')) ->setEmptyString('Select a teacher') ); return $fields; } 
+9
source

All Articles