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

How to run a function after certain CMS fields have been changed


Go to End


4 Posts   2664 Views

Avatar
Tama

Community Member, 138 Posts

3 October 2011 at 3:22pm

Edited: 03/10/2011 3:24pm

I've got a function I want to run on page save/ publish if certain custom CMS fields have been changed.

For example:

class Foo extends DataObjectDecorator {
 public function updateCMSFields() {
  $fields->addFieldsToTab("Root.Content", array(
   new NumericField('Bar', 'Change me and save this page to trigger FooBar function')
  ));
 }
}

function FooBar(){
 //Do things
 //With stuff
}

Any ideas?

Avatar
swaiba

Forum Moderator, 1899 Posts

3 October 2011 at 9:47pm

create some custom javascript to handle the change (inspect the current form to get the required ID to watch), add a REquirements::javascript within the function - job done.

Avatar
Tama

Community Member, 138 Posts

4 October 2011 at 8:40am

Hi Swaiba

Thank you for that.

I understand the principle of what you're saying but struggling with the correct execution.

Do you have any code examples of how to do this?

Cheers
Tama

Avatar
martimiz

Forum Moderator, 1391 Posts

6 October 2011 at 8:07am

Edited: 06/10/2011 8:07am

Looking at the SiteTree::onBeforeWrite() function I noticed the use of $this->isChanged('URLSegment') (line 1339). So maybe this would work in your Page class:

protected function onBeforeWrite() {

	if ($this->isChanged('SomeField')){
		// do something
	}

	parent::onBeforeWrite();
}

Mind - I didn't test it...