CMB2_Type_Taxonomy_Radio

CMB taxonomy_radio field type.

Defined (1)

The class is defined in the following location(s).

/includes/types/CMB2_Type_Taxonomy_Radio.php  
  1. class CMB2_Type_Taxonomy_Radio extends CMB2_Type_Taxonomy_Base { 
  2.  
  3. public function render() { 
  4. $field = $this->field; 
  5. $names = $this->get_object_terms(); 
  6.  
  7. $saved_term = is_wp_error( $names ) || empty( $names ) ? $field->get_default() : $names[key( $names )]->slug; 
  8. $terms = $this->get_terms(); 
  9. $options = ''; 
  10. $i = 1; 
  11.  
  12. if ( ! $terms ) { 
  13. $options .= sprintf( '<li><label>%s</label></li>', esc_html( $this->_text( 'no_terms_text', esc_html__( 'No terms', 'cmb2' ) ) ) ); 
  14. } else { 
  15. $option_none = $field->args( 'show_option_none' ); 
  16. if ( ! empty( $option_none ) ) { 
  17.  
  18. $field_id = $this->_id(); 
  19.  
  20. /** 
  21. * Default (option-none) taxonomy-radio value. 
  22. * @since 1.3.0 
  23. * @param string $option_none_value Default (option-none) taxonomy-radio value. 
  24. */ 
  25. $option_none_value = apply_filters( 'cmb2_taxonomy_radio_default_value', '' ); 
  26.  
  27. /** 
  28. * Default (option-none) taxonomy-radio value. 
  29. * The dynamic portion of the hook name, $field_id, refers to the field id attribute. 
  30. * @since 1.3.0 
  31. * @param string $option_none_value Default (option-none) taxonomy-radio value. 
  32. */ 
  33. $option_none_value = apply_filters( "cmb2_taxonomy_radio_{$field_id}_default_value", $option_none_value ); 
  34.  
  35. $args = array( 
  36. 'value' => $option_none_value,  
  37. 'label' => $option_none,  
  38. ); 
  39. if ( $saved_term == $option_none_value ) { 
  40. $args['checked'] = 'checked'; 
  41. $options .= $this->list_input( $args, $i ); 
  42. $i++; 
  43.  
  44. foreach ( $terms as $term ) { 
  45. $args = array( 
  46. 'value' => $term->slug,  
  47. 'label' => $term->name,  
  48. ); 
  49.  
  50. if ( $saved_term == $term->slug ) { 
  51. $args['checked'] = 'checked'; 
  52. $options .= $this->list_input( $args, $i ); 
  53. $i++; 
  54.  
  55. return $this->rendered( 
  56. $this->types->radio( array( 'options' => $options ), 'taxonomy_radio' ) 
  57. );