heldercervantes Posted July 19, 2017 Share Posted July 19, 2017 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. 1 Link to comment Share on other sites More sharing options...
rick Posted July 19, 2017 Share Posted July 19, 2017 Try this from ryan regarding tracking changes and notifications. 2 Link to comment Share on other sites More sharing options...
Robin S Posted July 19, 2017 Share Posted July 19, 2017 Also see this post: 2 Link to comment Share on other sites More sharing options...
bernhard Posted July 20, 2017 Share Posted July 20, 2017 15 hours ago, heldercervantes said: Hi guys, I almost feel like a grown up, now that I'm messing with creating modules. take care, it's addictive 3 Link to comment Share on other sites More sharing options...
heldercervantes Posted July 20, 2017 Author Share Posted July 20, 2017 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: Hook before saveReady, I start by looking at $session->controlArray and set it as a new array if unset; Push to that array as I compare the various thingies; 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 More sharing options...
heldercervantes Posted July 20, 2017 Author Share Posted July 20, 2017 48 minutes ago, heldercervantes said: But my controlArray seems to be scrapped after each hook. Of course. Should be using $this->session, instead of just $session >:[ Link to comment Share on other sites More sharing options...
heldercervantes Posted July 21, 2017 Author Share Posted July 21, 2017 On 7/20/2017 at 11:22 AM, bernhard said: take care, it's addictive Damn were you right. I'm hooked. Refactoring a bunch of stuff with a grin from ear to ear. 2 Link to comment Share on other sites More sharing options...
netcarver Posted July 21, 2017 Share Posted July 21, 2017 @heldercervantes Sounds like one of my older modules. Welcome to the world of module authorship! 2 Link to comment Share on other sites More sharing options...
heldercervantes Posted July 21, 2017 Author Share Posted July 21, 2017 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 More sharing options...
netcarver Posted July 21, 2017 Share Posted July 21, 2017 @heldercervantes Sounds good. I had a similar need for custom email templates and built a module for that too (along with a helper module.) You might find it useful in your application, too. Both modules are fairly old now - YMMV. Best wishes! 1 Link to comment Share on other sites More sharing options...
bernhard Posted July 21, 2017 Share Posted July 21, 2017 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 6 Link to comment Share on other sites More sharing options...
heldercervantes Posted July 24, 2017 Author Share Posted July 24, 2017 Thanks @bernhard, that tip will come in handy a bit down the road when I'll have to build reporting tools for this project. Great stuff! 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now