Created
August 25, 2017 14:17
-
-
Save jag1989/722b11c3e25f1421c0d76df4c7894ec6 to your computer and use it in GitHub Desktop.
GForm isSelected Issue
This file contains 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
/** | |
* Returns the field inner markup. | |
* | |
* @since Unknown | |
* @access public | |
* | |
* @uses GF_Field_MultiSelect::is_entry_detail() | |
* @uses GF_Field_MultiSelect::is_form_editor() | |
* @uses GF_Field_MultiSelect::get_conditional_logic_event() | |
* @uses GF_Field_MultiSelect::get_tabindex() | |
* | |
* @param array $form The Form Object currently being processed. | |
* @param string|array $value The field value. From default/dynamic population, $_POST, or a resumed incomplete submission. | |
* @param null|array $entry Null or the Entry Object currently being edited. | |
* | |
* @return string The field input HTML markup. | |
*/ | |
public function get_field_input( $form, $value = '', $entry = null ) { | |
$form_id = absint( $form['id'] ); | |
$is_entry_detail = $this->is_entry_detail(); | |
$is_form_editor = $this->is_form_editor(); | |
$id = $this->id; | |
$field_id = $is_entry_detail || $is_form_editor || $form_id == 0 ? "input_$id" : 'input_' . $form_id . "_$id"; | |
$logic_event = $this->get_conditional_logic_event( 'keyup' ); | |
$size = $this->size; | |
$class_suffix = $is_entry_detail ? '_admin' : ''; | |
$class = $size . $class_suffix; | |
$css_class = trim( esc_attr( $class ) . ' gfield_select' ); | |
$tabindex = $this->get_tabindex(); | |
$disabled_text = $is_form_editor ? 'disabled="disabled"' : ''; | |
/** | |
* Allow the placeholder used by the enhanced ui to be overridden | |
* | |
* @since 1.9.14 Third parameter containing the field ID was added. | |
* @since Unknown | |
* | |
* @param string $placeholder The placeholder text. | |
* @param integer $form_id The ID of the current form. | |
*/ | |
$placeholder = gf_apply_filters( array( | |
'gform_multiselect_placeholder', | |
$form_id, | |
$this->id | |
), __( 'Click to select...', 'gravityforms' ), $form_id, $this ); | |
$placeholder = $this->enableEnhancedUI ? "data-placeholder='" . esc_attr( $placeholder ) . "'" : ''; | |
$size = $this->multiSelectSize; | |
if ( empty( $size ) ) { | |
$size = 7; | |
} | |
// Get a collection of all posts in the development post type | |
$developments = new WP_Query([ | |
'post_type' => 'developments', | |
'posts_per_page' => -1 | |
]); | |
$choices = []; | |
$newBuilds = []; | |
$preOwned = []; | |
// loop through the list and assign the development to either | |
// a new build collection | |
// or | |
// a previously owned collection | |
if ( $developments->have_posts() ) : | |
while ( $developments->have_posts() ) : $developments->the_post(); | |
$development = new Development(); | |
$text = rd_get_gfrom_development_value( $development ); | |
$option = [ | |
'value' => $text, | |
'text' => $text, | |
'isSelected' => ( get_the_ID() == $_GET['ref'] ) ? true : false | |
]; | |
if( $development->isNewBuild == true ) : | |
$newBuilds[] = $option; | |
else : | |
$preOwned[] = $option; | |
endif; | |
endwhile; | |
endif; | |
wp_reset_postdata(); | |
// add a option group heading | |
// and all the options if there are any developments in the collection | |
if( count($newBuilds) > 0 ) : | |
$choices[] = [ | |
'value' => 'optgroup', | |
'text' => 'Developments', | |
'isSelected' => false, | |
]; | |
$choices = array_merge($choices, $newBuilds); | |
endif; | |
// add a option group heading | |
// and all the options if there are any developments in the collection | |
if( count($preOwned) > 0 ) : | |
$choices[] = [ | |
'value' => 'optgroup', | |
'text' => 'Previously Owned', | |
'isSelected' => false, | |
]; | |
$choices = array_merge($choices, $preOwned); | |
endif; | |
// set choices into the gravity form var | |
$this->choices = $choices; | |
return sprintf( "<div class='ginput_container ginput_container_multiselect ginput_container_development_multiselect'><select multiple='multiple' {$placeholder} size='{$size}' name='input_%d[]' id='%s' {$logic_event} class='%s' $tabindex %s>%s</select></div>", $id, esc_attr( $field_id ), $css_class, $disabled_text, $this->get_choices( $value ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment