Skip to content

Instantly share code, notes, and snippets.

@spivurno
Last active October 20, 2021 10:18
Show Gist options
  • Select an option

  • Save spivurno/9707874 to your computer and use it in GitHub Desktop.

Select an option

Save spivurno/9707874 to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms Populate With Entries
<?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 <david@gravitywiz.com>
* @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
)
) );
@yellowstonebum
Copy link
Copy Markdown

yellowstonebum commented Sep 15, 2018

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?

@Captaindrew80
Copy link
Copy Markdown

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?

Copy link
Copy Markdown

ghost commented Nov 21, 2018

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!

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