Jump to content

Hooks, compare page before and after save


heldercervantes
 Share

Recommended Posts

Hi guys, I almost feel like a grown up, now that I'm messing with creating modules.

I'm trying to do something that, when a page is saved in the admin, detects certain changes and reacts accordingly.

Example: Say an orderState field for... an order's state. And when the admin changes that state to something else, I want to fire an alert email to the client.

So I started from this example and I'm working my way up from there. It's working fine, but now I need to know how to add a hook that gives me the page as it was before saving. I tried `...addHookBefore("save"...` and `...addHookBefore("saveReady"...`, but both give me the page after the changes.

I'm sure it's super simple and I'm going to say "of course" once someone points me to the obvious solution that's right in front of me, but for now all I can do is bang my head against the wall and thank you in advance for your much appreciated help.

  • Like 1
Link to comment
Share on other sites

I've managed to get the uncached version of the page and do comparisons successfully. Now my problem is something else.

One of the fields I'm comparing is a repeater, and I'm noticing that in this case the saveReady hook is fired twice (or more), one time for the new entry in the repeater, and another for the page. So far so good.

But now I need to compile changes and react to them all in one go at the end. So here's what I did:

  1. Hook before saveReady, I start by looking at $session->controlArray and set it as a new array if unset;
  2. Push to that array as I compare the various thingies;
  3. Hook after save parses controlArray, reacts to what's changed, then resets the array.

But my controlArray seems to be scrapped after each hook.

Link to comment
Share on other sites

3 minutes ago, netcarver said:

Sounds like one of my older modules.

Hey @netcarver, I saw your module on my research. Cool stuff. But my case is different and too specific. It's for an online store, and it looks for specific stuff like changing the orders' state or adding a note. It then looks for the client's email address and sends out a notification.

I'm also implementing a way of having email templates for various things, like welcoming a new user, notifying of password changes, stuff like that, which are editable in the admin, and are triggered by the frontend when need be. And having fun doing it.  

Link to comment
Share on other sites

4 hours ago, heldercervantes said:

Damn were you right. I'm hooked. Refactoring a bunch of stuff with a grin from ear to ear.

as a next step i would recommend you try the processhello module soon! i was somehow afraid of building process modules but it really made "click" in my head once i got it. and it's often a lot easier to create a processmodule than hooking and modifying the page edit forms or messing around with runtimemarkup fields.

give yourself 30 minutes, copy the processhello to a sandbox site and then see somas thread about building forms via the API. this will warp you to the next level :)

using the InputfieldMarkup you can build your own custom HTML easily that has the look&feel&features of all the other admin fields:

public function ___execute() {
  // set headline + browser title
    $headline = __('Awesome!!');
    $this->wire('processBrowserTitle', $headline);
    $this->headline($headline);

  // create form
    $form = $this->modules->get('InputfieldForm');
    $form->action = './';

    $f = $this->modules->get('InputfieldMarkup');
    $f->value = '<h2>wow!</h2><div>you can do anything now with that knowledge ;)</div>'
    $form->add($f);

  return $form->render();
}

untested ;)

  • Like 6
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...