Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Last active May 26, 2024 05:43
Show Gist options
  • Save Crocoblock/f3c6237e3aa5156a7e0caac9d8c26476 to your computer and use it in GitHub Desktop.
Save Crocoblock/f3c6237e3aa5156a7e0caac9d8c26476 to your computer and use it in GitHub Desktop.
JetFormBuilder Call Hook action
<?php
add_action( 'jet-form-builder/custom-action/test-hook', function( $request, $action_handler ) {
//get value of field field1
$value = $request['field1'];
//or using jet_fb_context()->resolve_request()
$value = jet_fb_context()->resolve_request()['field1'];
//return an error if field1 is less than 10
if ( isset( $request['field1'] ) && ( int ) $request['field1'] < 10 ) {
throw new \Jet_Form_Builder\Exceptions\Action_Exception( 'field1 is less than 10');
}
//set field1 value to 20
jet_fb_context()->update_request( 20, 'field1' );
//Update value inside specific row in the repeater
//Where: 0 - first row in th repeater
jet_fb_context()->update_request( 'repeater_name.0.field_name', 'new inner field value' );
// or this way
jet_fb_context()->update_request( array( 'repeater_name', 0, 'field_name' ), 'new inner field value' );
//Update specific field in each row of repeater
foreach ( jet_fb_context()->iterate_inner( 'repeater_name' ) as $context ) {
$context->update_request( 'field_name', 'new inner field value' );
}
//get ID of the post, created by "Insert/Update Post" action
$inserted_post_id = $request['inserted_post_id'];
//check what data comes to hook
//jet_fb_context()->resolve_request() to get request with the changes
//form execution will be interruppted, though you will be able to check what may be the reason of the hook not working as intended
$request_from_context = jet_fb_context()->resolve_request();
var_dump( $request, $request_from_context );exit;
}, 0, 2 );
@WebAtelier47
Copy link

Very practical thank you, would there be a generic hook, i.e. to avoid having to add the name of a hook in the form settings?
THANKS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment