Support

Account

Home Forums General Issues pre populated repeater field

Solved

pre populated repeater field

  • Hi guys!

    I have a question about a functionality I would like to achieve and if it’s possible.
    Im using a repeater field for users to enter product contents, works great but I would like to automate some standard items for them so they don’t have to fill them out every time they add a product. this way I create consistency in my content as well, which is important.

    In my attachment I created a simple drawing of what I’d like to achieve:

    • partly pre-populated repeater field
    • existing of two fields: content item & quantity
    • I need to be able to specify an x amount of rows as ‘standard items’ where the user can only edit (read populate) the quantity field, they should not be able to reorder those either.
    • have standard repeater functionality where the user can add more content items when they’re not in my pre-defined list, and if possible, be able to reorder those.

    Would love to hear from you, thanks!
    https://dl.dropboxusercontent.com/u/27447908/scan.pdf

  • Can you pre-populate a repeater field, yes.

    You can use an acf/load_value filter. http://www.advancedcustomfields.com/resources/acfload_value/

    
    function my_acf_load_value($value, $post_id, $field) {
      if ($value !== NULL) {
        // if the value is exactly NULL it means
        // the field has never been updated
        // we don't want to change fields that have already been editied
        return $value;
      }
      // set the new field value
      $value = array(
        // add a nested array for each row
        array(
          // add an array item for each sub field
          'field_name_1' => 'Value for Field 1',
          'field_name_2' => 'Value for Field 2'
        )
      );
      return $value;
    }
    

    The second part of the question, can you prevent the rows of the repeater from being reordered or edited? As far as I know this can’t be done.

  • Thanks @hube2!

    this is sufficient for what I wanted, after seeing it to work, no need to prevent the rows from being edited.

    EDIT, by the way:
    'field_name' => 'Value for Field'
    should be
    'field_key' => 'Value for Field'

  • This doesn’t seem to work for me…I guess i’m just confused at how this works.

    I’m using

     function my_acf_load_value($value, $post_id, $field) {
    
    	if ($value !== NULL) {
    		// if the value is exactly NULL it means
    		// the field has never been updated
    		// we don't want to change fields that have already been editied
    		return $value;
    	}
    
    	// set the new field value
    	$value = array(
    		// add a nested array for each row
    		array(
    			// add an array item for each sub field
    			'item' => 'Platform?',
    		)
    	);
    	return $value;
    }
    add_filter('acf/load_value/name=checklist_for_setup', 'my_acf_load_value', 10, 3); 

    And it creates a row, but there are no values in it.

  • what kind of field is item?

  • Desperately trying to get this to work. I thought I understood it but I am getting no pre-populated fields at all. My repeater field name is “games” with 4 sub-fields. The first sub-field name is “team” and is a text field. I’m trying to pre-populate the team field. Am I missing something?:

      function my_acf_load_value($value, $post_id, $field) {
        if ($value !== NULL) {
          // if the value is exactly NULL it means
          // the field has never been updated
          // we don't want to change fields that have already been editied
          return $value;
        }
        // set the new field value
        $value = array(
          // add a nested array for each row
          array(
            // add an array item for each sub field
            'team' => 'hello world?',
            'team_1' => 'hello world?', // with or without "_1, etc."?
          )
        );   return $value;
      }
      add_filter('acf/load_value/name=games', 'my_acf_load_value', 10, 3);
  • I also had some issues with the above posted solution.

    The problem is:
    if ($value !== NULL) {

    $value is an empty array, and thus not NULL, so I got it working using this:

    function my_acf_load_value($value, $post_id, $field) {
        if (!empty($value)) {
          // if the value is exactly NULL it means
          // the field has never been updated
          // we don't want to change fields that have already been editied
          return $value;
        }
        // set the new field value
        $value = array(
          // add a nested array for each row
          array(
            // add an array item for each sub field (use field key - not name)
            'field_key_here' => 'field 1',
            'field_key_here' => 'field 2'
          )
        );   return $value;
      }
      add_filter('acf/load_value/name=games', 'my_acf_load_value', 10, 3);
  • The problem with using empty() is that if someone deletes all of the default values then it would also be an empty array and therefore you would be undoing any work that they did in deleting the values. When I first posted this code ACF would return NULL if the field was never set, this could have changed, but my assumption is still that something different should be returned for a field that has never been set than a field that has an empty value. This would need to be tested again.

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

The topic ‘pre populated repeater field’ is closed to new replies.