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.

General Questions /

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

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

Boolean checkbox to add class?


Go to End


4 Posts   7217 Views

Avatar
shakingpaper

Community Member, 15 Posts

13 April 2010 at 7:16pm

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)?

Avatar
Willr

Forum Moderator, 5523 Posts

13 April 2010 at 10:32pm

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.

Avatar
shakingpaper

Community Member, 15 Posts

14 April 2010 at 1:21pm

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)

Avatar
CHD

Community Member, 219 Posts

10 September 2010 at 12:05am

P.S - the above should be

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