Support

Account

Home Forums General Issues Update choice from dynamically populated radio button automatically

Solving

Update choice from dynamically populated radio button automatically

  • I’m hoping that someone can help me with this, as I’m stumped!

    I have a radio button that is dynamically populated from an Options page repeater field like so:

    function acf_load_service_choices( $field ) {
        
        // reset choices
        $field['choices'] = array();
    
        // if has rows
        if( have_rows('calls_to_action', 'option') ) {
            
            // while has rows
            while( have_rows('calls_to_action', 'option') ) {
                
                // instantiate row
                the_row();
                
                
                // vars
                $value = get_sub_field('title');
                $label = get_sub_field('title');
    
                
                // append to choices
                $field['choices'][ $value ] = $label;
                
            }
            
        }
    
        // return the field
        return $field;
        
    }
    
    add_filter('acf/load_field/name=choose_cta', 'acf_load_service_choices');

    An issue has come up whereby if someone changes the title on the repeater item, then the radio button values on other pages are now incorrect and the output doesn’t work. Is there anyway to have the change of title trickle down to other other pages, or to set a unique ID for each repeater item?

  • There isn’t any way to change the values saved to posts when the choices for the field change. Another issue you probably don’t realize is that the repeater can be reorganized, so, it may be impossible to tell what value in the repeater has changed, making it even more difficult to make the change to all of the posts.

    You could have two field, one for value and one for label, and then make the value field so that it cannot be edited after it is added. I recently replied to another topic on hot to disable some things about repeaters, including making a field readonly https://support.advancedcustomfields.com/forums/users/hube2/replies/. If you applied only the portion for readonly on your value field then it would allow users to add values and edit the labels, but not edit the existing values.

    Then you just need to figure out what to do if your user decides to delete a value.

  • HI John, that link you posted about disabling element of repeaters only goes to a page of replies you’ve posted on the forums.

    That does sound like a possible solution though. The only issue that could arise here is if a previously-used value is used again. It’s an unlikely event, but possible. I don’t suppose new fields in repeaters get assigned an ID value in the database that can be accessed somehow?

  • duh, sorry about that, copied the wrong link try this https://support.advancedcustomfields.com/forums/topic/write-only-repeater-field/

    No, there is no ID for repeaters, they are just meta values with meta keys based on the repeater field name and the field name and these meta keys will change for a row if they are reordered.

  • That’s the one. Okay, so I assume I’d be replacing:

    $(element).find('.acf-input input').attr('readonly', true);

    with something like:

    $(element).find('div[data-name="FIELD_NAME_HERE"] input').attr('readonly', true);

    Correct?

    Well that’s better than what I had originally, so thank you. Shame there’s no unique ID field available to repeaters!

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

The topic ‘Update choice from dynamically populated radio button automatically’ is closed to new replies.