GFPDFHelperFieldsField_Multiselect

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_Multiselect.php  
  1. class Field_Multiselect 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_MultiSelect ) { 
  15. throw new Exception( '$field needs to be in instance of GF_Field_MultiSelect' ); 
  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. $value = $this->value(); 
  28. $label = GFFormsModel::get_label( $this->field ); 
  29. $data = []; 
  30.  
  31. /** Backwards compatibility support for v3 */ 
  32. if ( 0 === sizeof( $value ) ) { 
  33. $data['field'][ $this->field->id . '.' . $label ] = ''; 
  34. $data['field'][ $this->field->id ] = ''; 
  35. $data['field'][ $label ] = ''; 
  36.  
  37. /** Name Format */ 
  38. $data['field'][ $this->field->id . '.' . $label . '_name' ] = ''; 
  39. $data['field'][ $this->field->id . '_name' ] = ''; 
  40. $data['field'][ $label . '_name' ] = ''; 
  41.  
  42. foreach ( $value as $item ) { 
  43.  
  44. /** Standadised Format */ 
  45. $data['field'][ $this->field->id . '.' . $label ][] = $item['value']; 
  46. $data['field'][ $this->field->id ][] = $item['value']; 
  47. $data['field'][ $label ][] = $item['value']; 
  48.  
  49. /** Name Format */ 
  50. $data['field'][ $this->field->id . '.' . $label . '_name' ][] = $item['label']; 
  51. $data['field'][ $this->field->id . '_name' ][] = $item['label']; 
  52. $data['field'][ $label . '_name' ][] = $item['label']; 
  53.  
  54. return $data; 
  55.  
  56. /** 
  57. * Display the HTML version of this field 
  58. * @param string $value 
  59. * @param bool $label 
  60. * @return string 
  61. * @since 4.0 
  62. */ 
  63. public function html( $value = '', $label = true ) { 
  64.  
  65. $value = apply_filters( 'gfpdf_show_field_value', false ); /** Set to `true` to show a field's value instead of the label */ 
  66. $items = $this->value(); 
  67. $html = ''; 
  68.  
  69. if ( sizeof( $items ) > 0 ) { 
  70. $i = 1; 
  71. $html = '<ul class="bulleted multiselect">'; 
  72.  
  73. foreach ( $items as $item ) { 
  74. $sanitized_value = $item['value']; 
  75. $sanitized_option = ( $value ) ? $sanitized_value : $item['label']; 
  76.  
  77. $html .= '<li id="field-' . $this->field->id . '-option-' . $i . '">' . $sanitized_option . '</li>'; 
  78. $i++; 
  79.  
  80. $html .= '</ul>'; 
  81.  
  82. return parent::html( $html ); 
  83.  
  84. /** 
  85. * Get the standard GF value of this field 
  86. * @return string|array 
  87. * @since 4.0 
  88. */ 
  89. public function value() { 
  90. if ( $this->has_cache() ) { 
  91. return $this->cache(); 
  92.  
  93. $value = $this->get_value(); 
  94.  
  95. /** Add support for JSON stored multiselect fields (added to GF2.2) */ 
  96. if ( $this->field->storageType === 'json' ) { 
  97. $value = json_decode( $value, true ); 
  98. $value = ( $value == null ) ? [] : $value; 
  99.  
  100. /** split value into an array */ 
  101. if ( ! is_array( $value ) ) { 
  102. $value = explode( ', ', $value ); 
  103.  
  104. /** remove any empty / unselected fields */ 
  105. $value = array_filter( $value ); 
  106.  
  107. /** loop through array and get the correct selection display value */ 
  108. $items = []; 
  109. foreach ( $value as $item ) { 
  110. $label = esc_html( GFCommon::selection_display( $item, $this->field, '', true ) ); 
  111. $value = esc_html( GFCommon::selection_display( $item, $this->field ) ); 
  112.  
  113. $items[] = [ 
  114. 'value' => $value,  
  115. 'label' => $label,  
  116. ]; 
  117.  
  118. $this->cache( $items ); 
  119.  
  120. return $this->cache();