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 new Textarea fields to CMS tab


Reply

6 Posts   5833 Views

Avatar
Brady.Dyer

17 December 2008 at 2:30am (Last edited: 17 December 2008 2:31am), Community Member, 21 Posts

Ok I've been trying to add a new field to a new page for about 4hours! its driving me crazy. I'm trying to add two new fields called Amy and Tash, which are text area fields, which can be edited in the CMS...

I've cut it down to just try and get ONE to work (the 'AboutAmy' field)

<?php
class AboutUsPage extends Page {
static $db = array(
);
static $has_one = array(
          'AboutAmy' => 'Text',
);
   
function getCMSFields( $cms ) {
      $fields = parent::getCMSFields( $cms );
      $fields->addFieldToTab("Root.Content.Main", new TextareaField("AboutAmy", "About Amy"))
      return $fields;
   }
}

class AboutUsPage_Controller extends Page_Controller {
      }
      
?>

It shows up in the CMS fine... when you put something in the box, click save everything's fine - till you reload the page, then what ever change you made is NOT saved? Any ideas anyone?

Also - BIG UPS to the new SS site. so much nicer and easier to use, especially the new forum =)

Avatar
Brady.Dyer

17 December 2008 at 2:39am (Last edited: 17 December 2008 2:39am), Community Member, 21 Posts

Well just fixed my own problem =) so thought I'd put up the fix here that I found [url=http://www.silverstripe.org/archive/show/502]here[/url]

When you put:

static $has_one = array(
          'AmyInfo' => 'Text',
);

It also adds a AmyInfoID into the table, so when you are adding this to the CMS you need to put:

$fields->addFieldToTab("Root.Content.Main", new TextField("AmyInfoID", "About Amy"));

Avatar
Brady.Dyer

17 December 2008 at 3:04am Community Member, 21 Posts

Ok fixed that problem... but for some reason when rebuilding the CMS it is setting the field to be "int(11)"

Field AboutUsPage.AmyID: created as int(11) not null default '0'
Field AboutUsPage.TashID: created as int(11) not null default '0'

Any ideas how to change this to text?

Avatar
Brady.Dyer

17 December 2008 at 3:23am Community Member, 21 Posts

are these two fields meant to be in has_one or db?

I tried this

<?php

class AboutUsPage extends Page {
static $db = array(
         'Info_Amy' => 'Text',
          'Info_Tash' => 'Text',
);
static $has_one = array(
   

);
   
   
   
   
function getCMSFields( $cms ) {
      $fields = parent::getCMSFields( $cms );
      
      $fields->addFieldToTab("Root.Content.Main", new TextareaField("Info_AmyID", "About Amy", "8", "3"));
      $fields->addFieldToTab("Root.Content.Main", new TextareaField("Info_TashID", "About Tash", "8", "3"));
      $fields->removeFieldFromTab("Root.Content.Main","Content");

         
         
         
      return $fields;
   }
   
}

class AboutUsPage_Controller extends Page_Controller {
   
      
      }
      
?>

and it now makes the fields 'mediumtext' not 'int(11)' - however when i put something into the CMS click save, it says it has saved, but when I open the page again - there is nothing in the fields in the CMS.

Avatar
Brady.Dyer

17 December 2008 at 3:26am Community Member, 21 Posts

Ahhh shesh fixed my problem again :p haha.

When you move the fields into $db - it doesn't automatically add -ID to the end, so removed the ID's from the:
      $fields->addFieldToTab("Root.Content.Main", new TextareaField("Info_Amy", "About Amy", "8", "3"));
line and all works fine =)

here is the final code for those that care.

<?php

class AboutUsPage extends Page {
static $db = array(
         'Info_Amy' => 'Text',
          'Info_Tash' => 'Text',
);
static $has_one = array(
   

);
   
   
   
   
function getCMSFields( $cms ) {
      $fields = parent::getCMSFields( $cms );
      
      $fields->addFieldToTab("Root.Content.Main", new TextareaField("Info_Amy", "About Amy", "8", "3"));
      $fields->addFieldToTab("Root.Content.Main", new TextareaField("Info_Tash", "About Tash", "8", "3"));
      $fields->removeFieldFromTab("Root.Content.Main","Content");

         
         
         
      return $fields;
   }
   
}

class AboutUsPage_Controller extends Page_Controller {
   
      
      }
      
?>

Avatar
Martin Pales

17 December 2008 at 10:36am Community Member, 19 Posts

This is also covered by the tutorial here:

http://doc.silverstripe.com/doku.php?id=tutorial:2-extending-a-basic-site#adding_date_and_author_fields