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.

Customising the CMS /

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

Accessing to a custom field in CMS DataObject


Go to End


3 Posts   1596 Views

Avatar
zz

Community Member, 16 Posts

13 April 2015 at 7:23pm

Hello,
I have added a custom TextField into CMS of a DataObject

function getCMSFields() {
  $fields = parent::getCMSFields();
  ...
  $fields->addFieldToTab('Root.Main', new TextField('MyField', 'My custom field'));
 ..
}

And I do not want how ti access to it (value, etc) from other method like function ManageMyField(). I have tried a lot of variations but none works.. Do you have an idea?

Avatar
Pyromanik

Community Member, 419 Posts

13 April 2015 at 9:24pm

Edited: 13/04/2015 9:25pm

You've added a field to the CMS, but you've not hinted at adding it to the model.

  • If you wish to access the data later, then you will need to create a field on the model.
  • If you wish to use this field to obtain information to modify before storing, then perhaps a hiddenfield and some javascript onchange to this field you've already added to store the value would be an acceptable solution.
  • If you want to store information on the model, but not have the field show in the CMS, simply add the field to the model (private static $db) but do not insert the field to the edit form via the getCMSFields() function.

Avatar
zz

Community Member, 16 Posts

13 April 2015 at 9:50pm

Thanks for the reply.
I want to use it to create another DataObject with it's value and I thought I can just get if from some DataObject's methods and use it in onBeforeWrite() for example.
I am new in SilverStripe so I do not know much, by creating a field on the model you mean creating some private $field?