Support

Account

Home Forums ACF PRO Unique, pre-populated repeater fields

Solved

Unique, pre-populated repeater fields

  • I’m looking to generate a GUID-like value (examples in code below simplified!) as a default for each row added to a repeater. The user can override this value, but I’d like it to be pre-populated so they can simply click ‘Update’ and it’s valid.

    I tried setting a default value using the code below. It sets the same value for each row in the repeater, which is obviously no good for a GUID:

    function my_acf_load_field($field) {
    
    	$field['default_value'] = uniqid();
    
    	return $field;
    }
    add_filter('acf/load_field/name=field_key', 'my_acf_load_field');
    

    I’ve also tried the same approach with using load value. This is even less helpful as the first row is populated, and subsequent rows have no value at all:

    function my_acf_load_value($value) {
    
    	return uniqid();
    }
    add_filter('acf/load_value/name=field_key', 'my_acf_load_value');
    

    I’ve read through many topics on here, and a good amount of documentation. What might I be missing here?

    Thanks!

  • In order to pre-populate a repeater and have unique values for each row you need to populate the entire repeater in one go.

    here’s an example:

    
    add_filter('acf/load_value/key=field_123456', 'pre_pop_repeater', 20, 3);
    /* in the above the field_123456 is the field key of your repeater */
    
    function pre_pop_repeater($value, $post_id, $field) {
      if ($value != NULL) {
        // if the acf value is not null then
        // repeater was previously saved
        // and we should not override
        return $value;
      }
      // return an array
      $value = array(
        // each element of the array represents a row
        array(
          // this is the first row
          // set value for every field in the row
          // the values are field_key => value pairs
          'field_678' => 'value for this field',
          'field_789' => 'value for this field'
          // etc...
        ),
        array(
          // this is the second row
          // etc....
        ),
      );
      return $value;
    }
    
  • Hi John,

    Thanks for the info.

    I may have phrased my question ambiguously, or perhaps not understood your response, but I’m not looking to generate rows in a repeater with values. I’m looking to have one of the values of a repeater pre-populated with a value generated in the back-end, which I can do with ‘load_value’ for the first row, but I’m looking to have all subsequent rows, no matter the number the user chooses, each have a unique value generated and pre-populated with each new row that may be added.

    Looking through the available hooks, I’m thinking now that this may lie more in the JavaScript domain, as there may not be any way to hook into each new row when a user adds it, as that’s probably not even a back-end process.

    Any further guidance on this would be greatly appreciated 🙂

  • You cannot generate different values for defaults, or any other value/setting of a sub field. The code for loading the sub field is run just once, each row will have the same value and this value is determined by what ACF puts in a hidden row in the repeater that it uses to copy it when a new row is created. There isn’t a way to do what you want for each row, unless you maybe use some custom JavaScript. https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/, in fact, I think this is the only way to achieve what you’re looking for.

  • Alright, thanks for confirming this. I may get into some custom JavaScript at some point, but this particular field is mostly read-only, and for now can be set when saving the post.

    For future readers, I ended up making the field read-only when set to null, and then hooking into update_value to generate unique values there. I set the placeholder text to “Automatically generated on save”. Not the most elegant solution, but it works!

    function my_acf_generate_file_url($value, $post_id, $field) {
    
    	if ($value == NULL) {
            
            // generate GUID unique to each row of the repeater and it's value
    		$data = openssl_random_pseudo_bytes(16);
    		$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
    		$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
    		$value = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    	}
    
    	return $value;
    }
    add_filter('acf/update_value/key=field_xxxxxxxx', 'my_acf_generate_file_url', 10, 3);
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Unique, pre-populated repeater fields’ is closed to new replies.