GFPDFHelperFieldsField_Address

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_Address.php  
  1. class Field_Address 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_Address ) { 
  15. throw new Exception( '$field needs to be in instance of GF_Field_Address' ); 
  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(); /** remove any empty fields from the array */ 
  29. $address = []; 
  30.  
  31. /** check if we should display the zip before the city */ 
  32. $address_display_format = apply_filters( 'gform_address_display_format', 'default' ); 
  33.  
  34. /** Start putting our address together */ 
  35. if ( ! empty( $data['street'] ) ) { 
  36. $address[] = $data['street']; 
  37.  
  38. if ( ! empty( $data['street2'] ) ) { 
  39. $address[] = $data['street2']; 
  40.  
  41. /** display in the standard "city, state zip" format */ 
  42. if ( $address_display_format != 'zip_before_city' ) { 
  43. $zip_string = $data['city']; 
  44. $zip_string .= ( ! empty( $zip_string ) && ! empty( $data['state'] ) ) ? ", {$data['state']}" : trim( $data['state'] ); 
  45. $zip_string .= " {$data['zip']}"; 
  46.  
  47. $zip_string = trim( $zip_string ); 
  48.  
  49. if ( ! empty( $zip_string ) ) { 
  50. $address[] = $zip_string; 
  51. } else { /** display in the "zip, city state" format */ 
  52. $zip_string = trim( $data['zip'] . ' ' . $data['city'] ); 
  53. $zip_string .= ( ! empty( $zip_string ) && ! empty( $data['state'] ) ) ? ", {$data['state']}" : trim( $data['state'] ); 
  54.  
  55. if ( ! empty( $zip_string ) ) { 
  56. $address[] = $zip_string; 
  57.  
  58. /** add country to address, if present */ 
  59. if ( ! empty( $data['country'] ) ) { 
  60. $address[] = $data['country']; 
  61.  
  62. /** display the address in the correct format */ 
  63. $html = implode( '<br />', $address ); 
  64.  
  65. /** return the results */ 
  66.  
  67. return parent::html( $html ); 
  68.  
  69. /** 
  70. * Check if the Address field is empty, based off the active sub-form fields 
  71. * This prevents problems with addresses showing when country or state fields are disabled but defaults are used 
  72. * return boolean Return true if the field is empty, false if it has a value 
  73. * @since 4.0 
  74. */ 
  75. public function is_empty() { 
  76. $field = $this->field; 
  77. $value = $this->get_value(); 
  78.  
  79. /** Loop through active fields and determine if field should be classed as empty */ 
  80. foreach ( $field->inputs as $item ) { 
  81. if ( ! isset( $item['isHidden'] ) || false === $item['isHidden'] ) { 
  82. /** Now we know item isn't hidden, go through the values and check if there's data */ 
  83. $item_value = trim( rgget( (string) $item['id'], $value ) ); 
  84. if ( ! empty( $item_value ) ) { 
  85. return false; 
  86.  
  87. return true; 
  88.  
  89. /** 
  90. * Get the standard GF value of this field 
  91. * @return array 
  92. * @since 4.0 
  93. */ 
  94. public function value() { 
  95. if ( $this->has_cache() ) { 
  96. return $this->cache(); 
  97.  
  98. $value = $this->get_value(); 
  99.  
  100. /** check if the returned results are an array */ 
  101. if ( ! is_array( $value ) ) { 
  102. $value[ $this->field->id . '.1' ] = $value; /** set to the street value */ 
  103.  
  104. $this->cache( [ 
  105. 'street' => esc_html( rgget( $this->field->id . '.1', $value ) ),  
  106. 'street2' => esc_html( rgget( $this->field->id . '.2', $value ) ),  
  107. 'city' => esc_html( rgget( $this->field->id . '.3', $value ) ),  
  108. 'state' => esc_html( rgget( $this->field->id . '.4', $value ) ),  
  109. 'zip' => esc_html( rgget( $this->field->id . '.5', $value ) ),  
  110. 'country' => esc_html( rgget( $this->field->id . '.6', $value ) ),  
  111. ] ); 
  112.  
  113. return $this->cache();