GravityView  1.19.4
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-list.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-field-list.php
4  * @package GravityView
5  * @subpackage includes\fields
6  * @since 1.14
7  */
8 
9 /**
10  * Add custom options for list fields
11  *
12  * @since 1.14
13  */
15 
16  var $name = 'list';
17 
18  var $search_operators = array( 'contains' );
19 
20  /**
21  * @var bool
22  * @since 1.15.3
23  */
24  var $is_sortable = false;
25 
26  /**
27  * @var bool
28  * @since 1.15.3
29  */
30  var $is_searchable = false;
31 
32  var $_gf_field_class_name = 'GF_Field_List';
33 
34  var $group = 'advanced';
35 
36  function __construct() {
37 
38  $this->label = esc_html__( 'List', 'gravityview' );
39 
40  parent::__construct();
41 
42  add_filter( 'gravityview/template/field_label', array( $this, '_filter_field_label' ), 10, 4 );
43 
44  add_filter( 'gravityview/common/get_form_fields', array( $this, 'add_form_fields' ), 10, 3 );
45  }
46 
47  /**
48  * If a form has list fields, add the columns to the field picker
49  *
50  * @since 1.17
51  *
52  * @param array $fields Associative array of fields, with keys as field type
53  * @param array $form GF Form array
54  * @param bool $include_parent_field Whether to include the parent field when getting a field with inputs
55  *
56  * @return array $fields with list field columns added, if exist. Unmodified if form has no list fields.
57  */
58  function add_form_fields( $fields = array(), $form = array(), $include_parent_field = true ) {
59 
60  $list_fields = GFAPI::get_fields_by_type( $form, 'list' );
61 
62  // Add the list columns
63  foreach ( $list_fields as $list_field ) {
64 
65  if( empty( $list_field->enableColumns ) ) {
66  continue;
67  }
68 
69  $list_columns = array();
70 
71  foreach ( (array)$list_field->choices as $key => $input ) {
72 
73  $input_id = sprintf( '%d.%d', $list_field->id, $key ); // {field_id}.{column_key}
74 
75  $list_columns[ $input_id ] = array(
76  'label' => rgar( $input, 'text' ),
77  'customLabel' => '',
78  'parent' => $list_field,
79  'type' => rgar( $list_field, 'type' ),
80  'adminLabel' => rgar( $list_field, 'adminLabel' ),
81  'adminOnly' => rgar( $list_field, 'adminOnly' ),
82  );
83  }
84 
85  // If there are columns, add them under the parent field
86  if( ! empty( $list_columns ) ) {
87 
88  $index = array_search( $list_field->id, array_keys( $fields ) ) + 1;
89 
90  /**
91  * Merge the $list_columns into the $fields array at $index
92  * @see https://stackoverflow.com/a/1783125
93  */
94  $fields = array_slice( $fields, 0, $index, true) + $list_columns + array_slice( $fields, $index, null, true);
95  }
96 
97  unset( $list_columns, $index, $input_id );
98  }
99 
100  return $fields;
101  }
102 
103  /**
104  * Get the value of a Multiple Column List field for a specific column.
105  *
106  * @since 1.14
107  *
108  * @see GF_Field_List::get_value_entry_detail()
109  *
110  * @param GF_Field_List $field Gravity Forms field
111  * @param string|array $field_value Serialized or unserialized array value for the field
112  * @param int|string $column_id The numeric key of the column (0-index) or the label of the column
113  * @param string $format If set to 'raw', return an array of values for the column. Otherwise, allow Gravity Forms to render using `html` or `text`
114  *
115  * @return array|string|null Returns null if the $field_value passed wasn't an array or serialized array
116  */
117  public static function column_value( GF_Field_List $field, $field_value, $column_id = 0, $format = 'html' ) {
118 
119  $list_rows = maybe_unserialize( $field_value );
120 
121  if( ! is_array( $list_rows ) ) {
122  do_action( 'gravityview_log_error', __METHOD__ . ' - $field_value did not unserialize', $field_value );
123  return null;
124  }
125 
126  $column_values = array();
127 
128  // Each list row
129  foreach ( $list_rows as $list_row ) {
130  $current_column = 0;
131  foreach ( $list_row as $column_key => $column_value ) {
132 
133  // If the label of the column matches $column_id, or the numeric key value matches, add the value
134  if( (string)$column_key === (string)$column_id || ( is_numeric( $column_id ) && (int)$column_id === $current_column ) ) {
135  $column_values[] = $column_value;
136  }
137  $current_column++;
138  }
139  }
140 
141  // Return the array of values
142  if( 'raw' === $format ) {
143  return $column_values;
144  }
145  // Return the Gravity Forms Field output
146  else {
147  return $field->get_value_entry_detail( serialize( $column_values ), '', false, $format );
148  }
149  }
150 
151  /**
152  * When showing a single column values, display the label of the column instead of the field
153  *
154  * @since 1.14
155  *
156  * @param string $label Existing label string
157  * @param array $field GV field settings array, with `id`, `show_label`, `label`, `custom_label`, etc. keys
158  * @param array $form Gravity Forms form array
159  * @param array $entry Gravity Forms entry array
160  *
161  * @return string Existing label if the field isn't
162  */
163  public function _filter_field_label( $label, $field, $form, $entry ) {
164 
165  $field_object = RGFormsModel::get_field( $form, $field['id'] );
166 
167  // Not a list field
168  if( ! $field_object || 'list' !== $field_object->type ) {
169  return $label;
170  }
171 
172  // Custom label is defined, so use it
173  if( ! empty( $field['custom_label'] ) ) {
174  return $label;
175  }
176 
178 
179  // Parent field, not column field
180  if( false === $column_id ) {
181  return $label;
182  }
183 
184  return self::get_column_label( $field_object, $column_id, $label );
185  }
186 
187  /**
188  * Get the column label for the list
189  *
190  * @since 1.14
191  *
192  * @param GF_Field_List $field Gravity Forms List field
193  * @param int $column_id The key of the column (0-index)
194  * @param string $backup_label Backup label to use. Optional.
195  *
196  * @return string
197  */
198  public static function get_column_label( GF_Field_List $field, $column_id, $backup_label = '' ) {
199 
200  // Doesn't have columns enabled
201  if( ! isset( $field->choices ) || ! $field->enableColumns ) {
202  return $backup_label;
203  }
204 
205  // Get the list of columns, with numeric index keys
206  $columns = wp_list_pluck( $field->choices, 'text' );
207 
208  return isset( $columns[ $column_id ] ) ? $columns[ $column_id ] : $backup_label;
209  }
210 
211 }
212 
Modify field settings by extending this class.
_filter_field_label($label, $field, $form, $entry)
When showing a single column values, display the label of the column instead of the field...
gravityview_get_input_id_from_id($field_id= '')
Very commonly needed: get the # of the input based on a full field ID.
add_form_fields($fields=array(), $form=array(), $include_parent_field=true)
If a form has list fields, add the columns to the field picker.
static get_column_label(GF_Field_List $field, $column_id, $backup_label= '')
Get the column label for the list.
Add custom options for list fields.
$column_id
Definition: list.php:18
static column_value(GF_Field_List $field, $field_value, $column_id=0, $format= 'html')
Get the value of a Multiple Column List field for a specific column.
$entry
Definition: notes.php:27
$field_value
Definition: checkbox.php:24
$field
Definition: gquiz_grade.php:11
if(gv_empty($field['value'], false, false)) $format