Support

Account

Home Forums General Issues Populate Gravity form fields with ACF subfield values based on dropdown select

Solving

Populate Gravity form fields with ACF subfield values based on dropdown select

  • Field:
    – course info (repeater)
    Subfields:
    – startdate (text)
    – enddate (text)
    – location (text)

    I succesfully managed to add the acf subfield ‘startdate’ values to a Gravity forms dropdown field (see code below).

    I want the enddate value and location value to correspond with the startdate value. So if the first startdate value is selected from the Gravity forms dropdown, I want the first enddate value and first location value to appear in two seperate Gravity form text fields. If the second startdate is selected, I want the second enddate value and second location value to appear in those extra Gravity form text fields. And so on…

    In the last part of the code below, I tried to retrieve on submission of the Gravity form.

    Can anyone help me out?

    
    // Get course post ID from slug
    add_action('init','register_course_parameter');
    function register_course_parameter() { 
        global $wp; 
        $wp->add_query_var('course'); 
    }
    
    // populate dropdown in Gravity form - reapeater course_info - subfield startdate
    add_filter( 'gform_pre_render_1', 'populate_dropdown_course_info' );
    add_filter( 'gform_pre_validation_1', 'populate_dropdown_course_info' );
    add_filter( 'gform_admin_pre_render_1', 'populate_dropdown_course_info' );
    add_filter( 'gform_pre_submission_filter_1', 'populate_dropdown_course_info' );
    
    	function populate_dropdown_course_info( $form ) {
    		if ( $form['id'] != 1 ) {
    			return $form;
    		}
    
        $acf_repeater = get_field( 'course_info' , get_query_var('training') );
    
        // declare the array
        $dropdownitems = array();
    
        foreach ($acf_repeater as $acf_repeater_subfield) {
          $dropdownitems[] = array(
            'value' => $acf_repeater_subfield['startdate'],
            'text' => $acf_repeater_subfield['startdate']
          );
        }
    
        //Add repeater values as dropdown options
        foreach ( $form['fields'] as &$field ) {
          if ( $field->id == 34 ) {
            $field->choices = $dropdownitems;
          }
        }
    	
        return $form;
        wp_reset_postdata();
      }
    
    // With the code below I hoped that the location value generated in Gravity form field 40 on submission, would automatically change based on the selected startdate in the dropdown. This obviously wasn't the case. But how to let them correspond?
    
    add_action( 'gform_pre_submission_1', function ( $form ) {
    $courseid = get_query_var('course');
    	if( have_rows( 'course_info' , $courseid ):
    			while ( have_rows( 'course_info' , $courseid ) : the_row();
    				$sub_value = get_sub_field('location');
    	$_POST['input_40'] = $sub_value;
    	endwhile;
    else :
    endif;
    } );
    
  • Your second code will always loop though all the rows and set the $_POST value each time, always ending with the value set to the final row of the repeater. You need to somehow match them and that will only be possible if the start date value is unique.

    
    while (have_rows('course_info', $courseid) {
      the_row();
      if (get_sub_field('startdate') == $submitted_start_date) {
        $_POST['input_40'] = get_sub_field('location');
      }
    } 
    
  • Thanks for your input. I also read the documentation on https://www.advancedcustomfields.com/resources/repeater/ and try to match the subfield values with the code below, but still doesn’t work out.

    add_action( 'gform_pre_submission_1', 'gf_get_corresponding_course_enddate' );
    function gf_get_corresponding_course_enddate( $form ) {
    	$post_id = get_query_var('course'); //post id from url parameter
    	$startdate = rgpost( 'input_34' ); //value from Gravity forms field with id=34
    	$rows = get_field('course_info'); //repeater field is called course_info
    		if( $rows ) {
       		$first_row = $rows[0];
        		$first_row_title = $first_row['startdate']; // repeater subfield is called startdate
    		$second_row = $rows[1];
    		$second_row_title = $second_row['startdate'];
    		}
        	if ($first_row_title == $startdate) { // if gravity forms field input value equals startdate subfield value of the first row
    		$_POST['input_40'] = get_post_meta($post_id, 'stap_0_location', true); // add location subfield value of the first row to the gravity forms text field with id=40
    	} else if ($second_row_title == $startdate) {
    		$_POST['input_40'] = get_post_meta($post_id, 'stap_1_location', true);
    // add location subfield value of the second row to the gravity forms text field with id=40
    	} else {
    		$_POST['input_40'] = 'location not known';
    	}
    }

    Unfortunately every time the result is ‘location not known’, even if the Gravity form input value equals the startdate subfield value…

  • Okay, I think I managed it this way:

    add_action( 'gform_pre_submission_1', 'gf_override_field_with_choice_label' );
    function gf_override_field_with_choice_label( $form ) {
    	$post_id = get_query_var('course');
    	$startdate = rgpost( 'input_38' );
    	$first_row_value = get_post_meta($post_id, 'course_info_0_startdate', true);
    	$second_row_value = get_post_meta($post_id, 'course_info_1_startdate', true);
    	if ($startdate == $first_row_value) {
    			$_POST['input_40'] = get_post_meta($post_id, 'course_info_0_location', true);
    		} else if ($startdate == $second_row_value {
    			$_POST['input_40'] = get_post_meta($post_id, 'course_info_1_location', true);
    		} else {
    			$_POST['input_42'] = 'no value';
    		}
    }

    Don’t know whether there would be an easier/cleaner way? Or is this the right way?

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.