function prepare_field()

in web/wp-content/plugins/acf-extended/includes/fields/field-taxonomy-terms.php [801:919]


    function prepare_field($field){
        
        // value
        $value = acf_maybe_get($field, 'value');
        $value = acf_get_array($value);
        
        // choices
        $field['choices'] = array();
    
        // field type
        $field['type'] = $field['field_type'];
    
        // normal choices
        if($field['type'] !== 'select' || !$field['ui'] || !$field['ajax'] || isset($field['ajax_action'])){
            
            //vars
            $name = $field['_name'];
            $key = $field['key'];
            $post_id = acfe_get_post_id();
    
            // filters
            $args = array();
            $args = apply_filters("acfe/fields/taxonomy_terms/query",               $args, $field, $post_id);
            $args = apply_filters("acfe/fields/taxonomy_terms/query/name={$name}",  $args, $field, $post_id);
            $args = apply_filters("acfe/fields/taxonomy_terms/query/key={$key}",    $args, $field, $post_id);
    
            $choices = $this->get_terms($field, $args);
    
            $keys = array_keys($choices);
            
            // Single Term
            if(count($keys) === 1){
                $choices = $choices[ $keys[0] ];
            }
            
            $field['choices'] = $choices;
            
        // ajax choices
        }else{
            
            if(!isset($field['ajax_action'])){
                $field['ajax_action'] = 'acf/fields/acfe_taxonomy_terms/query';
            }
    
            $all_terms = array();
            $terms = array_unique($value);
    
            foreach($terms as $term_id){
        
                $term = get_term($term_id);
        
                if(is_a($term, 'WP_Term')){
                    $all_terms[] = $term;
                }
        
            }
    
            if(!empty($all_terms)){
        
                $terms = $this->filter_terms($all_terms, $field);
        
                foreach($terms as $taxonomy => $term){
                    foreach($term as $term_id => $term_name){
                
                        $field['choices'][ $term_id ] = $term_name;
                
                    }
                }
        
            }
            
        }
    
        // allow custom
        if($field['allow_custom']){
            
            foreach($value as $v){
                
                $found = false;
                
                foreach($field['choices'] as $taxonomy => $term){
                    if(isset($term[ $v ])){
                        $found = true;
                        break;
                    }
                }
                
                if(!$found){
                    $field['choices'][ $v ] = $v;
                    $field['custom_choices'][ $v ] = $v;
                }
            
            }
        
        }
        
        // unarray values if radio
        if($field['type'] === 'radio'){
            
            $values = acf_get_array($field['value']);
            
            // check if value exists in choices and select it (in case of allowed terms)
            foreach($values as $value){
                
                if(isset($field['choices'][ $value ])){
                    $field['value'] = $value;
                    break;
                }
                
            }
            
            // unarray
            $field['value'] = acfe_unarray($field['value']);
        
        }
        
        return $field;
        
    }