-
-
Save spivurno/70ab25d52955829465e5c6136e8e127d to your computer and use it in GitHub Desktop.
<?php | |
class GW_Limit_Populated_Choices { | |
public function __construct( $args = array() ) { | |
// set our default arguments, parse against the provided arguments, and store for use throughout the class | |
$this->_args = wp_parse_args( $args, array( | |
'form_id' => false, | |
'field_id' => false, | |
'choice_limit' => false, | |
) ); | |
// do version check in the init to make sure if GF is going to be loaded, it is already loaded | |
add_action( 'init', array( $this, 'init' ) ); | |
} | |
public function init() { | |
$this->set_limit_on_populated_choices(); | |
} | |
function set_limit_on_populated_choices() { | |
if( is_callable( 'gp_limit_choices' ) ) { | |
add_filter( "gform_pre_process_{$this->_args['form_id']}", array( $this, 'enable_limits' ) ); | |
add_filter( "gppa_input_choices_{$this->_args['form_id']}_{$this->_args['field_id']}", array( $this, 'apply_limits'), 10, 2 ); | |
} | |
} | |
public function enable_limits( $form ) { | |
foreach( $form['fields'] as $field ) { | |
if( $field->id == $this->_args['field_id'] ) { | |
$field->{gp_limit_choices()->key( 'enableLimits' )} = true; | |
} | |
} | |
return $form; | |
} | |
public function apply_limits( $choices, $field ) { | |
foreach( $choices as &$choice ) { | |
$choice['limit'] = $this->_args['choice_limit']; | |
} | |
$choices = gp_limit_choices()->apply_choice_limits( $choices, $field, GFAPI::get_form( $field->formId ) ); | |
return $choices; | |
} | |
} | |
# Configuration | |
new GW_Limit_Populated_Choices( array( | |
'form_id' => 123, | |
'field_id' => 4, | |
'choice_limit' => 1 | |
) ); |
@rochekaid Drop us a line via support. We'll be happy to provide an example. 🙂
Just did...Thanks!
Does this snippet add the Limit Choices GUI interface for field_id 4 on form_id 123 even when that field is using GF Populate Anything? Or is the limit set by the number in the $choice['limit'] = 1;
line?
@Wordna1 It does not add the GUI. You would set the desired limit on the line you've indicated.
@Wordna1 It does not add the GUI. You would set the desired limit on the line you've indicated.
Thanks for the clarification - I really appreciate it!
A formula If I want a form for gravity, if the number obtained is less than 365 days, it will show the same number, and if it was more than 365 days, it will show only 365 days ??
An updated version of this snippet can be found here 👉 https://github.com/gravitywiz/snippet-library/blob/master/gp-limit-choices/gplc-gppa-integration.php
which fields should I make changes to? I'm starting and I had some doubts.
This is a great snippet and works well. I am wondering...is there is a way to add a specific limit if the choice contains a certain value?