Jump to content

Page on save:: copy field value into (hidden) field


matjash
 Share

Recommended Posts

I need some help.

I have fields price and priceBefore

I want to copy price value on save to priceBefore field.

    public function init() {
        $this->pages->addHookAfter('save', $this, 'hookSave');
        //$this->pages->addHookAfter('added', $this, 'hookAdded');
    }
    public function hookSave(HookEvent $event) {
        // this function called any time a page is saved (whether new or not)
        //$page = $event->object;
        $page = $event->arguments[0];
        // now you can access anything from the $page
        if ($page->template == "nepremicnine-vnos") {
            // get value from price field and copy to priceBefore/cenaStara
            $page->cenaStara = $page->cena;
            $page->save("cenaStara");
            
        }
    }

I 've read some examples but I just can't make it work.

Is it maybe better to put it in InputField::render? 

    public function init() {
            // add before-hook to the inputfield render method
            $this->addHookBefore("Inputfield::render", $this, "renderField");
        }

    public function renderField(HookEvent $event) {
        // // get the current field
        $field = $event->object;
        if($field->name == 'cenaStara' && $field->value == '' ) {
            $field->cenaStara = $field->cena; 
            $oldPrice = $field->cenaStara          
            $field->set('value', $oldPrice);
        }
    }
Link to comment
Share on other sites

You cannot (simply) save within the hook that is called after save. You need to avoid endless loop.

Add a temporary property to the page object you have altered and check for this in the beginning of the hook method:

    if (page->imAlreadyDone) return;  // is already altered
    if ($page->template != "nepremicnine-vnos") return;  // is the wrong page type
    
    $page->cenaStara = $page->cena;   // alter data
    $page->imAlreadyDone = true;      // flag page as altered
    $page->save("cenaStara");         // save
Link to comment
Share on other sites

Untested, but you could use trackChange.

public function init() {
    $this->addHookAfter('Pages::saveReady', $this, 'hookPageSave');
}

public function hookPageSave($event) {

    $page = $event->arguments[0];
    
    // check template
    if ($page->template == "nepremicnine-vnos") {

        // If there was a change to the price field
        if ($page->trackChange('price')) {
            
            // get the old page from the DB (contains the previous value of 'price');
            $old = $this->pages->get($page->id);
            
            // set the value of priceBefore to previous value of price.
            $page->priceBefore = $oldPage->price;

        }
    }
}
There is no need to do a save(), as the hook is just editing the values of the page object right before it's saved.
  • Like 2
Link to comment
Share on other sites

Thank you guys. In the meantime I found another solution which suits my need:

    public function init() {
        $this->addHookAfter('Pages::saveReady', $this, 'afterSaveReady');
    }

    public function afterSaveReady($event) {
        // Get the soon to be saved page object from the given event
        $page = $event->arguments[0];

        // Sample condition and changes
        if($page->template == "nepremicnine-vnos" && !$page->isTrash()){
            //Price
            $cena = $page->cena;
            //Old price
            $stara = $page->cenaStara;
            $znizano = $page->znizano;

            //Checkbox to display old price, if not checked on save copy price to oldPrice
            if($znizano == 0) {
                $page->set("cenaStara", $cena);
                $page->save("cenaStara");
            }
        }
    }
}

I will look into the way of track field changes. It seems usefull for future development.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...