__('Collapsed','acf'),
'instructions' => __('Select a sub field to show when row is collapsed','acf'),
'type' => 'select',
'name' => 'collapsed',
'allow_null' => 1,
'choices' => $choices
));
// min
acf_render_field_setting( $field, array(
'label' => __('Minimum Rows','acf'),
'instructions' => '',
'type' => 'number',
'name' => 'min',
'placeholder' => '0',
));
// max
acf_render_field_setting( $field, array(
'label' => __('Maximum Rows','acf'),
'instructions' => '',
'type' => 'number',
'name' => 'max',
'placeholder' => '0',
));
// layout
acf_render_field_setting( $field, array(
'label' => __('Layout','acf'),
'instructions' => '',
'class' => 'acf-repeater-layout',
'type' => 'radio',
'name' => 'layout',
'layout' => 'horizontal',
'choices' => array(
'table' => __('Table','acf'),
'block' => __('Block','acf'),
'row' => __('Row','acf')
)
));
// button_label
acf_render_field_setting( $field, array(
'label' => __('Button Label','acf'),
'instructions' => '',
'type' => 'text',
'name' => 'button_label',
));
}
/*
* load_value()
*
* This filter is applied to the $value after it is loaded from the db
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param $value (mixed) the value found in the database
* @param $post_id (mixed) the $post_id from which the value was loaded
* @param $field (array) the field array holding all the field options
* @return $value
*/
function load_value( $value, $post_id, $field ) {
// bail early if no value
if( empty($value) || empty($field['sub_fields']) ) {
return $value;
}
// convert to int
$value = intval( $value );
// vars
$rows = array();
// check number of rows
if( $value > 0 ) {
// loop through rows
for( $i = 0; $i < $value; $i++ ) {
// create empty array
$rows[ $i ] = array();
// loop through sub fields
foreach( array_keys($field['sub_fields']) as $j ) {
// get sub field
$sub_field = $field['sub_fields'][ $j ];
// bail ealry if no name (tab)
if( acf_is_empty($sub_field['name']) ) continue;
// update $sub_field name
$sub_field['name'] = "{$field['name']}_{$i}_{$sub_field['name']}";
// get value
$sub_value = acf_get_value( $post_id, $sub_field );
// add value
$rows[ $i ][ $sub_field['key'] ] = $sub_value;
}
// foreach
}
// for
}
// if
// return
return $rows;
}
/*
* format_value()
*
* This filter is appied to the $value after it is loaded from the db and before it is returned to the template
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param $value (mixed) the value which was loaded from the database
* @param $post_id (mixed) the $post_id from which the value was loaded
* @param $field (array) the field array holding all the field options
*
* @return $value (mixed) the modified value
*/
function format_value( $value, $post_id, $field ) {
// bail early if no value
if( empty($value) || empty($field['sub_fields']) ) {
return false;
}
// loop over rows
foreach( array_keys($value) as $i ) {
// loop through sub fields
foreach( array_keys($field['sub_fields']) as $j ) {
// get sub field
$sub_field = $field['sub_fields'][ $j ];
// bail ealry if no name (tab)
if( acf_is_empty($sub_field['name']) ) continue;
// extract value
$sub_value = acf_extract_var( $value[ $i ], $sub_field['key'] );
// update $sub_field name
$sub_field['name'] = "{$field['name']}_{$i}_{$sub_field['name']}";
// format value
$sub_value = acf_format_value( $sub_value, $post_id, $sub_field );
// append to $row
$value[ $i ][ $sub_field['_name'] ] = $sub_value;
}
}
// return
return $value;
}
/*
* validate_value
*
* description
*
* @type function
* @date 11/02/2014
* @since 5.0.0
*
* @param $post_id (int)
* @return $post_id (int)
*/
function validate_value( $valid, $value, $field, $input ){
// remove acfcloneindex
if( isset($value['acfcloneindex']) ) {
unset($value['acfcloneindex']);
}
// valid
if( $field['required'] && empty($value) ) {
$valid = false;
}
// check sub fields
if( !empty($field['sub_fields']) && !empty($value) ) {
$keys = array_keys($value);
foreach( $keys as $i ) {
foreach( $field['sub_fields'] as $sub_field ) {
// vars
$k = $sub_field['key'];
// test sub field exists
if( !isset($value[ $i ][ $k ]) ) {
continue;
}
// validate
acf_validate_value( $value[ $i ][ $k ], $sub_field, "{$input}[{$i}][{$k}]" );
}
}
}
return $valid;
}
/*
* update_value()
*
* This filter is appied to the $value before it is updated in the db
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param $value - the value which will be saved in the database
* @param $field - the field array holding all the field options
* @param $post_id - the $post_id of which the value will be saved
*
* @return $value - the modified value
*/
function update_value( $value, $post_id, $field ) {
// vars
$total = 0;
// bail early if no sub fields
if( empty($field['sub_fields']) ) return $value;
// remove acfcloneindex
if( isset($value['acfcloneindex']) ) {
unset($value['acfcloneindex']);
}
// update sub fields
if( !empty($value) ) {
// $i
$i = -1;
// loop through rows
foreach( $value as $row ) {
// $i
$i++;
// increase total
$total++;
// loop through sub fields
foreach( $field['sub_fields'] as $sub_field ) {
// value
$v = false;
// key (backend)
if( isset($row[ $sub_field['key'] ]) ) {
$v = $row[ $sub_field['key'] ];
} elseif( isset($row[ $sub_field['name'] ]) ) {
$v = $row[ $sub_field['name'] ];
} else {
// input is not set (hidden by conditioanl logic)
continue;
}
// modify name for save
$sub_field['name'] = "{$field['name']}_{$i}_{$sub_field['name']}";
// update value
acf_update_value( $v, $post_id, $sub_field );
}
// foreach
}
// foreach
}
// if
// get old value (db only)
$old_total = (int) acf_get_metadata( $post_id, $field['name'] );
if( $old_total > $total ) {
for( $i = $total; $i < $old_total; $i++ ) {
foreach( $field['sub_fields'] as $sub_field ) {
// modify name for delete
$sub_field['name'] = "{$field['name']}_{$i}_{$sub_field['name']}";
// delete value
acf_delete_value( $post_id, $sub_field );
}
// foreach
}
// for
}
// if
// update $value and return to allow for the normal save function to run
$value = $total;
// save false for empty value
if( empty($value) ) {
$value = '';
}
// return
return $value;
}
/*
* delete_value
*
* description
*
* @type function
* @date 1/07/2015
* @since 5.2.3
*
* @param $post_id (int)
* @return $post_id (int)
*/
function delete_value( $post_id, $key, $field ) {
// get old value (db only)
$old_total = (int) acf_get_metadata( $post_id, $field['name'] );
// bail early if no rows or no sub fields
if( !$old_total || empty($field['sub_fields']) ) {
return;
}
for( $i = 0; $i < $old_total; $i++ ) {
foreach( $field['sub_fields'] as $sub_field ) {
// modify name for delete
$sub_field['name'] = "{$key}_{$i}_{$sub_field['name']}";
// delete value
acf_delete_value( $post_id, $sub_field );
}
// foreach
}
}
/*
* delete_field
*
* description
*
* @type function
* @date 4/04/2014
* @since 5.0.0
*
* @param $post_id (int)
* @return $post_id (int)
*/
function delete_field( $field ) {
// bail early if no sub fields
if( empty($field['sub_fields']) ) return;
// loop through sub fields
foreach( $field['sub_fields'] as $sub_field ) {
acf_delete_field( $sub_field['ID'] );
}
}
/*
* update_field()
*
* This filter is appied to the $field before it is saved to the database
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param $field - the field array holding all the field options
* @param $post_id - the field group ID (post_type = acf)
*
* @return $field - the modified field
*/
function update_field( $field ) {
// remove sub fields
unset($field['sub_fields']);
// return
return $field;
}
/*
* duplicate_field()
*
* This filter is appied to the $field before it is duplicated and saved to the database
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param $field - the field array holding all the field options
*
* @return $field - the modified field
*/
function duplicate_field( $field ) {
// get sub fields
$sub_fields = acf_extract_var( $field, 'sub_fields' );
// save field to get ID
$field = acf_update_field( $field );
// duplicate sub fields
acf_duplicate_fields( $sub_fields, $field['ID'] );
// return
return $field;
}
}
// initialize
acf_register_field_type( new acf_field_repeater() );
endif; // class_exists check
?>