Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » How to run a function after certain CMS fields have been changed

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

Page: 1
Go to End
Author Topic: 598 Views
  • Tama
    Avatar
    Community Member
    119 Posts

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

    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?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: How to run a function after certain CMS fields have been changed Link to this post

    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.

  • Tama
    Avatar
    Community Member
    119 Posts

    Re: How to run a function after certain CMS fields have been changed Link to this post

    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

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: How to run a function after certain CMS fields have been changed Link to this post

    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...

    598 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.