GFPDFHelperFieldsField_Post_Category

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_Category.php  
  1. class Field_Post_Category 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. /** call our parent method */ 
  15. parent::__construct( $field, $entry, $gform, $misc ); 
  16.  
  17. /** 
  18. * Category can be multiple field types 
  19. * eg. Select, MultiSelect, Radio, Dropdown 
  20. */ 
  21. $class = $this->misc->get_field_class( $field->inputType ); 
  22.  
  23. try { 
  24. /** check load our class */ 
  25. if ( class_exists( $class ) ) { 
  26.  
  27. /** See https://gravitypdf.com/documentation/v4/gfpdf_field_class/ for more details about these filters */ 
  28. $this->fieldObject = apply_filters( 'gfpdf_field_class', new $class( $field, $entry, $gform, $misc ), $field, $entry, $this->form ); 
  29. $this->fieldObject = apply_filters( 'gfpdf_field_class_' . $field->inputType, $this->fieldObject, $field, $entry, $this->form ); 
  30. } else { 
  31. throw new Exception( 'Class not found' ); 
  32. } catch ( Exception $e ) { 
  33. /** Exception thrown. Load generic field loader */ 
  34. $this->fieldObject = apply_filters( 'gfpdf_field_default_class', new Field_Default( $field, $entry, $gform, $misc ), $field, $entry, $this->form ); 
  35.  
  36. /** force the fieldObject value cache */ 
  37. $this->value(); 
  38.  
  39. /** 
  40. * Used to check if the current field has a value 
  41. * @since 4.0 
  42. */ 
  43. public function is_empty() { 
  44. return $this->fieldObject->is_empty(); 
  45.  
  46. /** 
  47. * Return the HTML form data 
  48. * @return array 
  49. * @since 4.0 
  50. */ 
  51. public function form_data() { 
  52.  
  53. $field_value = $this->value(); 
  54. $label = GFFormsModel::get_label( $this->field ); 
  55. $data = []; 
  56. $value = [ 
  57. 'value' => '',  
  58. 'label' => '',  
  59. ]; 
  60.  
  61. /** If Radio of Select boxes */ 
  62. if ( ! isset( $field_value[0] ) ) { 
  63.  
  64. /** Set up our basic values */ 
  65. $value['value'] = ( isset( $field_value['value'] ) && ! is_array( $field_value['value'] ) ) ? $field_value['value'] : ''; 
  66. $value['label'] = ( isset( $field_value['label'] ) && ! is_array( $field_value['label'] ) ) ? $field_value['label'] : ''; 
  67.  
  68. } else { /** If Checkboxes or Multiselects */ 
  69.  
  70. /** Loop through the results and store in array */ 
  71. foreach ( $field_value as $item ) { 
  72. $value['value'][] = $item['value']; 
  73. $value['label'][] = $item['label']; 
  74.  
  75. $data['field'][ $this->field->id . '.' . $label ] = $value['value']; 
  76. $data['field'][ $this->field->id ] = $value['value']; 
  77. $data['field'][ $label ] = $value['value']; 
  78.  
  79. /** Name Format */ 
  80. $data['field'][ $this->field->id . '.' . $label . '_name' ] = $value['label']; 
  81. $data['field'][ $this->field->id . '_name' ] = $value['label']; 
  82. $data['field'][ $label . '_name' ] = $value['label']; 
  83.  
  84. return $data; 
  85.  
  86. /** 
  87. * Display the HTML version of this field 
  88. * @param string $value 
  89. * @param bool $label 
  90. * @return string 
  91. * @since 4.0 
  92. */ 
  93. public function html( $value = '', $label = true ) { 
  94. return $this->fieldObject->html(); 
  95.  
  96. /** 
  97. * Get the standard GF value of this field 
  98. * @return string|array 
  99. * @since 4.0 
  100. */ 
  101. public function value() { 
  102. if ( $this->fieldObject->has_cache() ) { 
  103. return $this->fieldObject->cache(); 
  104.  
  105. /** get the value from the correct field object */ 
  106. $items = $this->fieldObject->value(); 
  107.  
  108. /** 
  109. * Standardise the $items format 
  110. * The Radio / Select box will return a single-dimensional array,  
  111. * while checkbox and multiselect will not. 
  112. */ 
  113. $single_dimension = false; 
  114. if ( ! isset( $items[0] ) ) { /** convert single-dimensional array to multi-dimensional */ 
  115. $items = [ $items ]; 
  116. $single_dimension = true; 
  117.  
  118. /** Loop through standardised array and convert the label / value to their appropriate category */ 
  119. foreach ( $items as &$val ) { 
  120.  
  121. /** Process the category label */ 
  122. if ( isset( $val['label'] ) ) { 
  123. $val['label'] = GFCommon::prepare_post_category_value( $val['label'], $this->field ); 
  124.  
  125. if ( is_array( $val['label'] ) ) { 
  126. $val['label'] = ( isset( $val['label'][0] ) ) ? $val['label'][0] : ''; 
  127.  
  128. $val['label'] = esc_html( $val['label'] ); 
  129.  
  130. /** process the category value */ 
  131. if ( isset( $val['value'] ) ) { 
  132. $val['value'] = GFCommon::prepare_post_category_value( $val['value'], $this->field, 'conditional_logic' ); 
  133.  
  134. if ( is_array( $val['value'] ) ) { 
  135. $val['value'] = ( isset( $val['value'][0] ) ) ? $val['value'][0] : ''; 
  136.  
  137. $val['value'] = esc_html( $val['value'] ); 
  138.  
  139. /** 
  140. * Return in the appropriate format. 
  141. * Select / Radio Buttons will not have a multidimensional array 
  142. */ 
  143. if ( $single_dimension ) { 
  144. $items = $items[0]; 
  145.  
  146. /** force the fieldObject cache to be set so it doesn't run their 'value' method directly */ 
  147. $this->fieldObject->cache( $items ); 
  148.  
  149. return $this->fieldObject->cache();