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