GravityBoard: How to trigger feeds after a card changes
Saving a card on a board updates its entry without running the form’s add-on feeds, such as Zapier or Slack. To send the card’s new values to those services, add one of the code snippets below. GravityBoard runs a separate action for each kind of change, so a snippet can run the feeds after any edit or only when a card moves into one lane.
Each time a snippet runs, the feeds run again. Depending on the add-on, running a feed again can create a duplicate record. Notifications don’t need code: How GravityBoard syncs to Gravity Forms shows which ones a card change sends.
Run every feed after a card is edited #
This snippet runs each active feed whose conditions match the entry, every time someone saves a card in the card editor with more than its lane changed. Changing the assignees there counts as an edit. The snippet also runs anything else hooked to gform_entry_post_save.
/**
* Runs all of a form's add-on feeds again after a card is edited on a board.
*
* @param array $entry The card's entry, after the edit.
* @param array $feed The board.
* @param string[] $changes What the edit changed.
*/
add_action( 'gk/gravityboard/card/edited', function ( $entry, $feed, $changes ) {
// Change 100 to your form's ID.
if ( 100 !== (int) $entry['form_id'] ) {
return;
}
// Load the form and entry again to get the values that were just saved.
$form = GFAPI::get_form( $entry['form_id'] );
$entry = GFAPI::get_entry( $entry['id'] );
// Gravity Forms runs a feed once per entry unless told otherwise.
add_filter( 'gform_allow_feed_reprocessing', '__return_true', 1294873 );
// Run the feeds right away, during this request.
add_filter( 'gform_is_feed_asynchronous', '__return_false', 1294873 );
gf_apply_filters( array( 'gform_entry_post_save', $form['id'] ), $entry, $form );
remove_filter( 'gform_allow_feed_reprocessing', '__return_true', 1294873 );
remove_filter( 'gform_is_feed_asynchronous', '__return_false', 1294873 );
}, 10, 3 );Note: To add this code to your site, follow Adding custom PHP code snippets to your website.
This snippet doesn’t run when a card moves to another lane, whether someone drags it or saves the card editor with only the lane changed. To run the feeds on a move, use the next snippet.
Run every feed when a card moves into one lane #
This snippet runs the feeds when a card moves into the lane you name, whether someone drags the card or changes its lane in the card editor. Moves into other lanes are skipped.
Change 3 to the ID of the field your lanes come from, and Approved to the lane’s name. If the field uses separate choice values, use the lane’s value.
/**
* Runs all of a form's add-on feeds when a card moves into one lane.
*
* @param array $entry The card's entry, after the move.
* @param array $feed The board.
* @param string|int|null $source_lane_id The lane the card left.
* @param int $target_lane_id The lane the card moved into.
*/
add_action( 'gk/gravityboard/card/changed-lane', function ( $entry, $feed, $source_lane_id, $target_lane_id ) {
// Change 100 to your form's ID.
if ( 100 !== (int) $entry['form_id'] ) {
return;
}
// Change 3 to the ID of the field your lanes come from, and Approved to the lane's value.
if ( 'Approved' !== rgar( $entry, '3' ) ) {
return;
}
// Load the form and entry again to get the values that were just saved.
$form = GFAPI::get_form( $entry['form_id'] );
$entry = GFAPI::get_entry( $entry['id'] );
// Gravity Forms runs a feed once per entry unless told otherwise.
add_filter( 'gform_allow_feed_reprocessing', '__return_true', 1294873 );
// Run the feeds right away, during this request.
add_filter( 'gform_is_feed_asynchronous', '__return_false', 1294873 );
gf_apply_filters( array( 'gform_entry_post_save', $form['id'] ), $entry, $form );
remove_filter( 'gform_allow_feed_reprocessing', '__return_true', 1294873 );
remove_filter( 'gform_is_feed_asynchronous', '__return_false', 1294873 );
}, 10, 4 );Note: To add this code to your site, follow Adding custom PHP code snippets to your website.
Run the feeds of one add-on when a card moves into one lane #
This snippet runs the feeds of one add-on and leaves the others alone. It’s written for Zapier. For another add-on, replace GF_Zapier in both places with the add-on’s class name, which Run one add-on’s feeds lists for common add-ons.
/**
* Runs one add-on's feeds when a card moves into one lane.
*
* @param array $entry The card's entry, after the move.
* @param array $feed The board.
* @param string|int|null $source_lane_id The lane the card left.
* @param int $target_lane_id The lane the card moved into.
*/
add_action( 'gk/gravityboard/card/changed-lane', function ( $entry, $feed, $source_lane_id, $target_lane_id ) {
// Change 100 to your form's ID.
if ( 100 !== (int) $entry['form_id'] ) {
return;
}
// Change 3 to the ID of the field your lanes come from, and Approved to the lane's value.
if ( 'Approved' !== rgar( $entry, '3' ) ) {
return;
}
// Change GF_Zapier in the next line and in the get_instance() line below.
if ( ! class_exists( 'GF_Zapier' ) ) {
return;
}
// Load the form and entry again to get the values that were just saved.
$form = GFAPI::get_form( $entry['form_id'] );
$entry = GFAPI::get_entry( $entry['id'] );
// Gravity Forms runs a feed once per entry unless told otherwise.
add_filter( 'gform_allow_feed_reprocessing', '__return_true', 1294873 );
// Run the feeds right away, during this request.
add_filter( 'gform_is_feed_asynchronous', '__return_false', 1294873 );
GF_Zapier::get_instance()->maybe_process_feed( $entry, $form );
remove_filter( 'gform_allow_feed_reprocessing', '__return_true', 1294873 );
remove_filter( 'gform_is_feed_asynchronous', '__return_false', 1294873 );
}, 10, 4 );Note: To add this code to your site, follow Adding custom PHP code snippets to your website.
remove_filter() removes a callback only at the priority it’s given. The snippets use 1294873, a priority other code is unlikely to use, so removing their filters afterward leaves other code’s filters on the same hooks in place.
Other changes you can run feeds on #
GravityBoard runs an action for each of these changes as well. To run the feeds on one of them, change the first snippet in three places:
- Replace
gk/gravityboard/card/editedwith the action’s name. - Replace
$entry, $feed, $changeswith the action’s arguments from the table, in the same order. - Change the last number, the
3in}, 10, 3 );, to the number of arguments, which is4for some actions.
| Action | Runs when | Arguments |
|---|---|---|
gk/ | A card is added on the board | $entry, $feed, $request |
gk/ | A card moves up or down in its lane | $entry, $feed, $source_lane_id, $ordered_ids |
gk/ | A card’s assignees change | $entry_id, $assignees, $feed, $form |
gk/ | A file is added to a card or removed from it | $attachments, $entry, $feed |
gk/ | A card’s checklists are saved | $checklists, $entry, $feed |
gk/gravityboard/assignees/updated passes the entry’s ID instead of the entry. Add $entry = GFAPI::get_entry( $entry_id ); as the function’s first line, before the form check.
Adding a file to a card doesn’t run gk/gravityboard/card/edited. The full list of actions is in GravityBoard hooks and REST API.
