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.

Form Questions /

Custom title on CheckboxSetField


Reply


2 Posts   539 Views

Avatar
hpeide

Community Member, 3 Posts

26 October 2012 at 11:08pm

Hi!

Is there a way to set an custom title on the CheckboxSetField items in SS3? eg. I have a Product with an title and a group title and I would like to display both titles in the CheckboxSetField, like "[x] Product title, (Group Title)".

class Deal extends DataObject {

public static $db = array(
'Title' => 'Varchar(255)'
);

public static $has_one = array(
"HomePage" => "HomePage"
);

public static $many_many = array(
"Products" => "Product"
);

public function getCMSFields()
{
return new FieldList(
new TextField("Title", _t("Deal.Title", "Tittel")),
new CheckboxSetField('Products', _t("Deal.MaltProduct", "Produkter"), DataObject::get('Product'))
);
}
}

Avatar
Willr

Forum Moderator, 5513 Posts

2 November 2012 at 11:55am

The CheckboxSet api simply takes an associative array which can be used with the SS_Map class to generate an array from the DataList.

public function getCMSFields() {
$products = new SS_Map(Product::get(), 'ID', 'DropdownTitle');

return new FieldList(
new TextField("Title", _t("Deal.Title", "Tittel")),
new CheckboxSetField('Products', _t("Deal.MaltProduct", "Produkter"), $products->toArray())
);
}

Then define a getDropdownTitle() function on your Product object to return the format you want.

You could also manually create the associative array inside this function (using a foreach loop) but that's a little messier.