Last active
March 31, 2021 06:44
-
-
Save landbryo/f4b699c2e68f6cf0c30be9f7332e0d38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'gform_form_post_get_meta_7', 'add_form_fields' ); | |
function add_form_fields( $form ) { | |
// Don't make the parent form messy | |
if ( ! GFCommon::is_form_editor() ) { | |
// form we are adding fields to | |
$parent_id = $form['id']; | |
// template form ID | |
$childId = 5; | |
// we need a unique prefix to prevent field ID conflicts | |
$prefix = '891794134'; | |
// get template form using Gravity Forms API | |
$template = GFAPI::get_form( $childId ); | |
if ( $template ) { | |
foreach ( $template['fields'] as $field ) { | |
// Create unique field id using prefix and previous field ID | |
$field_id = $prefix . $field['id']; | |
// Map field inputs id to prefix | |
$field_inputs = $field['inputs']; | |
if ( ! empty ( $field_inputs ) ) { | |
foreach ( $field_inputs as $key => $input ) { | |
$inputId = explode( '.', $input['id'], 2 ); | |
$field_inputs[ $key ]['id'] = $field_id . '.' . $inputId[1]; | |
} | |
} | |
// Map conditional logic fieldId to prefix | |
$field_conditions = $field['conditionalLogic']; | |
if ( ! empty ( $field['conditionalLogic'] ) ) { | |
foreach ( $field['conditionalLogic']['rules'] as $key => $rule ) { | |
$newId = $prefix . $rule['fieldId']; | |
$field_conditions['rules'][ $key ]['fieldId'] = $newId; | |
} | |
} | |
// Map field parameters | |
$field_names[] = GF_Fields::create( array( | |
'label' => $field['label'], | |
'adminLabel' => $field['adminLabel'], | |
'type' => $field['type'], | |
'id' => $field_id, | |
'formId' => $parent_id, | |
'pageNumber' => $field['pageNumber'], | |
'dbParam' => $field['dbParam'], | |
'nameFormat' => $field['nameFormat'], | |
'inputs' => $field_inputs, | |
'choices' => $field['choices'], | |
'isRequired' => $field['isRequired'], | |
'visibility' => $field['visibility'], | |
'defaultValue' => $field['defaultValue'], | |
'dateFormat' => $field['dateFormat'], | |
'dateType' => $field['dateType'], | |
'conditionalLogic' => $field_conditions, | |
'errorMessage' => $field['errorMessage'], | |
'calendarIconType' => $field['calendarIconType'], | |
'phoneFormat' => $field['phoneFormat'], | |
'size' => $field['size'], | |
'inputMask' => $field['inputMask'], | |
'inputMaskValue' => $field['inputMaskValue'], | |
'inputMaskIsCustom' => $field['inputMaskIsCustom'], | |
'cssClass' => $field['cssClass'], | |
'allowsPrepopulate' => $field['allowsPrepopulate'], | |
'oldId' => $field['id'] | |
) ); | |
} | |
// Add fields to form | |
foreach ( $field_names as $newField ) { | |
$form['fields'][] = $newField; | |
} | |
} | |
} | |
return $form; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment