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

[SOLVED] How get the CMS form of a DataObject "getCMSForm" method


Go to End


3 Posts   1600 Views

Avatar
Gizzo

Community Member, 2 Posts

18 July 2017 at 1:54am

Edited: 18/07/2017 1:57am

Hi all,

I am saving custom text fields in the CMS, what I mean by that is that where normally you'd have a DataObject with

private static $db = array(
'MyField' => 'Text'
);

And you then have
public function getCMSFields(){
...
TextField::create('MyField', 'Title of the field: ')
...
}
To add the fields to the form.

Well I am not using matching field names because I have custom save logic in place that saves the data some other way. So now I need to populate these textfields with data. Right now everytime you reload the CMS the fields are empty.

So what I'd like to to is
or:

TextField::create('MyCustomNameThatDoesNotMatch', 'Title of the field: ', 'Initial data that the user is editing')

or (what I see as feasable): Some method that returns me the Form instance of the CMS Form so I can use loadDataFrom on it to populate the fields with the data.

I really hope this is possible so I can continue working :)

Thank you all for reading this!

Avatar
Devlin

Community Member, 344 Posts

21 July 2017 at 3:51am

Edited: 21/07/2017 4:09am

At a second glance, there are also extension hooks.

ModelAdmin:
- GridFieldDetailForm_ItemRequest: $this->extend("updateItemEditForm", $form);

GridFieldDetailForm_ItemRequest:
  extensions:
    - MyGridFieldDetailForm_ItemRequestExtension

class MyGridFieldDetailForm_ItemRequestExtension extends DataExtension {
	public function updateItemEditForm($form) {
		$form->loadDataFrom($myRecord);
	}
}

CMS:
- CMSMain: $this->extend('updateEditForm', $form);

CMSMain:
  extensions:
    - MyCMSMainExtension

class MyCMSMainExtension extends DataExtension {
	public function updateEditForm($form) {
		$form->loadDataFrom($myRecord);
	}
}

Avatar
Gizzo

Community Member, 2 Posts

21 July 2017 at 4:25am

Thanks a lot! That is an excellent answer to what I asked. Fortunately I found out today that FormFields have a method called setValue() I really don't know how I missed that (for multiple hours!)
Anyway, thank you for your answer!