Home › Forums › Bug Reports › ACF Repeater sub-fields are losing / swapping content when re-ordered
WordPress 4.7.3
ACF Pro 5.5.10
I have a repeater, with nested repeaters. In each nested repeater item is a set of sub-fields. So far so good.
However, because there are so many fields, I chose to only show these sub-fields on each nested repeater, if the user checks a box to see them (more user-friendly).
All of this works, except for when items are re-ordered on the parent repeater. The content doesn’t swap over in the fields that are hidden, but the other fields showing do. This is very strange behaviour and may be a bug.
e.g.
Sets (repeater)
– Set 1 (repeated field)
— Items (repeater)
— Title
— Show / Hide sub-fields (checkbox)
— Sub-fields (the problem occurs on these fields)
– Set 2 (repeated field)
— Items (repeater)
— Title
— Show / Hide sub-fields (checkbox)
— Sub-fields (the problem occurs on these fields)
– Set 3 (repeated field)
— Items (repeater)
— Title
— Show / Hide sub-fields (checkbox)
— Sub-fields (the problem occurs on these fields)
I hope this is clear?
If all sub-fields are hidden with the ‘show / hide’ checkbox and I update say, ‘set 3’, then re-order it to where ‘set 2’ was, the contents of the sub-fields in each set swap over! This is madness in behaviour and makes reordering the items near impossible.
Once I removed the show / hide functionality, the fields saved correctly after re-ordering.
However, removing this show / hide means that dragging and dropping the items to re-order is near impossible as they’re too big on the screen and it becomes very confusing.
At first I used a layout ‘tab’ above the sub-fields to act as the wrapper with which it shows and hides items beneath it. But then I tried putting the conditional logic instead on each sub-field directly. The results are the same however.
Any idea on this?
I came across this thread, which seems to be a similar issue, but Elliot states that the fields now all save regardless of whether shown or not.
I’m very stuck here on a solution and my client is getting rather agitated about it.
Any help would be greatly appreciated.
Many thanks
Michael
The problem is that you’re using conditional logic to hide/show the other fields. Fields that are hidden by conditional logic are not submitted and this is where you’re running into a problem. Only the fields that are shown are being updated.
If you want functionality like this in ACF, you’ll probably need to write your own JavaScript to do it.
Thanks John for the reply!
I understand that about hidden fields not saving, but I really do think this is a bug, because I just can’t imagine that content swapping around like that would be the expected behaviour.
Please see a video I made demonstrating this issue:
https://streamable.com/3zpab
I’ll also attach a dump of the field group JSON if you wouldn’t mind giving it a go?
Thanks
Michael
You can submit this as a bug to the developer in a support ticket https://support.advancedcustomfields.com/new-ticket/ but it really comes down to the fact that your using conditional logic in a way that it was not intended to be used and I don’t know if it can be fixed or not.
Hi John
Thanks again for the speedy reply. I have now submit this as a bug.
I may be using it incorrectly, but the fact it allows me to, then has unexpected behaviour – well, I just feel something needs to be guarded on that at the least.
Does the video play for you? If not, you can right-click to get the URL. This forum appears to have a few glitches of its own occurring right now too 🙂
Thanks
Michael
I don’t even need to see the video to understand what’s happening.
Conditional logic is for fields that will not be used or submitted rather than as a convenience to make the admin more readable, even if it is tempting to do so. ACF has a built in ability to collapse repeater rows that can be used for this purpose. My guess is that if the developer expands conditional login to include this use that there will be people submitting bugs because these conditional values are being saved when they should not be.
This has not been completely tested and may cause timeout issue if used with options pages or excessive numbers of fields. There may be other conditions where it causes problems that I’m not aware of.
step 1, create this function
// this function with the help of
// @gabriel dot sobrinho at gmail dot com
// http://php.net/manual/en/function.array-merge-recursive.php
function acf_array_merge_recursive_distinct(array &$array1, array &$array2) {
$merged = $array1;
foreach ($array2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = $this->array_merge_recursive_distinct($merged[$key], $value);
} else {
// change from original code
// do not overwrite value in first array if it exists
if (!isset($merged[$key])) {
$merged[$key] = $value;
}
}
}
return $merged;
}
Now we’re going to create an acf/save_post filter
// low priority ensures it runs before anything else
// make lower if needed
add_filter('acf/save_post', 'acf_reorder_hidden_fields', -1);
function acf_reorder_hidden_fields($post_id) {
if (!isset($_POST['acf'])) {
// no need to run
return;
}
/*
You probably want to do some more checking here
for example only running on certain post types
or not running on options pages, etc, etc...
*/
// get existing field values for all fields of this $post_id
// we need to do this by getting the field objects
// and load unformulated values
// because we need to have field keys
$fields = get_field_objects($post_id, false);
// set up array to hold old values
$old_values = array();
foreach ($fields as $field) {
$old_values[$field['key']] = $field['value'];
}
// now using the custom array maerg function
$_POST['acf'] = acf_array_merge_recursive_distinct($_POST['acf'], $old_values);
}
I’d like to chime in and say that I’m having the same/similar issue. (Actually my problem is the exact same as the other support thread that was linked in the original post.)
The main reason I use the conditional logic this way is that it can be very difficult to re-order repeater rows if they have more than one or two small fields, as dragging and dropping becomes almost impossible when each repeater field row fills up most of the screen.
I’m going to experiment with using the built-in feature of collapsing repeater fields instead.
I would like to also chime in to say that I’m having the same issue as above. We used conditional hide/show logic in a more complex repeater field to make choices more clear to a variety of editors, but now the reordering of those repeaters swaps and/or merges content in a similar way as described in the original post. Is there any chance this will be addressed in a patch update? Thank you in advance!
As described above, fields hidden by conditional logic are not submitted or updated. I didn’t build ACF and I can’t say if this will change, I just know the way it works and I don’t expect it to change.
You would need to submit a support request to get the developer’s input and that can be done here https://support.advancedcustomfields.com/new-ticket/.
If you want to use conditional logic for some other purpose than having conditional values then you need to look at some other way of doing this. For example, building custom JavaScript that will hide and show fields in some other way than using the built in condition logic functionality of ACF. You could hide and show fields in some other way that would allow the values of the fields to be submitted.
Another chime in, I’m experiencing the same issues as @mummarketing. Did anyone find a solution? I’ve spent a day banging my head against the wall on this one.
Experiencing the same problem. Has anyone found a workable solution?
I think I understand why fields hidden by conditional logic are not submitted or updated, but if the repeater rows are draggable, the expected behavior is that the hidden fields get updated.
This works in latest ACF PRO. It will make all hidden fields to be saved even tho they are hidden. Fixes issue with re-ordering flexible content rows and data being messed up.
function my_acf_input_admin_footer() {
?>
<script type="text/javascript">
(function($) {
acf.add_action('hide_field', function($field, context) {
// bail early if not conditional logic (may be tab field)
if (context !== 'conditional_logic') {
return;
}
// remove disable attr so value gets saved even tho field was hidden
$field.find(':disabled').removeAttr('disabled');
});
})(jQuery);
</script>
<?php
}
add_action('acf/input/admin_footer', 'my_acf_input_admin_footer');
This is always the way ACF has worked. Changing a field’s name will cause any data already stored in a field to not be found. Changing the hierarchy of a field changes it’s name. Once a field is created and is in use its name, and in many cases other settings as well, should not be altered.
The topic ‘ACF Repeater sub-fields are losing / swapping content when re-ordered’ is closed to new replies.
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.