#1 July 13, 2018 3:49pm

matthisamoto
Member
From: Milwaukee, WI
Registered: February 27, 2013
Posts: 5

Module Form Hook on Create only

I've been tasked with sending a notification email once a new item is added through a Module.

I used the Publish hook, before realizing that it will run the thing any time it's published, so each subsequent update, which we don't want.

I'm thinking I just need to check for existence of the record before, but that brings me to a question about how the records work: when you go to add a new item in a Module, does it create the record in that moment, then after filling out the form, it uses an Update action rather than Create? And even still, is there any hook I can use only for Create, and not for Update?

Offline

#2 July 13, 2018 3:53pm

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Module Form Hook on Create only

It does not create a record upon hitting the form, but it does create it prior to the publish hook being run. If you're only interested in sending an email on the initial creation, I would check for the presence of an "update" in the audit trail. Something like this in your publish hook:

$created = !SQL::exists("SELECT * FROM bigtree_audit_trail WHERE `table` = ? AND `entry` = ? AND `type` = 'updated'", $table, $id);

Then you could just send the email if $created.

Offline

#3 July 13, 2018 3:54pm

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Module Form Hook on Create only

I just realized you might not be running > 4.2.21 or whenever the SQL class was introduced, but you could do the same thing using sqlrows(sqlquery( to check the number of rows returned from a query.

Offline

#4 July 13, 2018 3:54pm

matthisamoto
Member
From: Milwaukee, WI
Registered: February 27, 2013
Posts: 5

Re: Module Form Hook on Create only

I actually did end up upgrading them to 4.2.23 so I should be good there. I'll try something like that, thanks a ton!

Offline

Board footer

Powered by FluxBB

The Discussion Forum is not available on displays of this size.