GFPDFHelperFieldsField_Radio

Controls the display and output of a Gravity Form field.

Defined (1)

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

/src/helper/fields/Field_Radio.php  
  1. class Field_Radio extends Helper_Abstract_Fields { 
  2.  
  3. /** 
  4. * Check the appropriate variables are parsed in send to the parent construct 
  5. * @param object $field The GF_Field_* Object 
  6. * @param array $entry The Gravity Forms Entry 
  7. * @param \GFPDF\Helper\Helper_Abstract_Form $gform 
  8. * @param \GFPDF\Helper\Helper_Misc $misc 
  9. * @throws Exception 
  10. * @since 4.0 
  11. */ 
  12. public function __construct( $field, $entry, Helper_Abstract_Form $gform, Helper_Misc $misc ) { 
  13.  
  14. if ( ! is_object( $field ) || ! $field instanceof GF_Field_Radio ) { 
  15. throw new Exception( '$field needs to be in instance of GF_Field_Radio' ); 
  16.  
  17. /** call our parent method */ 
  18. parent::__construct( $field, $entry, $gform, $misc ); 
  19.  
  20. /** 
  21. * Display the HTML version of this field 
  22. * @param string $value 
  23. * @param bool $label 
  24. * @return string 
  25. * @since 4.0 
  26. */ 
  27. public function html( $value = '', $label = true ) { 
  28. $data = $this->value(); 
  29.  
  30. $value = apply_filters( 'gfpdf_show_field_value', false ); /** Set to `true` to show a field's value instead of the label */ 
  31. $output = ( $value ) ? $data['value'] : $data['label']; 
  32.  
  33. return parent::html( $output ); 
  34.  
  35. /** 
  36. * Checks if the selected Radio button value is defined by the site owner (standard radio options) 
  37. * or by the end user (through the "other" option). 
  38. * @param string $value The user-selected radio button value 
  39. * @return bool Returns true if value is user-defined, or false otherwise 
  40. * @since 4.0.1 
  41. */ 
  42. protected function is_user_defined_value( $value ) { 
  43.  
  44. /** Check if the field has the "Other" choice enabled */ 
  45. if ( ! isset( $this->field->enableOtherChoice ) || true !== $this->field->enableOtherChoice ) { 
  46. return false; 
  47.  
  48. /** Loop through the values and check if we have a match */ 
  49. foreach ( $this->field->choices as $item ) { 
  50. if ( wp_specialchars_decode( $value, ENT_QUOTES ) === $item['value'] ) { 
  51. return false; 
  52.  
  53. return true; 
  54.  
  55. /** 
  56. * Return the HTML form data 
  57. * @return array 
  58. * @since 4.0 
  59. */ 
  60. public function form_data() { 
  61.  
  62. $value = $this->value(); 
  63. $label = GFFormsModel::get_label( $this->field ); 
  64. $data = []; 
  65.  
  66. /** Standadised Format */ 
  67. $data['field'][ $this->field->id . '.' . $label ] = $value['value']; 
  68. $data['field'][ $this->field->id ] = $value['value']; 
  69. $data['field'][ $label ] = $value['value']; 
  70.  
  71. /** Name Format */ 
  72. $data['field'][ $this->field->id . '.' . $label . '_name' ] = $value['label']; 
  73. $data['field'][ $this->field->id . '_name' ] = $value['label']; 
  74. $data['field'][ $label . '_name' ] = $value['label']; 
  75.  
  76. return $data; 
  77.  
  78. /** 
  79. * Get the standard GF value of this field 
  80. * @return array 
  81. * @since 4.0 
  82. */ 
  83. public function value() { 
  84. if ( $this->has_cache() ) { 
  85. return $this->cache(); 
  86.  
  87. $label = esc_html( GFCommon::selection_display( $this->get_value(), $this->field, '', true ) ); 
  88. $value = esc_html( GFCommon::selection_display( $this->get_value(), $this->field ) ); 
  89.  
  90. /** Allow HTML if the radio value isn't the "other" option */ 
  91. if ( ! $this->is_user_defined_value( $value ) ) { 
  92. $value = wp_kses_post( wp_specialchars_decode( $value, ENT_QUOTES ) ); 
  93. $label = wp_kses_post( wp_specialchars_decode( $label, ENT_QUOTES ) ); 
  94.  
  95.  
  96. /** return value / label as an array */ 
  97. $this->cache( [ 
  98. 'value' => $value,  
  99. 'label' => $label,  
  100. ] ); 
  101.  
  102. return $this->cache();