Support

Account

Home Forums Add-ons Repeater Field Issues using a repeater field with the REST API

Unread

Issues using a repeater field with the REST API

  • Hey guys, I’ve registered a custom REST field to allow a user to get and update some data in an ACF repeater field.

    The issue I’m having – actually there are two – is that the response from the POST request to update the record contains the previous value of the ACF repeater row.

    I’ve narrowed this down to two things. Firstly, when I update the record, ACF maintains an $active_loop, which I needed to reset after updating the row, which I have done so using reset_rows(); This was causing the have_rows() loop to return false when I was fetching the new data for the row.

    Secondly – and where I’m still stuck- is when I go to fetch the updated record. It seems like ACF maintains a local store of fields, which it queries before it looks at the database acf-value-functions.php line 64 $store = acf_get_store( 'values' );.

    I found an undocumented function acf_reset_local() which I thought would clear the store, but it doesn’t work. I need to clear the store (or at least update it), so that I can fetch the correct record from the database.

    Any help would be greatly appreciated. Thank you in advance.

    Code snippets included below:

    
    public static function get_private_note($prepared, $field_name, $request, $object_type)
        {
            if (have_rows('private_notes', $prepared['id'])) {
                while (have_rows('private_notes', $prepared['id'])) {
                    the_row();
                    $user_id = absint(get_sub_field('user'));
                    if ($user_id === get_current_user_id()) {
                        return get_sub_field('private_note');
                    }
                }
            }
    
            return '';
        }
    
        public static function update_private_note($value, \WP_POST $object, $field_name, $request, $object_type)
        {
            if (have_rows('private_notes', $object->ID)) {
                while (have_rows('private_notes', $object->ID)) {
                    the_row();
                    $user_id = absint(get_sub_field('user'));
                    if ($user_id === get_current_user_id()) {
                        update_sub_field('private_note', esc_html($value));
                        break;
                    }
                }
                reset_rows();
            } else {
                add_row('private_notes', [
                    'user' => absint(get_current_user_id()),
                    'private_note' => esc_html($value),
                ], $object->ID);
            }
        }
    
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.