Jump to:

22972 Posts in 11596 Topics by 2824 members

General Questions

SilverStripe Forums » General Questions » Boolean checkbox to add class?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 3107 Views
  • shakingpaper
    Avatar
    Community Member
    15 Posts

    Boolean checkbox to add class? Link to this post

    I am wanting to add a link class to nav pages on a page-by-page basis and am wondering if there is a way to add this via a boolean checkbox (or any other method, really)?

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Boolean checkbox to add class? Link to this post

    Sure you could use a checkbox if there is only 2 states (true or false).

    You would need to add the database field for it.

    static $db = array(
    'ShowExtraClass' => 'Boolean'
    );

    Add the checkbox to the cms

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Content.Main', new Checkbox('ShowExtraClass'));

    return $fields;
    }

    After running a /dev/build to rebuild the database you should be able to see a new checkbox in the cms and save the value so all you need to add an <% if ShowExtraClass %> in your menu template to conditionally add the class.

    Another option would be to have a free text field for entering the class (or dropdown) if multiple classes.

  • shakingpaper
    Avatar
    Community Member
    15 Posts

    Re: Boolean checkbox to add class? Link to this post

    Thanks for your advice - I ended up mixing the two together and using a text field but placing it in the Behaviour tab to keep it out of the content section (in case it gets edited by mistake)

  • CHD
    Avatar
    Community Member
    217 Posts

    Re: Boolean checkbox to add class? Link to this post

    P.S - the above should be

    $fields->addFieldToTab("Root.Content.Main", new CheckboxField ("ShowExtraClass"));

    3107 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.