Support

Account

Home Forums General Issues Saving a dynamic Select Box

Solving

Saving a dynamic Select Box

  • Hi,

    Here an article talks about populating a select box dynamically:
    https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/

    I’m using flexible content with a select box so it’s a little bit different.

    I can get the the values to load correctly, but I cant get them to save. I have tried so may variations. It looks like the issue stems from calling have_rows() during the loop.

    The Following code works:

    function acf_load_color_field_choices($field)
    {
    // // reset choices
    $field['choices'] = array();
    $field['choices'][1] = "one";
    $field['choices'][2] = "two";
    return $field;
    }
    add_filter('acf/load_field/key=field_63f3f71d371a8', 'acf_load_color_field_choices');

    However, when I try to change it to something a little more dynamic everything breaks. Here is the code that I am currently working with.

    function acf_load_color_field_choices($field)
    {
    
    // // reset choices
    $field['choices'] = array();
    
    if (have_rows('page_sections')) :
    while (have_rows('page_sections')) : the_row();
    $anchor_link = get_sub_field('anchor_link');
    $anchor_link_sani = str_replace(" ", "", strtolower($anchor_link));
    if ($anchor_link) :
    $field['choices'][$anchor_link_sani] = ucwords($anchor_link);
    endif;
    endwhile;
    endif;
    
    return $field;
    }
    add_filter('acf/load_field/key=field_63f3f71d371a8', 'acf_load_color_field_choices');

    Even when I loop using have_rows without loading anything, everything breaks.
    I’m assuming this has something to do with accessing the loop? Why does it load correctly, but not save?
    Is there anything that I can do?

    Thanks.

  • Okay after further testing I have a little more info. I guess it has to do with the structure of my fields!

    Currently the select field is part of Repeater, that is inside a Flexible Content.

    When I take the Repeater out of the Flexible Content and attach it it directly to the page everything works!

    I can’t seem to find the connection. I’ve tried adding Prefix Field Labels/Names but couldn’t get to work. The documentation states that clone fields are fields themself, but maybe I’m missing something.

    I’m hoping this insight might be enough information to help us find a solution.

  • Are you trying to loop over the same flex field that the repeater=>select is part of. I don’t know if that would cause the problem but I’m guessing that might be it.

    My second guess would be that you are not defining the post ID for the have rows loop and ACF does not know what post to use. You can test this by adding the following to the top of your function

    
    global $post;
    var_dump($post->ID);
    

    and see what post ACF is assuming it should get the values from.

  • Hi. Thanks for the help. It does indeed know the post id. Removing the section out of the flex content makes it work. The code you posted also shows the correct post id.

    However, after a little more digging I seem to have found the issue.
    It seems to be a known limitation: https://www.advancedcustomfields.com/resources/clone/#limitations

    Any suggestions on getting around this?

    For now I’m thinking of changing the clone inside the flex field to just duplicate the data.

  • What part of the limitations are you having an issue with. There is no way to work around #1.

  • Yeah, unfortunatlly it is number one 🙁

    Whats the saying? When the door is closed, climb through the window

    I guess I’ll have to find another way unless anyone else has some fancy ideas.

  • Instead of making the clones siblings put each clone inside a group field with a different name for each group.

    Or create a repeater field and clone the group as a the only sub field of the repeater. This will allow have_rows on the repeater and get_sub_field() for each field of the cloned group.

  • For now just to get it done, this is what I ended up doing:

    When I take the Repeater out of the Flexible Content and attach it it directly to the page everything works!

    Can you expand a little on the first part?

    I don’t think the second option will work, because all the information is needed. I’ve attached a screenshot.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.