Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vfontjr/75ab98c12570455d357c85d94adf3e33 to your computer and use it in GitHub Desktop.
Save vfontjr/75ab98c12570455d357c85d94adf3e33 to your computer and use it in GitHub Desktop.
<?php
/* use this function for populating entries where
* dynamic lookup entry ids are required
*/
function masterminds_get_dynamic_field_entry_id_by_value( $field_id, $value ) {
global $wpdb;
$frm_item_metas_table = $wpdb->prefix . 'frm_item_metas';
$sql = "SELECT item_id FROM `{$frm_item_metas_table}` WHERE field_id = '{$field_id}' AND meta_value = '{$value}'";
return $wpdb->get_var($sql);
}
<?php
add_action('frm_after_create_entry', 'pre_pop_forms', 30, 2);
function pre_pop_forms($entry_id, $form_id){
if ( $form_id != 52 ) {
return;
}
/* bullet proof FrmEntryMeta::add_entry_meta() to avoid duplicate entries
* of the client entry id field on form 52 if the record is edited and you
* are also using the frm_after_update_entry filter
* vmf - 10/23/2023
*/
/* test for current value */
$current_value = FrmEntryMeta::get_entry_meta_by_field( $entry_id, $field_id );
/* if no current value, add the field */
if( is_null( $current_value ) ) {
/* if successful, function returns entry id of meta data record */
$added = FrmEntryMeta::add_entry_meta( $entry_id, $field_id, null, $entry_id );
} else {
FrmEntryMeta::update_entry_meta( $entry_id, $field_id, null, $entry_id );
}
/* add entry to form 2 */
/* make sure user id is populated */
if ( !isset( $_POST['frm_user_id'] ) ){
return;
}
/* get form 2 id from key for transportable code
* see https://formidable-masterminds.com/writing-transportable-code-keys-vs-ids/
*/
$form2 = FrmForm::get_id_by_key('form2_key');
/* get field id by keys for transportable code */
$dynamic_lu_field = FrmField::get_id_by_key('status_lu_field');
$form2_entry_id_field = FrmField::get_id_by_key('form2_entry_id');
$form2_status_id_field = FrmField::get_id_by_key('form2_status_id');
/* get record id for "Pre-pitch" status */
$status_id = masterminds_get_dynamic_field_entry_id_by_value( $dynamic_lu_field, "Pre-pitch" );
//create entry
FrmEntry::create( array(
'form_id' => $form2,
'frm_user_id' => $_POST['frm_user_id'],
'item_meta' => array(
$form2_entry_id_field => $entry_id,
$form2_status_id_field => $status_id,
),
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment