Jump to:

1976 Posts in 1306 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » Custom title on CheckboxSetField

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

Page: 1
Go to End
Author Topic: 346 Views
  • hpeide
    Avatar
    Community Member
    3 Posts

    Custom title on CheckboxSetField Link to this post

    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'))
    );
    }
    }

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Custom title on CheckboxSetField Link to this post

    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.

    346 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.