GFPDFHelperFieldsField_Fileupload

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_Fileupload.php  
  1. class Field_Fileupload 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_FileUpload ) { 
  15. throw new Exception( '$field needs to be in instance of GF_Field_FileUpload' ); 
  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. $value = $this->value(); 
  30. $field_id = $this->field->id; 
  31.  
  32. /** Backwards compatibility support for v3 */ 
  33. if ( 0 === sizeof( $value ) ) { 
  34. $data[ $field_id . '.' . $label ] = []; 
  35. $data[ $field_id ] = []; 
  36. $data[ $label ] = []; 
  37.  
  38. /** Path Format */ 
  39. $data[ $field_id . '_path' ] = []; 
  40. $data[ $field_id . '.' . $label . '_path' ] = []; 
  41.  
  42. foreach ( $value as $image ) { 
  43.  
  44. $data[ $field_id . '.' . $label ][] = $image; 
  45. $data[ $field_id ][] = $image; 
  46. $data[ $label ][] = $image; 
  47.  
  48. $path = $this->misc->convert_url_to_path( $image ); 
  49.  
  50. $data[ $field_id . '_path' ][] = $path; 
  51. $data[ $field_id . '.' . $label . '_path' ][] = $path; 
  52.  
  53. return [ 'field' => $data ]; 
  54.  
  55. /** 
  56. * Display the HTML version of this field 
  57. * @param string $value 
  58. * @param bool $label 
  59. * @return string 
  60. * @since 4.0 
  61. */ 
  62. public function html( $value = '', $label = true ) { 
  63. $files = $this->value(); 
  64. $html = ''; 
  65.  
  66. if ( sizeof( $files ) > 0 ) { 
  67. $html = '<ul class="bulleted fileupload">'; 
  68. $i = 1; 
  69.  
  70. foreach ( $files as $file ) { 
  71. $file_info = pathinfo( $file ); 
  72. $html .= '<li id="field-' . $this->field->id . '-option-' . $i . '"><a href="' . esc_url( $file ) . '">' . esc_html( $file_info['basename'] ) . '</a></li>'; 
  73. $i++; 
  74.  
  75. $html .= '</ul>'; 
  76.  
  77. return parent::html( $html ); 
  78.  
  79. /** 
  80. * Get the standard GF value of this field 
  81. * @return string|array 
  82. * @since 4.0 
  83. */ 
  84. public function value() { 
  85. if ( $this->has_cache() ) { 
  86. return $this->cache(); 
  87.  
  88. $value = $this->get_value(); 
  89. $files = []; 
  90.  
  91. if ( ! empty( $value ) ) { 
  92. $paths = ( $this->field->multipleFiles ) ? json_decode( $value ) : [ $value ]; 
  93.  
  94. if ( is_array( $paths ) && sizeof( $paths ) > 0 ) { 
  95. foreach ( $paths as $path ) { 
  96. $files[] = esc_url( $path ); 
  97.  
  98. $this->cache( $files ); 
  99.  
  100. return $this->cache();