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.

General Questions /

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

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

Data object trouble


Reply


5 Posts   949 Views

Avatar
Harley

Community Member, 153 Posts

14 September 2009 at 12:47pm

Hi,

Having a small problem here with a data object, just can't see the problem, maybe someone can spot it.

Basically I have a data object that I am trying to use in my homepage, but when I click on my tab I am getting no form field as you will see in the attached image.

This is the code in my homepage:

class elevatorHomePage extends Page {

   static $db = array();
   
   static $has_one = array(
      'myAboutPage' => 'AboutPage'
   );
      
   static $icon = "themes/elevator/images/treeicons/home";

   static $allowed_children = array('AboutPage', 'VacanciesPage');
   
   function getCMSFields() {
      $fields = parent::getCMSFields();

      $tableField = new ComplexTableField(
         $this,
         'myAboutPage',
         'AboutPage',
         array(
         'AboutContent' => 'About'
         ),
      'getCustomCMSFields'
      );
      
      $tableField->setParentClass('elevatorHomePage');
      
      $fields->addFieldToTab('Root.Content.About', new HtmlEditorField('About'), $tableField);
      return $fields;
   }
   
}

and this is the corresponding data object:

class AboutPage extends DataObject {
   static $db = array(
      'AboutContent' => 'Text',
   );
   
   static $has_one = array(
      'elevatorHomePages' => 'elevatorHomePage'
   );

function getCMSFields() {
      $fields = parent::getCMSFields();
      $fields->addFieldToTab('Root.Content.About', new HtmlEditorField('About'), 'AboutContent');
return $fields;
}

}

Attached Files
Avatar
banal

Community Member, 901 Posts

14 September 2009 at 6:52pm

When you setup your ComplexTableField, you're supplying the constructor with the name of the method to get the DataObject Form-Fields (for editing in the CMS). In your case, you set this to: getCustomCMSFields, therefore you should rename the method of your DataObject from getCMSFields to getCustomCMSFields.

Avatar
Harley

Community Member, 153 Posts

15 September 2009 at 12:32pm

Thanks for the info, I've changed the name of the method so everything now corresponds, I've reverted it back to getCMSFields.

No joy though, what else could be wrong with the script?

Avatar
UncleCheese

Forum Moderator, 4096 Posts

16 September 2009 at 9:09am

You're trying to add two fields at once.

$fields->addFieldToTab('Root.Content.About', new HtmlEditorField('About'), $tableField);

either use addFieldsToTab("tab",array(field1,field2))'

or

addFieldToTab("tab",field1);
addFieldToTab("tab",field2);

Avatar
Harley

Community Member, 153 Posts

16 September 2009 at 12:45pm

Ok, I've tried that too and just to show where I am up to here with the code here we are;

About;

class AboutPage extends DataObject {
   static $db = array(
      'AboutContent' => 'Text',
   );
   
   static $has_one = array(
      'elevatorHomePages' => 'elevatorHomePage'
   );

   function getCMSFields(){
      $fields = parent::getCMSFields();
      $fields->addFieldToTab('Root.Content.Main', new HtmlEditorField('About'), 'Content');
   return $fields;
   }
}

Home;

class elevatorHomePage extends Page {

   static $db = array();
   
   static $has_one = array(
      'myAboutPage' => 'AboutPage'
   );
      
   static $icon = "themes/elevator/images/treeicons/home";

   static $allowed_children = array('AboutPage');
   
   function getCMSFields() {
      $fields = parent::getCMSFields();
      $fields->addFieldToTab('Root.Content.About', new HtmlEditorField('About'));
      return $fields;
   }
   
}

Still no joy though, I get a HtmlEditorField, but when I enter content here and save it, then go back to it, it reverts back to a blank field.

It looks correct to me, is there anything glaringly obvious I have missed here?

Thanks