GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
field-survey-html.php
Go to the documentation of this file.
1 <?php
2 /**
3  * The default survey field output template.
4  *
5  * @global \GV\Template_Context $gravityview
6  * @since 2.0
7  */
8 
9 if ( ! isset( $gravityview ) || empty( $gravityview->template ) ) {
10  gravityview()->log->error( '{file} template loaded without context', array( 'file' => __FILE__ ) );
11  return;
12 }
13 
14 /** @var \GV\Field $field */
15 $field = $gravityview->field;
16 $display_value = $gravityview->display_value;
18 
19 // Backward compatibility for the `score` field setting checkbox before migrating to `choice_display` radio
20 $default_display = $field->score ? 'score' : 'default';
21 
23 
24 switch ( $gravityview->field->field->inputType ) {
25  case 'text':
26  case 'textarea':
27  case 'radio':
28  case 'rank':
29  case 'select':
30  default:
31  echo $display_value;
32  return; // Return early
33  case 'checkbox':
34 
35  // Display the <ul>
36  if ( ! $input_id ) {
37  echo $display_value;
38  return;
39  }
40 
41  if ( 'tick' === $choice_display || 'default' === $choice_display ) {
42  /**
43  * Filter is defined in /templates/fields/field-checkbox-html.php
44  */
45  echo apply_filters( 'gravityview_field_tick', '<span class="dashicons dashicons-yes"></span>', $gravityview->entry, $field->as_configuration(), $gravityview );
46 
47  return; // Return early
48  }
49 
50  echo RGFormsModel::get_choice_text( $field->field, $gravityview->value, $field->ID );
51 
52  return; // Return early
53  case 'likert':
54 
55  if ( class_exists( 'GFSurvey' ) && is_callable( array('GFSurvey', 'get_instance') ) ) {
56  wp_register_style( 'gsurvey_css', GFSurvey::get_instance()->get_base_url() . '/css/gsurvey.css' );
57  wp_print_styles( 'gsurvey_css' );
58  }
59 
60  // Gravity Forms-generated Likert table output
61  if ( 'default' === $choice_display || empty( $choice_display ) ) {
62 
63  // Default is the likert table; show it and return early.
64  if( $field->field->gsurveyLikertEnableMultipleRows && ! $input_id ) {
65  echo $display_value;
66  return; // Return early
67  }
68  }
69 
70  // Force the non-multirow fields into the same formatting (row:column)
71  $raw_value = is_array( $gravityview->value ) ? $gravityview->value : array( $field->ID => ':' . $gravityview->value );
72 
73  $output_values = array();
74  foreach( $raw_value as $row => $row_values ) {
75  list( $_likert_row, $row_value ) = array_pad( explode( ':', $row_values ), 2, '' );
76 
77  // If we're displaying a single row, don't include other row values
78  if ( $input_id && $row !== $field->ID ) {
79  continue;
80  }
81 
82  switch( $choice_display ) {
83  case 'score':
84  $output_values[] = GravityView_Field_Survey::get_choice_score( $field->field, $row_value, $row );
85  break;
86  case 'text':
87  $output_values[] = RGFormsModel::get_choice_text( $field->field, $row_value, $row );
88  break;
89  case 'default':
90  default:
91  // When displaying a single input, render as if multiple rows were disabled
92  /** @var GF_Field_Likert $single_input_field */
93  $single_input_field = clone $field->field;
94  $single_input_field->id = $field->ID;
95  $single_input_field->gsurveyLikertEnableMultipleRows = false;
96  $output_values[] = $single_input_field->get_field_input( array( 'id' => $field->form_id ), $row_value );
97  break;
98  }
99  }
100 
101  /**
102  * @filter `gravityview/template/field/survey/glue` The value used to separate multiple values in the Survey field output
103  * @since 2.10.4
104  *
105  * @param string The glue. Default: "; " (semicolon with a trailing space)
106  * @param \GV\Template_Context The context.
107  */
108  $glue = apply_filters( 'gravityview/template/field/survey/glue', '; ', $gravityview );
109 
110  echo implode( $glue, $output_values );
111 
112  return; // Return early
113 
114  case 'rating':
115 
116  $choice_text = RGFormsModel::get_choice_text( $field->field, $gravityview->value, $input_id );
117 
118  if( ! in_array( $choice_display, array( 'stars', 'dashicons', 'emoji' ), true ) ) {
119  echo $choice_text;
120  return;
121  }
122 
123  $choices = $field->field->choices;
124  $choice_values = wp_list_pluck( $choices, 'value', $gravityview->value );
125  $starred_index = array_search( $gravityview->value, $choice_values );
126  $star_a11y_label = sprintf( __( '%s (%d out of %d stars)', 'gk-gravityview'), $choice_text, ( $starred_index + 1 ), sizeof( $choice_values ) );
127 
128  /**
129  * @action `gravityview/field/survey/rating-styles`
130  * @usedby {@see GravityView_Field_Survey::output_frontend_css} to Enqueue styles for the Survey field.
131  * @since 2.16
132  * @param \GV\GF_Field $field The current field.
133  * @param \GV\Template_Context $gravityview The context.
134  */
135  do_action( 'gravityview/template/field/survey/rating/before', $field, $gravityview );
136 
137  echo '<span class="gv-field-survey-screen-reader-text">' . esc_html( $star_a11y_label ) . '</span>';
138  foreach ( $choices as $current_index => $choice_value ) {
139 
140  // Have we already shown the last filled-in star?
141  $empty = ( $current_index > $starred_index );
142  $css_class = 'gv-field-survey-star-' . ( $empty ? 'empty' : 'filled' );
143 
144  echo sprintf( '<span class="%s" title="%s"></span>', esc_attr( $css_class ), esc_attr( $choice_value['text'] ) );
145  }
146 
147 
148  return;
149 }
$display_value
$choice_display
gravityview_get_input_id_from_id( $field_id='')
Very commonly needed: get the # of the input based on a full field ID.
$default_display
static get_choice_score( $field, $value, $input_id=0)
Returns the score for a choice at $value.
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
gravityview()
The main GravityView wrapper function.