Support

Account

Home Forums Add-ons Repeater Field Default open fields in repeater Reply To: Default open fields in repeater

  • This will create a number of empty rows when there are no rows

    
    function set_start_rows($value, $post_id, $field) {
      if (!$value) {
        $row = array(
          // field key => value pairs for all subfields
          'field_5bcf70b75df1d' => NULL,
        );
        $number_of_rows = 10;
        $value = array_fill(0, $number_of_rows, $row);
      }
      return $value;
    }
    add_filter('acf/load_value/key=field_5bcf70a95df1c', 'set_start_rows', 20, 3);
    

    This would automatically add 10 a number of new rows if there are no rows or the last row does not contain a value in a field

    
    function set_start_rows($value, $post_id, $field) {
      // only do this in the admin
      if (!is_admin()) {
        return $value;
      }
      $add_rows = false;
      $index = 0;
      if (is_array($value)) {
        $index = count($value);
        if ($value[count($value)-1]['field_5bcf70b75df1d']) {
          // field is not empty in last row
          $add_rows = true;
        }
      }
      if (!$value) {
        $add_rows = true;
      }
      if ($add_rows) {
        if (!$value) {
          $value = array();
        }
        $row = array(
          // field key => value pairs for all subfields
          'field_5bcf70b75df1d' => NULL,
        );
        $number_of_rows = 5;
        $value = array_merge($value, array_fill($index, $number_of_rows, $row));
      }
      return $value;
    }
    add_filter('acf/load_value/key=field_5bcf70a95df1c', 'set_start_rows', 20, 3);
    

    A really elegant way to do this would be to use JavaScript and detect when a value is added to the last row and then automatically add a new row. This should be possible, but I don’t have the answer to how to do that and it would take me a long time to figure out and test.