CheckboxSetField use more than 1 field of the linked resource as a label

Silverstripe Version:


Hi everyone,
just a small question, maybe I don’t see the forest bc of the trees:

What I need is a CheckboxSetField in the cms backend, currently it works with the last names of some people, but there are more than just 1 with the same last name - so I am trying to convince the CheckboxSetField to not only show the lastname in every checkbox title, but as well the first name ( like [checkbox] Myers, Michael

Btw, ‘Sprecher’ class has a firstname and a lastname.
Any ideas on that?
Thanks in advance to all of you!

CheckboxSetField::create('Sprechers', 'Select Sprecher', DataObject::get('Sprecher')));

You could try putting something like this on your Sprecher class:

public function getTitle()
    if (!$this->FirstName && !$this->LastName) return "";
    return "$this->LastName, $this->FirstName";

Other option would be to manually create an array to pass in as the third arg, where Key is ID of the Sprecher and Value is whatever you like.

JonoM, thanks for your reply - I don’t really get it, you mean I should write a function that manipulates the Sprechers Dataobject in a way that the ‘Name’ would be ‘Name’ . ’ '. ‘Firstname’ and then push it into the checkboxsetfield function?

But would it then save correctly?

When deciding what to use as a title for a dataobject in the CMS, Silverstripe will call getTitle() on the dataobject. By default (from memory) this will return the value of the Title or Name database field id present, but you can override this method to return whatever you like.

It shouldn’t interfere with any saving, it’s only the ID that is used for this. When you pass DataObject::get('Sprecher') that would be reduced to a simple key/value array behind the scenes. See the API docs for an example: SilverStripe\Forms\CheckboxSetField | SilverStripe API

Edit: If you have a database field named Title on this dataobject, then yes, it would interfere with saving and you shouldn’t do this :grinning_face_with_smiling_eyes:. But I’m assuming since you have FirstName and LastName that you don’t have a Title field.

Thanks Jono, so it will call getTitle automatically then?
So I could construct whatever Iwant, and then it gets displayed for every checkbox?

(If I got you right… ^^ )

Yes but I’m going from memory here :grinning_face_with_smiling_eyes: best to give it a shot and see what happens! You can loop over DataObject::get('Sprecher') and create a custom array from it to pass to your CheckboxSetField constructor if the getTitle() approach doesn’t work.

Jono is right here.

One point is this will change the name in the CMS UI for these records when editing. If you don’t want that and you only want to change it for this situation you could do:

CheckboxSetField::create('Sprechers', 'Select Sprecher', DataObject::get('Sprecher')->map('ID', 'SpecificTitle')));

Then in your Sprechers data object write this function:

public function getSpecificTitle()
    if (!$this->FirstName && !$this->LastName) return "";
    return "$this->LastName, $this->FirstName";

Different name, same as Jono’s.

If SS can’t find the field, it’ll try and find a function with the same name or with get prepended.

Personally, I prefer the get prefix to give a clue to what it does.

1 Like

I couldn’t remember if map accepted a method name, glad to see that it does!

Thanks for your help! It works well.

pluri sistemas

Thanks guys,
I think getSpecificTitle is the best (coolest) solution.
Thanks for your help!!