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.

Customising the CMS

CheckBox in Admin Area


Reply

4 Posts   5460 Views

Avatar
Boogiez

4 January 2009 at 12:29pm Community Member, 17 Posts

Hi,

I would like to create a checkbox in my admin area for my client to indicate whether an item in her online gallery has been sold or not. If an item is sold, then I have a little extra HTML to insert in the page ("SOLD. This item has been sold and is available for viewing only.").

I know how to create images and text fields in the CMS, but how to create a checkbox?
And then how do I call an "if" statement on the checkbox in my template page? Would I need to write a function to query the state of the checkbox or can I check its value in the database directly?

Thanks,
Mark

Avatar
Carbon Crayon

4 January 2009 at 3:15pm (Last edited: 4 January 2009 3:18pm), Community Member, 598 Posts

Hi Boogiez, welcome to Silverstripe

I would recommend you do the tutorials to start with, they give you a very good introduction to how both the templates and admin/db work. You can find them here: [url]http://doc.silverstripe.com/doku.php[/url]

To answer your question you can easily add a checkbox to a page in the admin by adding this to your Page.php file:

class Page extends SiteTree {
   
static $db = array(
'CheckboxValue' => 'Boolean'
   );

   
   function getCMSFields() {
   $fields = parent::getCMSFields();
   
   $fields->addFieldToTab("Root.Content.Main", new CheckboxField ("CheckboxValue"));
   
   return $fields;   
   }
   
}

then in your template you simply do this:

<% if CheckboxValue %>
<p>SOLD</p>
<% end_if %>

Hope that helps :)

Avatar
Boogiez

5 January 2009 at 8:33am Community Member, 17 Posts

Thanks very much aram! Works like a charm. I checked the tutorials but I didn't see how to create a checkbox. Do you know if there's a page that lists all of the controls we can offer in the CMS?

Avatar
Carbon Crayon

5 January 2009 at 8:51am Community Member, 598 Posts

yep, check these pages out, I found them very useful when I started and still refer to them often:

Form types: [url]http://doc.silverstripe.com/doku.php?id=form-field-types[/url]

Built in page controls: [url]http://doc.silverstripe.com/doku.php?id=built-in-page-controls[/url]

DataModel: [url]http://doc.silverstripe.com/doku.php?id=datamodel[/url]

Recipies (various how too's): [url]http://doc.silverstripe.com/doku.php?id=recipes:start[/url]

I think the tutorials are invaluable if you want to start developing sites in SS beacuse they give you a really good introduction to how it all works and so once you have done them you can really start descovering the beauty of SS (with the help of the docs and forum of course)

Anyway good luck and hope to see you around the forums :)