__('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',
'placeholder' => __('Add Row', 'acf'),
)
);
}
/*
* 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)) {
return false;
}
// bail ealry if not numeric
if (! is_numeric($value)) {
return false;
}
// bail early if no sub fields
if (empty($field['sub_fields'])) {
return false;
}
// vars
$value = intval($value);
$rows = array();
// loop
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;
}
}
// 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)) {
return false;
}
// bail ealry if not array
if (! is_array($value)) {
return false;
}
// bail early if no sub fields
if (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)
{
// vars
$count = 0;
// check if is value (may be empty string)
if (is_array($value)) {
// remove acfcloneindex
if (isset($value['acfcloneindex'])) {
unset($value['acfcloneindex']);
}
// count
$count = count($value);
}
// validate required
if ($field['required'] && ! $count) {
$valid = false;
}
// min
$min = (int)$field['min'];
if ($min && $count < $min) {
// create error
$error = __('Minimum rows reached ({min} rows)', 'acf');
$error = str_replace('{min}', $min, $error);
// return
return $error;
}
// validate value
if ($count) {
// bail early if no sub fields
if (! $field['sub_fields']) {
return $valid;
}
// loop rows
foreach ($value as $i => $row) {
// loop sub fields
foreach ($field['sub_fields'] as $sub_field) {
// vars
$k = $sub_field['key'];
// test sub field exists
if (! isset($row[$k])) {
continue;
}
// validate
acf_validate_value($row[$k], $sub_field, "{$input}[{$i}][{$k}]");
}
// end loop sub fields
}
// end loop rows
}
// return
return $valid;
}
/**
* This function will update a value row.
*
* @date 15/2/17
*
* @param array $row
* @param int $i
* @param array $field
* @param mixed $post_id
*
* @return boolean
* @since 5.5.8
*
*/
function update_row($row, $i, $field, $post_id)
{
// bail early if no layout reference
if (! is_array($row)) {
return false;
}
// bail early if no layout
if (empty($field['sub_fields'])) {
return false;
}
// loop
foreach ($field['sub_fields'] as $sub_field) {
// value
$value = null;
// find value (key)
if (isset($row[$sub_field['key']])) {
$value = $row[$sub_field['key']];
// find value (name)
} elseif (isset($row[$sub_field['name']])) {
$value = $row[$sub_field['name']];
// value does not exist
} else {
continue;
}
// modify name for save
$sub_field['name'] = "{$field['name']}_{$i}_{$sub_field['name']}";
// update field
acf_update_value($value, $post_id, $sub_field);
}
// return
return true;
}
/**
* This function will delete a value row.
*
* @date 15/2/17
*
* @param int $i
* @param array $field
* @param mixed $post_id
*
* @return boolean
* @since 5.5.8
*
*/
function delete_row($i, $field, $post_id)
{
// bail early if no sub fields
if (empty($field['sub_fields'])) {
return false;
}
// loop
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);
}
// return
return true;
}
/*
* 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)
{
// bail early if no sub fields
if (empty($field['sub_fields'])) {
return $value;
}
// vars
$new_value = 0;
$old_value = (int)acf_get_metadata($post_id, $field['name']);
// update sub fields
if (! empty($value)) {
$i = -1;
// remove acfcloneindex
if (isset($value['acfcloneindex'])) {
unset($value['acfcloneindex']);
}
// loop through rows
foreach ($value as $row) {
$i++;
// bail early if no row
if (! is_array($row)) {
continue;
}
// update row
$this->update_row($row, $i, $field, $post_id);
// append
$new_value++;
}
}
// remove old rows
if ($old_value > $new_value) {
// loop
for ($i = $new_value; $i < $old_value; $i++) {
$this->delete_row($i, $field, $post_id);
}
}
// save false for empty value
if (empty($new_value)) {
$new_value = '';
}
// return
return $new_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_value = (int)acf_get_metadata($post_id, $field['name']);
// bail early if no rows or no sub fields
if (! $old_value || empty($field['sub_fields'])) {
return;
}
// loop
for ($i = 0; $i < $old_value; $i++) {
$this->delete_row($i, $field, $post_id);
}
}
/*
* 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;
}
/*
* translate_field
*
* This function will translate field settings
*
* @type function
* @date 8/03/2016
* @since 5.3.2
*
* @param $field (array)
* @return $field
*/
function translate_field($field)
{
// translate
$field['button_label'] = acf_translate($field['button_label']);
// return
return $field;
}
/*
* validate_any_field
*
* This function will add compatibility for the 'column_width' setting
*
* @type function
* @date 30/1/17
* @since 5.5.6
*
* @param $field (array)
* @return $field
*/
function validate_any_field($field)
{
// width has changed
if (isset($field['column_width'])) {
$field['wrapper']['width'] = acf_extract_var($field, 'column_width');
}
// return
return $field;
}
/**
* prepare_field_for_export
*
* Prepares the field for export.
*
* @date 11/03/2014
*
* @param array $field The field settings.
*
* @return array
* @since 5.0.0
*
*/
function prepare_field_for_export($field)
{
// Check for sub fields.
if (! empty($field['sub_fields'])) {
$field['sub_fields'] = acf_prepare_fields_for_export($field['sub_fields']);
}
return $field;
}
/**
* prepare_field_for_import
*
* Returns a flat array of fields containing all sub fields ready for import.
*
* @date 11/03/2014
*
* @param array $field The field settings.
*
* @return array
* @since 5.0.0
*
*/
function prepare_field_for_import($field)
{
// Check for sub fields.
if (! empty($field['sub_fields'])) {
$sub_fields = acf_extract_var($field, 'sub_fields');
// Modify sub fields.
foreach ($sub_fields as $i => $sub_field) {
$sub_fields[$i]['parent'] = $field['key'];
$sub_fields[$i]['menu_order'] = $i;
}
// Return array of [field, sub_1, sub_2, ...].
return array_merge(array($field), $sub_fields);
}
return $field;
}
/**
* Additional validation for the repeater field when submitted via REST.
*
* @param bool $valid
* @param int $value
* @param array $field
*
* @return bool|WP_Error
*/
public function validate_rest_value($valid, $value, $field)
{
if (! is_array($value) && is_null($value)) {
$param = sprintf('%s[%s]', $field['prefix'], $field['name']);
$data = array(
'param' => $param,
'value' => $value,
);
$error = sprintf(__('%s must be of type array or null.', 'acf'), $param);
return new WP_Error('rest_invalid_param', $error, $param);
}
return $valid;
}
/**
* Return the schema array for the REST API.
*
* @param array $field
*
* @return array
*/
public function get_rest_schema(array $field)
{
$schema = array(
'type' => array('array', 'null'),
'required' => ! empty($field['required']),
'items' => array(
'type' => 'object',
'properties' => array(),
),
);
foreach ($field['sub_fields'] as $sub_field) {
if ($sub_field_schema = acf_get_field_rest_schema($sub_field)) {
$schema['items']['properties'][$sub_field['name']] = $sub_field_schema;
}
}
if (! empty($field['min'])) {
$schema['minItems'] = (int)$field['min'];
}
if (! empty($field['max'])) {
$schema['maxItems'] = (int)$field['max'];
}
return $schema;
}
/**
* Apply basic formatting to prepare the value for default REST output.
*
* @param mixed $value
* @param int|string $post_id
* @param array $field
*
* @return array|mixed
*/
public function format_value_for_rest($value, $post_id, array $field)
{
if (empty($value) || ! is_array($value) || empty($field['sub_fields'])) {
return null;
}
// Loop through each row and within that, each sub field to process sub fields individually.
foreach ($value as &$row) {
foreach ($field['sub_fields'] as $sub_field) {
// Bail early if the field has no name (tab).
if (acf_is_empty($sub_field['name'])) {
continue;
}
// Extract the sub field 'field_key'=>'value' pair from the $row and format it.
$sub_value = acf_extract_var($row, $sub_field['key']);
$sub_value = acf_format_value_for_rest($sub_value, $post_id, $sub_field);
// Add the sub field value back to the $row but mapped to the field name instead
// of the key reference.
$row[$sub_field['name']] = $sub_value;
}
}
return $value;
}
}
// initialize
acf_register_field_type('acf_field_repeater');
endif; // class_exists check
?>