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

Adding a simple multi-checkbox list on the CMS to show list items on public pages


Go to End


8 Posts   6481 Views

Avatar
octopuscreative

Community Member, 10 Posts

16 July 2011 at 9:19am

Hi, hope someone can help with my problem. I'm relatively new to Silverstripe and am getting up to speed with the CMS framework. I've put together some project overview pages on a site, they show images, a brief description and a section that lists the key services used on a particular project, ie:

- Copywriting
- Photography
- Logo design
- Brochure design...etc

Rather than let the client type these services out on every project page I want to put a section on the CMS that lists 15-20 master items. When the client builds a new project page they select which services are relevant to a project with checkboxes, that way only the defined list items appear on the public pages as appropriate.

So far I've managed to configure a single checkbox sitewide on the CMS with the following (from this forum):

class Page extends SiteTree {

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

function getCMSFields() {
$fields = parent::getCMSFields();

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

return $fields;
}

}

with the following on my template:

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

But I'm struggling to see how I can add multiple checkboxes against a defined list on the CMS. Ideally an ordered list of the items on a separate tab in the CMS would keep things neat and simple for my client to administer.

Any help greatly appreciated!

Avatar
stallain

Community Member, 68 Posts

16 July 2011 at 11:45am

Edited: 16/07/2011 11:46am

Hi,

Instead of creating 15 or 20 checkboxes, I would rather create a dataobject named "KeyService", and then define the kind of relationship between your project page and your key services (can a project belong to several key services or not ?).
Then you can use a complextablefield in the cms to enable the user to select one or several categories per project (she/he can even add new services if necessary).
In the template, you will only need a simple control to call your key services in a project page : something like <% control KeyServices %>.

As a start, I suggest you refer to this tutorial : http://doc.silverstripe.org/sapphire/en/tutorials/5-dataobject-relationship-management

Stan

Avatar
octopuscreative

Community Member, 10 Posts

17 July 2011 at 7:17am

Stan, thanks for the feedback. The various projects can have multiple key services so I'll work through the tutorial as a starting point and adapt to suit. I was unsure of the best approach for this so your advice is much appreciated. I'll let you know how I get on!

Matt

Avatar
martimiz

Forum Moderator, 1391 Posts

21 July 2011 at 5:31am

Edited: 21/07/2011 5:31am

I did something similar recently and thought to document it in in my online logs. Maybe it can be of some help to you as well:

http://www.balbus.tk/many-many-checkboxsetfield/

Avatar
stallain

Community Member, 68 Posts

25 July 2011 at 12:08pm

@Matt : so, are you OK with your project ? If you need more help, just whistle :)

Avatar
octopuscreative

Community Member, 10 Posts

2 August 2011 at 4:17am

Stan, apologies for the silence, I've had a few other web projects that needed attention but am back on this now. I've worked through the sample Martimiz posted and that worked perfectly for my project. My problem now is I need to combine an image gallery function on the same page as the multi-checkbox feature.

I have the two functions working exclusively on separate pages but am having problems combining the 2 php scripts onto 1 page, here's what I have for my image gallery function:

<?php
/**
* Defines the FeaturePage page type
*/
class MarketingProjectPage extends Page {

static $has_many = array (
'Images' => 'ImageResource'
);

public static $has_one = array(
);

public function getCMSFields() {
$f = parent::getCMSFields();
$manager = new ImageDataObjectManager(
$this, // Controller
'Images', // Source name
'ImageResource', // Source class
'Attachment', // File name on DataObject
array(
'Title' => 'Title',
'Caption' => 'Caption'
), // Headings
'getCMSFields_forPopup' // Detail fields
// Filter clause
// Sort clause
// Join clause
);
$f->addFieldToTab("Root.Content.Gallery",$manager);
return $f;

return $fields;
}
}

class MarketingProjectPage_Controller extends Page_Controller {
function init() {
parent::init();

//Include fancybox Code
Requirements::javascript('themes/genesism/js/jquery.js');
Requirements::javascript('themes/genesism/js/fancybox.js');
Requirements::javascript('themes/genesism/js/gallery.js');
Requirements::themedCSS('fancybox');
}

}

?>

and I need to combine the above with the many-many-checkbox php script below:

<?php
/**
* Defines the FeaturePage page type
*/
class FeaturePage extends Page {

// pages can have many features
public static $many_many = array(
'Features' => 'Feature'
);

function getCMSFields() {
$fields = parent::getCMSFields();

// get all existing features
$features= DataObject::get('Feature');

if (!empty($features)) {

// create an array('ID'=>'Name')
$map = $features->toDropdownMap('ID', 'Name');

// create a Checkbox group based on the array
$fields->addFieldToTab('Root.Content.Features',
new CheckboxSetField(
$name = "Features",
$title = "Select Features",
$source = $map
));
return $fields;
}
}
}

class FeaturePage_Controller extends Page_Controller {

}

?>

I've had a go at combining and can post my efforts if that helps. I can't seem to get the Gallery and Feature tabs to appear on the CMS. I'll work through the tutorial you suggested Stan and see if that provides some answers!

thanks, matt

Avatar
stallain

Community Member, 68 Posts

2 August 2011 at 1:25pm

Hi Matt, I don't think the tutorials will help you at this point. If both scripts work separately, there may be something wrong with the way you combine them. Could you post your whole script (gallery + features on the same page), please ?
Stan

Avatar
octopuscreative

Community Member, 10 Posts

3 August 2011 at 9:33pm

Stan, I went with your tutorial suggestion in the end, the 'Modules' part was the function that suited the project I was working on (great tutorial by the way, really nice introduction to managing relations and $has_one, $has_many, $many_many functions. I'm sure I'll be referring to this again). I solved the problem of combining the 2 scripts, here's the final code:

<?php
class MarketingProjectPage extends Page {

public static $db = array(
);
public static $has_one = array(
);
static $has_many = array (
'Images' => 'ImageResource'
);
static $many_many = array(
'Modules' => 'Module'
);

public function getCMSFields() {
$f = parent::getCMSFields();
$manager = new ImageDataObjectManager(
$this, // Controller
'Images', // Source name
'ImageResource', // Source class
'Attachment', // File name on DataObject
array(
'Title' => 'Title',
'Caption' => 'Caption'
), // Headings
'getCMSFields_forPopup' // Detail fields
// Filter clause
// Sort clause
// Join clause
);
$f->addFieldToTab("Root.Content.Gallery",$manager);

$modulesTablefield = new ManyManyComplexTableField(
$this,
'Modules',
'Module',
array(
'Name' => 'Name'
),
'getCMSFields_forPopup'
);
$modulesTablefield->setAddTitle( 'A Service' );

$f->addFieldToTab( 'Root.Content.Services', $modulesTablefield );

return $f;
}
}

class MarketingProjectPage_Controller extends Page_Controller {
function init() {
parent::init();

//Include fancybox Code
Requirements::javascript('themes/genesism/js/jquery.js');
Requirements::javascript('themes/genesism/js/fancybox.js');
Requirements::javascript('themes/genesism/js/gallery.js');
Requirements::themedCSS('fancybox');
}

}

?>

I think the errors were because the one script used $f command and the other used $field. I converted everything to $f and it seemed to do the trick? Does this shortened version of code act a bit like CSS ie #000000 can be shortened to #000?

Thanks for your interest in my project Stan, I'm finding these forums invaluable for development work. Good to know I have great support on hand!

matt