Jump to:

22998 Posts in 11853 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Data object trouble

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

Page: 1
Go to End
Author Topic: 824 Views
  • Harley
    Avatar
    Community Member
    153 Posts

    Data object trouble Link to this post

    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
  • banal
    Avatar
    Community Member
    901 Posts

    Re: Data object trouble Link to this post

    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.

  • Harley
    Avatar
    Community Member
    153 Posts

    Re: Data object trouble Link to this post

    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?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Data object trouble Link to this post

    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);

  • Harley
    Avatar
    Community Member
    153 Posts

    Re: Data object trouble Link to this post

    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

    824 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.