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

adding CheckboxSetField on new Page to CMS - tab does not appear


Reply

3 Posts   322 Views

Avatar
acktivate

18 December 2013 at 12:57pm Community Member, 11 Posts

I am running 3.1.2 and trying to add a group of checkboxes to the CMS. I've added the form to manage the checkboxes to the CMS and that seems to work but I am unable t

I found an example on the internet from 2011 based on the 2.x framework and can't seem to get it working for 3.x. Feature.php and FeatureAdmin.php seem to work but I can't get the list of checkbox values to appear on a tab in the CMS labeled FeaturePage.php - the tab won't appear.

suggestions?

My code is below from mysite/code

FeaturePage.php
<?php
class FeaturePage extends Page {

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

function getCMSFields() {
$fields = parent::getCMSFields();
$features=Feature::get();
$map = $features->toDropdownMap('ID', 'Name', '(Select one)', true);
       $fields->addFieldToTab('Root.Features', new CheckboxSetField($name = 'Features',$title = 'Select Features',$source = $map));
}
}

class FeaturePage_Controller extends Page_Controller {
}

FeatureAdmin.php
<?php
class FeatureAdmin extends ModelAdmin {

private static $managed_models = array(
'Feature'
);

private static $url_segment = 'features';
private static $menu_title = 'Features';
}

Feature.php
<?php
class Feature extends DataObject {

private static $db = array(
'Name' => 'Varchar(255)',
'Description' => 'HTMLText'
);

public function getCMSFields() {

$fields = new FieldList();

$fields->push(new TextField('Name', 'Name of the feature'));
$fields->push(new TextareaField('Description', 'Short description'));

return $fields;
}
}

Avatar
martimiz

6 January 2014 at 11:04pm Forum Moderator, 1091 Posts

Hi,
Sorry for the late reply, I hope you did already find an answer to your question, but just in case: two things actuallywrong with your code:

- Your pages getCMSFields didn't return any fields
- toDropdownMap() doesn't exist in v3.1, use map().

FeaturePage:

<?php
class FeaturePage extends Page {

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

   public function getCMSFields() {
      $fields = parent::getCMSFields();
   
      $features=Feature::get();
      $map = $features->map('ID', 'Name', '(Select one)', true);
      
      $fields->addFieldToTab(
       'Root.Features',
       new CheckboxSetField($name = 'Features',$title = 'Select Features',$source = $map));
   
      return $fields;
   }
}

class FeaturePage_Controller extends Page_Controller {
}

Also in Feature I would add the reverse relation, to make it possible to get the pages belonging to a certain feature:

   private static $belongs_many_many = array(
    'FeaturePages' => 'FeaturePage'
   );

Martine

Avatar
acktivate

10 January 2014 at 1:09pm (Last edited: 12 January 2014 7:01am), Community Member, 11 Posts

Hi Martine -

No worries. Your info helped solved the problem but it is not functioning as intended. I fixed the intended functionality by using a Data Extension. It is now working correctly. Thank you.