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

Custom title on CheckboxSetField


Go to End


2 Posts   1496 Views

Avatar
hpeide

Community Member, 6 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, 5523 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.