Support

Account

Home Forums Backend Issues (wp-admin) Dynamically populating repeater fields with default values?

Solved

Dynamically populating repeater fields with default values?

  • I have a scenario where I need a bunch of repeater fields with two sub fields each: one for a name (which should not be editable) and another for a value score.

    I created an options page with the list of names that each of the repeater fields should have, and used prepare_field to set the number of fields to the length of the list — that all works fine. Here’s that code:

    add_filter('acf/prepare_field/key=field_582e290a22e62', 'preparefield');
    
    function preparefield($field) {
      // see all the field settings before changing
      //echo '<pre>'; print_r($field); echo '</pre>';
    
      $num = get_field('medication_types','option');
      $num = count($num);
    
      $field['max'] = $num;
      $field['min'] = $num;
    
      return $field;
    }
    
    add_filter('acf/prepare_field/name=medication_types', 'medicationTypes');

    My problem is I can’t insert the options page list of names into the repeater field names. I was able to use load_value to echo out all the repeater field values, but I can’t quite figure out how to actually set them.

    Has anyone worked on something similar?

  • Just as an example, say this is the list of names in the options page:

    Hospital
    Pharmacy
    Surgery Center

    etc…

    What I need to do is set those as default values in the repeater name subfields on every new post that’s created:

    Row 1
    -Name: Hospital
    -Value: (User will set)

    Row 2
    -Name: Pharmacy
    -Value: (User will set)

    Row 3
    -Name: Surgery Center
    -Value: (User will set)

  • Hi @folosophy

    You should be able to do it like this:

    function my_acf_set_repeater( $value, $post_id, $field ){
        
        $value = array();
        
        // this one should consists array of the names
        $settings_values = get_field('medication_types','option');
        
        foreach( $settings_values as $settings_value ){
            $value []= array('field_1234567890abc' => $settings_value['name_field'], 'field_abc1234567890' => '');
        }
    
        return $value;
    }
    
    // acf/load_value/name={$field_name} - filter for a specific value load based on it's field name
    add_filter('acf/load_value/key=field_582e290a22e62', 'my_acf_set_repeater', 10, 3);

    Where “field_1234567890abc” is the name field key, “field_abc1234567890” is the value field key, and “name_field” is the name field in the “medication_types” repeater.

    I hope this helps 🙂

  • Hey James, thanks for the help!

    I had to make a tweak to your code to get it to work. I added a counter to modify the $value array instead of building it from scratch (otherwise it replaces any value the user sets with ‘ ‘).

    function my_acf_set_repeater( $value, $post_id, $field ){
    
        // echo '<pre>'; print_r($value); echo '</pre>';
    
        // this one should consists array of the names
        $settings_values = get_field('medication_types','option');
    
        $i = 0;
    
        foreach( $settings_values as $settings_value ){
    
          $value[$i]['field_582e293d22e63'] = $settings_value['type'];
    
          $i++;
    
        }
    
        //echo '<pre>'; print_r($value); echo '</pre>';
    
        return $value;
    
    }
    
    add_filter('acf/load_value/name=medication', 'my_acf_set_repeater', 10, 3);

    I realized, though, that using indexed repeater fields is not a smart way to do this—the client could mess it up by reordering the repeater fields.

    I’ll probably redo it as a normal named field repeater, then set the field names as a prefixed version of the medication types fields… that way the client can’t reorder the fields, and they can change the medication type names without wrecking the fields.

    Cheers!

  • Thank you @folosophy for posting the code (and the fix w/ the counter). I’m wondering if you would be able to help with a solution for pre-populating the repeater with post titles from a custom post type instead of values from another ACF repeater?

    I have a Custom Post Type “Teams” and each post’s title is a team name. I would like to pre-populate an ACF Repeater Field with each team name. Any suggestions?

  • Sorry, I know this is a very later post but very badly need it, I have an options page using ACF

    This is the structure (OPTIONS PAGE)

    - specifications - group
    
    -- specifications_repeater - repeater
    
    --- specification_name - text
    
    so on the options page I added 3 fields and save
    Size
    Range
    Colour

    Now I have another another custom fields set in pages

    - specifications - repeater
    
    -- specification_item - text (populate this fields based on the "specification_name" field on the options page)
    
    -- specification_value - text

    What I want and hoping that it populates whatever its in the options page, so it will populate automatically the fields ( Size, Colour & Range )

    I have this code below but it only populates “Colour” on all its fields, I think its pulling the last data.

    
    function my_acf_load_field($field) {
        $field['default_value'] = array();
        if( have_rows('specifications', 'option') ) {
            while( have_rows('specifications', 'option') ) {
                the_row();
    
                if( have_rows('specifications_repeater', 'option') ) {
                    while( have_rows('specifications_repeater', 'option') ) {
                        the_row();
    
                        $value = get_sub_field('specification_name');
                        $field['default_value'] = $value;
    
                    }
                }
    
            }
        }
        //$field['default_value'] = uniqid();
        return $field;
    }
    add_filter('acf/load_field/key=field_5e86e1166f98a', 'my_acf_load_field');

    also field_5e86e1166f98a is the target field

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

You must be logged in to reply to this topic.