-
-
Save spivurno/9707874 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* Gravity Wiz // Gravity Forms Populate With Entries | |
* | |
* Populate choice-based fields (i.e. drop downs, radios, checkboxes) with data from Gravity Form entries. | |
* | |
* @version 1.0 | |
* @author David Smith <[email protected]> | |
* @license GPL-2.0+ | |
* @link http://gravitywiz.com/... | |
* @copyright 2013 Gravity Wiz | |
*/ | |
class GW_Populate_Entries { | |
public function __construct( $args = array() ) { | |
// make sure we're running the required minimum version of Gravity Forms | |
if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) | |
return; | |
// set our default arguments, parse against the provided arguments, and store for use throughout the class | |
$this->_args = wp_parse_args( $args, array( | |
'target_form_id' => false, | |
'target_field_id' => false, // must be a choice-based field (drop down, radio, checkbox) | |
'source_form_id' => false, | |
'value' => false, // can be a field ID or a 'template' string with merge tag support | |
'label' => false, // can be a field ID or a 'template' string with merge tag support | |
'query_args' => array() | |
) ); | |
$this->_args['query_args'] = wp_parse_args( $this->_args['query_args'], array( | |
'status' => 'active', | |
'order' => 'asc', | |
'order_by' => 'date_created', | |
'nopaging' => true, | |
'offset' => 0, | |
'page_size' => 20, | |
'field_id' => false, | |
'field_value' => '', | |
'field_filters' => array() | |
) ); | |
if( $this->_args['query_args']['field_id'] ) { | |
$this->_args['query_args']['field_filters'] = array( | |
array( | |
'key' => $this->_args['query_args']['field_id'], | |
'value' => $this->_args['query_args']['field_value'] | |
) | |
); | |
} | |
$is_no_paging = $this->_args['query_args']['nopaging'] == true; | |
$this->_args['paging'] = array( | |
'offset' => $is_no_paging ? 0 : $this->_args['query_args']['offset'], | |
'page_size' => $is_no_paging ? 999 : $this->_args['query_args']['page_size'] | |
); | |
$this->_args['sorting'] = array( | |
'key' => $this->_args['query_args']['order_by'], | |
'direction' => $this->_args['query_args']['order'] | |
); | |
// time for hooks | |
add_action( "gform_pre_render_{$this->_args['target_form_id']}", array( $this, 'populate' ) ); | |
add_action( "gform_admin_pre_render_{$this->_args['target_form_id']}", array( $this, 'populate' ) ); | |
add_action( "gform_pre_submission_filter_{$this->_args['target_form_id']}", array( $this, 'populate' ) ); | |
} | |
public function populate( $form ) { | |
foreach( $form['fields'] as &$field ) { | |
if( $field['id'] != $this->_args['target_field_id'] ) | |
continue; | |
$entries = $this->get_entries_for_population( $this->_args['source_form_id'] ); | |
$choices = array(); | |
foreach( $entries as $entry ) { | |
$value = $this->get_entry_value( $entry, $this->_args['value'] ); | |
$label = $this->_args['label'] ? $this->get_entry_value( $entry, $this->_args['label'] ) : $value; | |
$choices[] = array( | |
'text' => $label, | |
'value' => $value | |
); | |
} | |
if( empty( $choices ) ) { | |
$choices[] = array( | |
'text' => __( 'There are no options available currently.' ), | |
'value' => '' | |
); | |
} | |
$field['choices'] = $choices; | |
if( GFFormsModel::get_input_type( $field ) != 'checkbox' ) | |
continue; | |
$field['inputs'] = array(); | |
foreach( $choices as $id => $choice ) { | |
$field['inputs'][] = array( | |
'id' => sprintf( '%d.%d', $field['id'], $id + 1 ), | |
'label' => $choice['text'], | |
'name' => '' | |
); | |
} | |
} | |
return $form; | |
} | |
public function get_entries_for_population( $form_id ) { | |
return GFAPI::get_entries( $form_id, $this->_args['query_args'], $this->_args['sorting'], $this->_args['paging'] ); | |
} | |
public function get_entry_value( $entry, $template ) { | |
$field_id = intval( $template ); | |
if( $field_id ) | |
return rgar( $entry, $field_id ); | |
$form = GFAPI::get_form( $entry['form_id'] ); | |
$template = GFCommon::replace_variables( $template, $form, $entry, false, true, false, 'html' ); | |
return $template; | |
} | |
} | |
# Configuration | |
new GW_Populate_Entries( array( | |
'target_field_id' => 1, // the field to be populated with entry data; must be a choice-based field (drop down, radio, checkbox) | |
'target_form_id' => 449, // the form that contains the field to be populated | |
'source_form_id' => 414, // the form from which entry data should be retrieved | |
'value' => '{entry_id}', // specifies the data that should be populated for the choice's 'value'; can be a field ID (i.e. 12) or a 'template' string with merge tag support (i.e. '{entry_id}') | |
'label' => '{:1.3} {:1.6} ({entry_id})', // same as 'value' but used to populate the label of the choice | |
'query_args' => array( // specifies which entries should be pulled for display | |
'status' => 'trash', | |
'field_id' => '1.3', | |
'field_value' => 'test', | |
'order' => 'asc', | |
'order_by' => '1.3', | |
'nopaging' => true | |
) | |
) ); |
I have a link with "idpost" parameter (www.mywebsite.it?idpost=125)
how can I populate dropdown menu with post ID = idpost?
'value' => '$_GET['idpost']' ??
Hi, thanks a lot for your great script, I am not able to populate differently label and value tho,
Here's my exact situation:
form A (field 1 customer, field 2 email),
form B (field 1 dropdown list with customer and the value should be its email)
'value' => 2, // specifies the data that should be populated for the choice's 'value'; can be a field ID (i.e. 12) or a 'template' string with merge tag support (i.e. '{entry_id}')
'label' => 1, // same as 'value' but used to populate the label of the choice
using this configuration the dropdown shows the default values, the form works if I use the same value and label instead.
Any hint? What am I doing wrong?
Thanks a lot guys
Hello everyone,
Thank you for this code. I am looking for some help extending it a bit. Here is my situation:
Form 1 - An Admin form to control choices for Form 2.
- Brand
- Model
- Type
- Capacity
I have these fields on an equipment registration form (Form 2). I would like to filter the model when brand is selected based on the Form 1 entries because the equipment model is specific to the brand. Then, once the model is selected the type and capacity can be auto populated from the Form 1 entries.
Any help is greatly appreciated!
Firstly great script. I just have one question, can I have multiple 'target_field_id' ?
All the other settings are the same, I just need to repeat this selection
I tried various options but it doesnt work like
'target_field_id' => 152,157,
I'm looking for something like this but for the List Field drop down. I can get it to work for user meta, but not for Gravity Entries:
add_filter( 'gform_column_input_42_1_3', 'business_column', 10, 5 );
function business_column( $input_info, $field, $column, $value, $form_id ) {$items = array(); // Get the custom field values stored in the array
// If editing this lookup where you would like to get your data from
// this example loads through all users of the website
$metas = get_users( array( 'role' => 'company' ) );if (is_array($metas))
{
// in this example we just load the display_name for each user into our drop-down field
foreach($metas as $meta) $items[] = array("value" => $meta->display_name, "text" => $meta->display_name);
}return array( 'type' => 'select', 'choices' => $items );
}
Any thoughts?
How do you change this to alphabetical order? I see :
'order_by' => '1.3
What's that mean? What do I need to change in the config?
Somebody has the solution to auto populate fields based on the selection from the drop down box?
@WesternWebDoc commented one year ago this question, and nobody reply it.
Thanks a lot!
Thank you very much for this piece of code and for everyone's contribution. It works pretty much out of the box!
One thing I'm trying to do is add multiple filters but I'm unsure how to achieve this.
I've already added a filter to only display entries originated from current user ('field_id' => 24, 'field_value' => $current_user->user_login) However id like to add an additional filter (e.g. with status 'active') this is an existing field in the form, so that it only displays "active" entries.
Thanks in advance