Support

Account

Home Forums General Issues Populate a repeater select field depending on other subfield value (no ajax)

Solved

Populate a repeater select field depending on other subfield value (no ajax)

  • I’m asking the question in the name of my developer, who can’t figure out the solution.

    We have a custom post type (let’s call it “Country”) with : 
    – a true false called “active”
    – a Cities flexible content with a text subfield called “cityname”.

    We have another custom post type (“Trip”) with a repeater and 2 subfields :
    1. a COUNTRIES select field (not a relationship field) that is populated dynamically, via PHP. It displays the posts of the CPT “Countries” that have the “active” true/false checked. 
    2. a CITIES select field, also populated dynamically, but this one via an Ajax call, triggered when selecting a Country : it will go in the corresponding country’s post, check for each row of the Cities flexible content and retrieve the values of the subfield “cityname” to populate the cities select field.

    This works, but it has one problem : 
    When opening an existing “Trip” post, we have to trigger the ajax call to populate the “cities” select field. Otherwise, the select field won’t display anything. So that’s what we did, but this ajax call creates some other limitations (no need to go into details). 

    => When opening an existing “Trip” post, we would rather have the Cities select field to be already populated, via PHP instead of Ajax.
    (and if afterwards we add more rows to the repeater, the cities select field of the new rows would be populated via the ajax call that is already set up)

    That seemed relatively straightforward considering that he managed to do it with Ajax, but he can’t figure it out with PHP only. He manages to get the information, but not to insert them in the select fields of the repeater. I wish he could explain himself but he doesn’t speak english.

    Sorry for the long post…

  • Your developer needs to use an acf/prepare_field filter using the field key varient. This filter is called before each instance of the field in the existing repeater.

    $field['name'] will have values that look like this:

    
    acf[field_61f2e6625ac01][row-0][field_61f2e66c5ac02]
    acf[field_61f2e6625ac01][row-1][field_61f2e66c5ac02]
    acf[field_61f2e6625ac01][row-2][field_61f2e66c5ac02]
    etc...
    

    This value needs to be parsed to determine what row of the repeater is being populated. There is also one that looks like this that needs to be ignored

    
    acf[field_61f2e6625ac01][acfcloneindex][field_61f2e66c5ac02]
    

    something like

    
    // this code has not been tested
    preg_match('/\]\[row-([0-9]+)/', $field['name'], $matches);
    if (!empty($matches[1])) {
      $row_index = intval($matches[1];
    }
    

    Using this information the value of the corresponding country field must be retrieved. I am not sure of the best way to do this. There is a chance that trying to read the repeater that is currently in the process of being displayed could cause the display loop to do odd things. I have never tried this. It may be necessary to use the field names in this instance an use get_post_meta() rather than using ACF functions. This can be done by constructing the correct meta_key
    "{$repeater_name}_{$row_index}_{$sub_field_name}"

    Then the choices of the current field can be populated based on the value of “country”

  • Thank you very much, it helped him a lot indeed !

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

You must be logged in to reply to this topic.