PluginProbe
Advanced Forms for ACF / trunk
Advanced Forms for ACF vtrunk
1.9.0 1.9.1 1.9.2.1 1.9.3 1.9.3.1 1.9.3.2 1.9.3.3 1.9.3.4 1.9.3.5 1.9.3.6 1.9.3.7 1.9.3.8 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.3.1 1.0.3.2 1.0.3.3 1.0.4 1.1.0 1.1.1 1.2.0 1.3.0 All 51 releases
advanced-forms / acf / fields / field_select.php

field_select.php in Advanced Forms for ACF trunk, at acf/fields/field_select.php

344 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Based on the default ACF select field
5 *
6 * @since 1.3.2
7 *
8 */
9
10 if( ! class_exists('AF_Form_Field_Picker') ) :
11
12 /**
13 * Custom private ACF field used for picking a form field.
14 *
15 * @since 1.3.2
16 *
17 */
18 class AF_Form_Field_Picker extends acf_field {
19
20
21 /**
22 * Set up field defaults
23 *
24 * @since 1.3.2
25 *
26 */
27 function __construct() {
28
29 // vars
30 $this->name = 'field_picker';
31 $this->label = _x('Field picker', 'noun', 'acf');
32 $this->public = false;
33 $this->defaults = array(
34 'allow_null' => 0,
35 'allow_custom' => 0,
36 'field_types' => 'regular',
37 'choices' => array(),
38 'default_value' => array(
39 'field' => false,
40 'format' => false,
41 ),
42 'placeholder' => '',
43 );
44
45
46 // do not delete!
47 parent::__construct();
48
49 }
50
51
52 /**
53 * Render interface for field
54 *
55 * @since 1.3.2
56 *
57 */
58 function render_field( $field ) {
59
60 global $post;
61
62 $field['value'] = wp_parse_args( $field['value'], array(
63 'field' => false,
64 'format' => false,
65 ));
66
67
68 if ( $field['allow_custom'] ) {
69 $field['choices']['custom'] = 'Custom format';
70 }
71
72 $form_key = get_post_meta( $post->ID, 'form_key', true );
73 $form = af_get_form( $form_key );
74
75 if ( $post && $form_key ) {
76
77 $field['choices']['Fields'] = _af_form_field_choices( $form_key, $field['field_types'] );
78
79 }
80
81
82 // placeholder
83 if( empty($field['placeholder']) ) {
84
85 $field['placeholder'] = _x('Select', 'verb', 'acf');
86
87 }
88
89
90 // add empty value (allows '' to be selected)
91 if( empty($field['value']) ) {
92
93 $field['value'] = array('');
94
95 }
96
97
98 // allow null
99 // - have tried array_merge but this causes keys to re-index if is numeric (post ID's)
100 if ( $field['allow_null'] ) {
101
102 $prepend = array('' => '- ' . $field['placeholder'] . ' -');
103 $field['choices'] = $prepend + $field['choices'];
104
105 }
106
107
108 // vars
109 $atts = array(
110 'id' => $field['id'],
111 'class' => $field['class'],
112 'name' => $field['name'] . '[field]',
113 'data-placeholder' => $field['placeholder'],
114 'data-allow_null' => $field['allow_null']
115 );
116
117
118 // special atts
119 foreach( array( 'readonly', 'disabled' ) as $k ) {
120
121 if( !empty($field[ $k ]) ) $atts[ $k ] = $k;
122
123 }
124
125
126
127 // open
128 echo '<select ' . acf_esc_atts($atts) . '>';
129
130
131 // walk
132 $this->walk( $field['choices'], $field['value']['field'] );
133
134
135 // close
136 echo '</select>';
137
138 if ( $field['allow_custom'] ) {
139
140 echo '<div class="acf-input format-input" style="display: none; margin-top: 10px;">';
141
142 $format_atts = array(
143 'type' => 'text',
144 'name' => $field['name'] . '[format]',
145 'value' => $field['value']['format'] ?: '',
146 );
147
148 echo '<input ' . acf_esc_atts( $format_atts ) . '/>';
149
150 _af_field_inserter_button( $form, 'all', true );
151
152 echo '</div>';
153
154 }
155
156 }
157
158
159 /**
160 * Walk and output select options
161 *
162 * @since 1.3.2
163 *
164 */
165 function walk( $choices, $value ) {
166
167 // bail ealry if no choices
168 if( empty($choices) ) return;
169
170
171 // loop
172 foreach( $choices as $k => $v ) {
173
174 // optgroup
175 if( is_array($v) ){
176
177 // optgroup
178 echo '<optgroup label="' . esc_attr($k) . '">';
179
180
181 // walk
182 $this->walk( $v, $value );
183
184
185 // close optgroup
186 echo '</optgroup>';
187
188
189 // break
190 continue;
191
192 }
193
194
195 // vars
196 $search = html_entity_decode($k);
197 $atts = array( 'value' => $k );
198
199
200 // validate selected
201 if( $search == $value ) {
202
203 $atts['selected'] = 'selected';
204
205 }
206
207
208 // option
209 echo '<option ' . acf_esc_atts($atts) . '>' . $v . '</option>';
210
211 }
212
213 }
214
215
216 function validate_value( $valid, $value, $field, $input ) {
217
218 if ( empty( $value['field'] ) && $field['required'] ) {
219 return sprintf( __( '%s value is required', 'acf' ), $field['label'] );
220 }
221
222 return $valid;
223 }
224
225
226 /*
227 * load_value()
228 *
229 * This filter is applied to the $value after it is loaded from the db
230 *
231 * @type filter
232 * @since 3.6
233 * @date 23/01/13
234 *
235 * @param $value (mixed) the value found in the database
236 * @param $post_id (mixed) the $post_id from which the value was loaded
237 * @param $field (array) the field array holding all the field options
238 * @return $value
239 */
240
241 function load_value( $value, $post_id, $field ) {
242
243 // ACF4 null
244 if( $value === 'null' ) return false;
245
246
247 // return
248 return $value;
249 }
250
251
252 /*
253 * update_value()
254 *
255 * This filter is appied to the $value before it is updated in the db
256 *
257 * @type filter
258 * @since 3.6
259 * @date 23/01/13
260 *
261 * @param $value - the value which will be saved in the database
262 * @param $post_id - the $post_id of which the value will be saved
263 * @param $field - the field array holding all the field options
264 *
265 * @return $value - the modified value
266 */
267
268 function update_value( $value, $post_id, $field ) {
269
270 // validate
271 if( empty($value) ) {
272
273 return $value;
274
275 }
276
277
278 // return
279 return $value;
280 }
281
282
283 /*
284 * translate_field
285 *
286 * This function will translate field settings
287 *
288 * @type function
289 * @date 8/03/2016
290 * @since 5.3.2
291 *
292 * @param $field (array)
293 * @return $field
294 */
295
296 function translate_field( $field ) {
297
298 // translate
299 $field['choices'] = acf_translate( $field['choices'] );
300
301
302 // return
303 return $field;
304
305 }
306
307
308 /*
309 * format_value()
310 *
311 * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
312 *
313 * @type filter
314 * @since 3.6
315 * @date 23/01/13
316 *
317 * @param $value (mixed) the value which was loaded from the database
318 * @param $post_id (mixed) the $post_id from which the value was loaded
319 * @param $field (array) the field array holding all the field options
320 *
321 * @return $value (mixed) the modified value
322 */
323
324 function format_value( $value, $post_id, $field ) {
325
326 $value = wp_parse_args( $value, array(
327 'field' => false,
328 'format' => false,
329 ));
330
331 // return
332 return $value;
333
334 }
335
336 }
337
338
339 // initialize
340 acf_register_field_type( new AF_Form_Field_Picker() );
341
342 endif; // class_exists check
343
344 ?>