GFPDFHelperFieldsField_Post_Image

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_Post_Image.php  
  1. class Field_Post_Image 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_Post_Image ) { 
  15. throw new Exception( '$field needs to be in instance of GF_Field_Post_Image' ); 
  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. $value = $this->value(); 
  29.  
  30. /** Start building image link */ 
  31. $html = '<a href="' . $value['url'] . '" target="_blank">'; 
  32. $html .= '<img width="150" src="' . $value['url'] . '" />'; 
  33.  
  34. /** Include title / caption / description if needed */ 
  35. if ( ! empty( $value['title'] ) ) { 
  36. $html .= '<div class="gfpdf-post-image-title">' . $value['title'] . '</div>'; 
  37.  
  38. if ( ! empty( $value['caption'] ) ) { 
  39. $html .= '<div class="gfpdf-post-image-caption">' . $value['caption'] . '</div>'; 
  40.  
  41. if ( ! empty( $value['description'] ) ) { 
  42. $html .= '<div class="gfpdf-post-image-description">' . $value['description'] . '</div>'; 
  43.  
  44. $html .= '</a>'; 
  45.  
  46. return parent::html( $html ); 
  47.  
  48. /** 
  49. * Return the HTML form data 
  50. * @return array 
  51. * @since 4.0 
  52. */ 
  53. public function form_data() { 
  54. $value = $this->value(); 
  55. $label = GFFormsModel::get_label( $this->field ); 
  56.  
  57. if ( 0 === sizeof( $value ) ) { 
  58. $data = []; 
  59.  
  60. $data['field'][ $this->field->id . '.' . $label ] = ''; 
  61. $data['field'][ $this->field->id ] = ''; 
  62. $data['field'][ $label ] = ''; 
  63.  
  64. return $data; 
  65.  
  66. return parent::form_data(); 
  67.  
  68. /** 
  69. * Get the standard GF value of this field 
  70. * @return string|array 
  71. * @since 4.0 
  72. */ 
  73. public function value() { 
  74.  
  75. if ( $this->has_cache() ) { 
  76. return $this->cache(); 
  77.  
  78. $value = $this->get_value(); 
  79. $img = []; 
  80.  
  81. if ( strlen( $value ) > 0 ) { 
  82. $value = explode( '|:|', $this->get_value() ); 
  83.  
  84. $img['url'] = ( isset( $value[0] ) ) ? esc_url( $value[0] ) : ''; 
  85. $img['path'] = ( isset( $value[0] ) ) ? esc_url( $value[0] ) : ''; 
  86. $img['title'] = ( isset( $value[1] ) ) ? esc_html( $value[1] ) : ''; 
  87. $img['caption'] = ( isset( $value[2] ) ) ? esc_html( $value[2] ) : ''; 
  88. $img['description'] = ( isset( $value[3] ) ) ? esc_html( $value[3] ) : ''; 
  89.  
  90. $path = ( isset( $value[0] ) ) ? $this->misc->convert_url_to_path( $value[0] ) : ''; 
  91. if ( $path != $img['url'] ) { 
  92. $img['path'] = $path; 
  93.  
  94. $this->cache( $img ); 
  95.  
  96. return $this->cache();