GFPDFHelperFieldsField_Likert

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_Likert.php  
  1. class Field_Likert extends Helper_Abstract_Fields { 
  2.  
  3. /** 
  4. * Used to check if the current field has a value 
  5. * @since 4.0 
  6. */ 
  7. public function is_empty() { 
  8.  
  9. $value = $this->value(); 
  10.  
  11. if ( isset( $value['row'] ) ) { /** Check for single row likerts */ 
  12. if ( sizeof( array_filter( $value['row'] ) ) === 0 ) { /** if empty */ 
  13. return true; 
  14. } else { /** 
  15. multi row likert */ 
  16. /** loop through the results and check if they are all empty */ 
  17. $empty = true; 
  18.  
  19. foreach ( $value['rows'] as $row ) { 
  20. if ( sizeof( array_filter( $row ) ) > 0 ) { 
  21. $empty = false; 
  22. break; 
  23.  
  24. return $empty; 
  25.  
  26. return false; 
  27.  
  28. /** 
  29. * Return the HTML form data 
  30. * @return array 
  31. * @since 4.0 
  32. */ 
  33. public function form_data() { 
  34.  
  35. $data = []; 
  36. $value = $this->value(); 
  37.  
  38. $data['survey']['likert'][ $this->field->id ] = $value; 
  39.  
  40. return $data; 
  41.  
  42. /** 
  43. * Display the HTML version of this field 
  44. * @param string $value 
  45. * @param bool $label 
  46. * @return string 
  47. * @since 4.0 
  48. */ 
  49. public function html( $value = '', $label = true ) { 
  50.  
  51. $html = GFCommon::get_lead_field_display( $this->field, $this->get_value(), $this->entry['currency'] ); 
  52. $html = apply_filters( 'gform_entry_field_value', $html, $this->field, $this->entry, $this->form ); 
  53.  
  54. return parent::html( $html ); 
  55.  
  56. /** 
  57. * Get the standard GF value of this field 
  58. * @return string|array 
  59. * @since 4.0 
  60. */ 
  61. public function value() { 
  62. if ( $this->has_cache() ) { 
  63. return $this->cache(); 
  64.  
  65. /** 
  66. * Process Single and Multi Column Likerts 
  67. */ 
  68. $likert = []; 
  69.  
  70. /** 
  71. * Get the column names 
  72. */ 
  73. foreach ( $this->field->choices as $column ) { 
  74. $likert['col'][ $column['value'] ] = esc_html( $column['text'] ); 
  75.  
  76. /** 
  77. * Build our Likert Array 
  78. */ 
  79. if ( is_array( $this->field->inputs ) && sizeof( $this->field->inputs ) > 0 ) { /** Handle our multirow likert */ 
  80.  
  81. /** loop through each row */ 
  82. foreach ( $this->field->inputs as $row ) { 
  83. /** loop through each column */ 
  84. foreach ( $likert['col'] as $id => $text ) { 
  85. /** check if user selected this likert value */ 
  86. $data = rgar( $this->entry, $row['id'] ); 
  87.  
  88. $likert['rows'][ $row['label'] ][ $text ] = ( ( $row['name'] . ':' . $id ) == $data ) ? 'selected' : ''; 
  89. } else { /** Handle our single-row likert */ 
  90.  
  91. /** Get the value from the entry */ 
  92. $data = rgar( $this->entry, $this->field->id ); 
  93. foreach ( $likert['col'] as $id => $text ) { 
  94. /** check if user selected this likert value */ 
  95. $likert['row'][ $text ] = ( $id == $data ) ? 'selected' : ''; 
  96.  
  97. $this->cache( $likert ); 
  98.  
  99. return $this->cache();