Home › Forums › Add-ons › Repeater Field › Repeater load_value() › Reply To: Repeater load_value()
Think i have found a way to achieve what i need.
I ended up using the pre_load_value filter. which if you hook into and return a value. is then skipped when the repeater field goes to load its value.
This way i can mimic the load_value function and the acf_get_value function just returns my value as opposed to the system value.
incase anyone ever needs this in the future here is the code i used.
add_filter('acf/pre_load_value', array($this, 'pre_load_value'), 3, 3);
function pre_load_value( $value, $post_id, $field ){
if($field['type'] != 'repeater'){
return $value;
}
if(!$this->search_sub_fields_for_type($field['sub_fields'], $this->name)){
return $value;
}
// Get field name.
$field_name = $field['name'];
// Check store.
$store = acf_get_store( 'values' );
if( $store->has( "$post_id:$field_name" ) ) {
return $store->get( "$post_id:$field_name" );
}
// Load value from database.
$value = acf_get_metadata( $post_id, $field_name );
// Use field's default_value if no meta was found.
if( $value === null && isset($field['default_value']) ) {
$value = $field['default_value'];
}
//User our own load_value filter to add additional information to subfields
$value = apply_filters( "ttc_repeater_load_value", $value, $post_id, $field );
// Update store.
$store->set( "$post_id:$field_name", $value );
// Return value.
return $value;
}
add_filter('ttc_repeater_load_value', array($this, 'try_add_grid_info'), 2, 3);
function try_add_grid_info($value, $post_id, $field){
// bail early if no sub fields
if( empty($field['sub_fields']) ) return $value;
// vars
$value = intval($value);
$rows = array();
$field["total_rows"] = $value;
// 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']}";
$sub_field["original_order"] = $i;
$sub_field["latest_row"] = (($field["total_rows"]-1) == $i);
$sub_field["total_row_count"] = ($field["total_rows"]);
// get value
$sub_value = acf_get_value( $post_id, $sub_field );
// add value
$rows[ $i ][ $sub_field['key'] ] = $sub_value;
}
}
return $rows;
}
/** function to loop through sub fields and look for a particular sub field type.
* @param $sub_fields array of repeater sub fields
* @param $field_type - Field type we are looking for
* @return bool
*/
function search_sub_fields_for_type($sub_fields, $field_type){
if(!empty($sub_fields)){
foreach($sub_fields as $sub_field){
if(!empty($sub_field["type"]) && $sub_field["type"] == $field_type ){
return $sub_field;
}
}
}
return false;
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.