GFPDFHelperFieldsField_List

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_List.php  
  1. class Field_List 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_List ) { 
  15. throw new Exception( '$field needs to be in instance of GF_Field_List' ); 
  16.  
  17. /** call our parent method */ 
  18. parent::__construct( $field, $entry, $gform, $misc ); 
  19.  
  20. /** 
  21. * Return the HTML form data 
  22. * @return array 
  23. * @since 4.0 
  24. */ 
  25. public function form_data() { 
  26.  
  27. $data = []; 
  28. $label = GFFormsModel::get_label( $this->field ); 
  29. $html = $this->html(); 
  30.  
  31. /** Add our List array */ 
  32. $list_array = $this->value(); 
  33. $list_array = ( 0 < sizeof( $list_array ) ) ? $list_array : ''; 
  34.  
  35. $data['list'][ $this->field->id ] = $list_array; 
  36.  
  37. /** Add our List HTML */ 
  38. $data['field'][ $this->field->id . '.' . $label ] = $html; 
  39. $data['field'][ $this->field->id ] = $html; 
  40. $data['field'][ $label ] = $html; 
  41.  
  42. return $data; 
  43.  
  44. /** 
  45. * Display the HTML version of this field 
  46. * @param string $value 
  47. * @param bool $label 
  48. * @return string 
  49. * @since 4.0 
  50. */ 
  51. public function html( $value = '', $label = true ) { 
  52.  
  53. /** exit early if list field is empty */ 
  54. if ( $this->is_empty() ) { 
  55. return parent::html( '' ); 
  56.  
  57. /** get out field value */ 
  58. $value = $this->value(); 
  59. $columns = is_array( $value[0] ); 
  60.  
  61. /** Start buffer and generate a list table */ 
  62. ob_start(); 
  63. ?> 
  64.  
  65. <table autosize="1" class="gfield_list"> 
  66.  
  67. <!-- Loop through the column names and output in a header (if using the advanced list) --> 
  68. <?php if ( $columns ) : $columns = array_keys( $value[0] ); ?> 
  69. <tbody class="head"> 
  70. <tr> 
  71. <?php foreach ( $columns as $column ) : ?> 
  72. <th> 
  73. <?php echo esc_html( $column ); ?> 
  74. </th> 
  75. <?php endforeach; ?> 
  76. </tr> 
  77. </tbody> 
  78. <?php endif; ?> 
  79.  
  80. <!-- Loop through each row --> 
  81. <tbody class="contents"> 
  82. <?php foreach ( $value as $item ) : ?> 
  83. <tr> 
  84. <!-- handle the basic list --> 
  85. <?php if ( ! $columns ) : ?> 
  86. <td><?php echo esc_html( $item ); ?></td> 
  87. <?php else : ?><!-- handle the advanced list --> 
  88. <?php foreach ( $columns as $column ) : ?> 
  89. <td> 
  90. <?php echo esc_html( rgar( $item, $column ) ); ?> 
  91. </td> 
  92. <?php endforeach; ?> 
  93. <?php endif; ?> 
  94. </tr> 
  95. <?php endforeach; ?> 
  96. </tbody> 
  97.  
  98. </table> 
  99.  
  100. <?php 
  101. /** get buffer and return HTML */ 
  102.  
  103. return parent::html( ob_get_clean() ); 
  104.  
  105. /** 
  106. * Get the standard GF value of this field 
  107. * @return string|array 
  108. * @since 4.0 
  109. */ 
  110. public function value() { 
  111. if ( $this->has_cache() ) { 
  112. return $this->cache(); 
  113.  
  114. $value = maybe_unserialize( $this->get_value() ); 
  115.  
  116. /** make sure value is an array */ 
  117. if ( ! is_array( $value ) ) { 
  118. $value = [ $value ]; 
  119.  
  120. /** Remove empty rows */ 
  121. $value = $this->remove_empty_list_rows( $value ); 
  122.  
  123. $this->cache( $value ); 
  124.  
  125. return $this->cache(); 
  126.  
  127. /** 
  128. * Remove empty list rows 
  129. * @param array $list The current list array 
  130. * @return array The filtered list array 
  131. * @since 4.0 
  132. */ 
  133. private function remove_empty_list_rows( $list ) { 
  134.  
  135. /** if list field empty return early */ 
  136. if ( ! is_array( $list ) || sizeof( $list ) === 0 ) { 
  137. return $list; 
  138.  
  139. /** If single list field */ 
  140. if ( ! is_array( $list[0] ) ) { 
  141. $list = array_filter( $list ); 
  142. $list = array_map( 'esc_html', $list ); 
  143. } else { 
  144.  
  145. /** Loop through the multi-column list */ 
  146. foreach ( $list as $id => &$row ) { 
  147.  
  148. $empty = true; 
  149.  
  150. foreach ( $row as &$col ) { 
  151.  
  152. /** Check if there is data and if so break the loop */ 
  153. if ( strlen( trim( $col ) ) > 0 ) { 
  154. $col = esc_html( $col ); 
  155. $empty = false; 
  156.  
  157. /** Remove row from list */ 
  158. if ( $empty ) { 
  159. unset( $list[ $id ] ); 
  160.  
  161. return $list;