function select_choices()

in web/wp-content/plugins/acf-extended/includes/modules/form/module-form-fields.php [576:689]


    function select_choices($field){
        
        // validate
        if(acf_maybe_get($field, 'ajax_action') !== 'acfe/form/map_field_ajax'){
            return $field;
        }
        
        // is load
        $is_load = isset($field['wrapper']['data-related-field']) && !empty($field['wrapper']['data-related-field']);
        
        // fix nonce with acf 6.3.10
        $field['nonce'] = wp_create_nonce($field['key']);
        
        // global
        global $item;
        
        // prepare field groups
        $field_groups = $item['field_groups'];
        $field_groups = array_filter($field_groups, 'acf_get_field_group');
        $field_groups = array_unique($field_groups);
        
        // loop field groups
        foreach($field_groups as $field_group){
            
            // get fields
            $fields = $this->get_fields($field_group);
            
            // append
            foreach($fields as $_field){
                
                $key = $is_load ? $_field['key'] : "{field:{$_field['key']}}";
                
                $field['choices'][ $key ] = $_field['label'];
                
            }
            
        }
        
        $ajax_choices = array();
        
        // loop choices
        foreach(array_keys($field['choices']) as $key){
            
            // get value
            $value = $field['choices'][ $key ];
            
            // check field key
            if(is_string($key) && $this->is_field_key_tag($key, $is_load)){
                
                if(is_string($value) && acf_is_field_key($value)){
                    $field['choices'][ $key ] = $this->get_field_label($value);
                }
                
                continue;
                
            }
            
            $ajax_choices[ $key ] = $value;
            
        }
        
        // encode choices
        if($ajax_choices){
            
            // remove custom choices from choice
            if(isset($field['custom_choices'])){
                foreach(array_keys($ajax_choices) as $key){
                    if(isset($field['custom_choices'][ $key ])){
                        unset($ajax_choices[ $key ]);
                    }
                }
            }
            
            $field['wrapper']['data-choices'] = json_encode($ajax_choices);
            
        }
    
        // encode custom choices
        if(isset($field['custom_choices'])){
        
            // remove custom choice if already in choices
            foreach(array_keys($field['custom_choices']) as $key){
                if(isset($field['choices'][ $key ])){
                    unset($field['custom_choices'][ $key ]);
                }
            }
        
            $field['wrapper']['data-custom-choices'] = json_encode($field['custom_choices']);
        
        }
        
        // todo: remove exception here
        if($field['_name'] === 'save_post_terms'){
            
            foreach(array_keys($field['choices']) as $taxonomy){

                $terms = $field['choices'][ $taxonomy ];
                if(is_array($terms)){

                    unset($field['choices'][ $taxonomy ]);

                    foreach($terms as $key => $value){
                        $field['choices'][ $key ] = $value;
                    }

                }
            }
            
        }
        
        // return
        return $field;
        
    }