Home › Forums › Add-ons › Repeater Field › Get row index inside add_filter( 'acf/load_field' ) › Reply To: Get row index inside add_filter( 'acf/load_field' )
@hube2
Hi John, That’s really useful, wish i had found this sooner it would saved me a day or so.
I’m really interested in your filtering is a sub field in repeater that is nested in another repeater that is then nested in a group field that is nested in another repeater.
I am trying and failing to update my sub fields of nested repeaters.
I have the following options page, basically i want to create business events (repeater 1)
Field Group: group_6380cf9026f49
Repeater (1): field_6380d01f71839 (these are events)
Group(1): field_645e4a88f718e
Group(2): field_645e3a33aa018
Repeater (2): field_646205bfe80ff
Group (3): field_6462172e85e75
Select field: field_646206abe8102 (these are Tips)
The select field is grabbing a field group name from a field in Group (2): field_645e628bf1588 which pulls all the field name values as choices from another field group.
I’ve used your class code solution and it fetches the group name fine from Group (2): field_645e628bf1588, i can see it’s pulling back the field group values for Select field (field_646206abe8102)
But the sub repeater that allows people to select the Tips per event aren’t the correct fields for the event when either adding new tips or editing previous tips. The very first tip in the first event is fine, then they are all out of sync to the row index or don’t show at all.
I think i need another index counter for the sub rows, but i am not quite sure how best to achieve this. Would love to know more on how you handle this? especially as i’m going crazy now
class MY_PROJECT_NAME_acf_filters
{
// variable for row index
private $my_field_name_index = 0;
public function __construct()
{
// add a filter when preparing repeater for monitoring index count
// Field: event -> field_6380d01f71839
add_filter('acf/prepare_field/key=field_6380d01f71839', array($this, 'init_my_field_name_index'));
// add filter for ths sub field that you want to filter
// FIeld: tips field_646206abe8102
add_filter('acf/prepare_field/name=field_646206abe8102', array($this, 'filter_my_sub_field_name'));
}
public function init_my_field_name_index($field)
{
$this->my_field_name_index = 0;
return $field;
}
public function filter_my_sub_field_name($field)
{
// Get the index count
$row_index = $this->my_field_name_index;
//acf[field_6380d01f71839][row-2][field_645e4a88f718e][field_645e3a33aa018][field_646205bfe80ff][acfcloneindex][field_6462172e85e75]" (overview group)
// Get Group Key Name from Group(2): field_645e3a33aa018 -> field_645e628bf1588
$getGroupKey = get_option('events_judging_event_' . $row_index . '_all_settings_judging_settings_judging_field_group');
if ($getGroupKey) {
$field['choices'] = [];
// Get all fields for the group
$choices = acf_get_fields($getGroupKey);
if (is_array($choices)) {
// loop through array and add to field 'choices'
foreach ($choices as $choice) {
//Set the select values
if ($choice['type'] != 'message') {
$field['choices'][$choice['key']] = $choice['label'];
}
}
}
}
// after filtering increment the field index
$this->my_field_name_index++;
return $field;
} else {
return $field;
}
}
new MY_PROJECT_NAME_acf_filters ();
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.