Support

Account

Home Forums Add-ons Repeater Field load_field – repeater adds empty fields to Field Group

Solved

load_field – repeater adds empty fields to Field Group

  • Hi Team,

    Not sure if this is a bug or not.. but when using load_field to add fields to a repeater ($field['sub_fields'] = array(...) it works great.

    However, if one were to add another field to the Field Group (sister to the repeater field, not a child) – ACF adds dozens of empty fields. Screenshot attached.

    load_field repeater adds empty fields to Field Group

    Code I am using attached:

    function my_acf_load_field( $field ) {
      $field['sub_fields'] = array(
      	array(
      		'key' => 'headline',
      		'label' => 'Headline',
      		'name' => 'headline',
      		'type' => 'text'
      	),
      	array(
      		'key' => 'content',
      		'label' => 'Content',
      		'name' => 'content',
      		'type' => 'textarea'
      	),
      	array(
      		 'key' => 'gallery',
      		 'label' => 'Gallery',
      		 'name' => 'gallery',
      		 'type' => 'repeater',
      		 'sub_fields' => array(
      			 array(
      				 'key' => 'title',
      				 'label' => 'Title',
      				 'name' => 'title',
      				 'type' => 'text',
      			 ),
      			 array(
      				 'key' => 'description',
      				 'label' => 'Description',
      				 'name' => 'description',
      				 'type' => 'textarea',
      			 )
      			)
      	)
      );
      return $field;
    }
    
    add_filter('acf/load_field/name=custom_repeater', 'my_acf_load_field');

    The issue seems to be when there is an array inside of 'sub_fields' => array(). The same issue occurs for flexible content fields…

    Example of code attached:

    $field['layouts'] = array(
      array(
        'key' => 'layout_1',
        'label' => 'Layout 1',
        'name' => 'layout_1',
        'sub_fields' => array(
          array(
            'key' => 'headline',
            'label' => 'Headline',
            'name' => 'headline',
            'type' => 'text'
          )
        )
      )
    );
  • So, I have a function that pre-loads a repeater field with a specific number of sub fields and adds content to them. I’m doing this with acf/load_value rather than acf/load_field. This will also only create create the sub fields and values it it is a new post.

    Hoping this helps.

    
    <?php 
      
      /*
          the format of the value array needs to be
          
          $value = array(
            // a nested array for each row to be created
            array(
              // an array object for each field_key in the row
              'field_key_1' => 'value',
              'field_key_2' => 'value'
            )
          );
          
          The above would be tedious to create and error prone
          so I did it the other way and then did a loop to make it right
          I suspect you'd need to do it the hard way for nested repeaters
      */
      
      add_filter('acf/load_value/name=FIELD_NAME', 'acf_load_FIELD_NAME_value', 10, 3);
        
      function acf_load_FIELD_NAME_value($value, $post_id, $field) {
        if ($value !== NULL) {
          return $value;
        }
        $subfield_values = array(
          // field_key => array of values
          'field_5601faa3a7737' => array('Test 1', 'Test 2', 'Test 3'),
          'field_5601fbc1a7738' => array('Test 4', 'Test 5', 'Test 6'),
          'field_5601fbd9a7739' => array('Test 7', 'Test 8', 'Test 9'),
        );
        $values = array();
        foreach ($subfield_values as $field_key => $field_values) {
          for ($i=0; $i<count($field_values); $i++) {
            $values[$i][$field_key] = $field_values[$i];
          }
        }
        $value = $values;
        return $value;
      }
      
    ?>
    

    I’m not sure if this will work with nested repeater fields.

    To find out the format of a repeater field with nested repeaters create an acf/load_field filter for an existing repeater that has content in the sub fields and in the nested repeater sub fields and output the value. Here is an example of how to do that. Then you can see how you need to format the value to return to add content with the function above.

    
    <?php 
      
      add_filter('acf/load_value/name=FIELD_NAME', 'acf_load_FIELD_NAME_value', 10, 3);
        
      function acf_load_FIELD_NAME_value($value, $post_id, $field) {
        echo '<pre>'; print_r($value); echo '</pre>';\
        return $value;
      }
      
    ?>
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘load_field – repeater adds empty fields to Field Group’ is closed to new replies.