|
| 1 | +<?php |
| 2 | + |
| 3 | +if( ! class_exists('acf_field_dynamic_select_extension') ) : |
| 4 | + |
| 5 | + class acf_field_dynamic_select_extension extends acf_field_select { |
| 6 | + |
| 7 | + function render_field( $field ) { |
| 8 | + |
| 9 | + // convert |
| 10 | + $value = acf_get_array($field['value']); |
| 11 | + |
| 12 | + $field_key = explode('.', $field['field_key']); |
| 13 | + |
| 14 | + if( $field_key[0] == 'options'){ |
| 15 | + |
| 16 | + $other_field_values = get_field($field_key[1], 'options'); |
| 17 | + array_shift($field_key); |
| 18 | + } |
| 19 | + else{ |
| 20 | + |
| 21 | + $other_field_values = get_field($field_key[0]); |
| 22 | + } |
| 23 | + |
| 24 | + $choices = array(); |
| 25 | + |
| 26 | + if( count($field_key) == 2 ){ |
| 27 | + |
| 28 | + foreach ($other_field_values as $other_field_value){ |
| 29 | + |
| 30 | + $choice = $other_field_value[$field_key[1]]??''; |
| 31 | + |
| 32 | + if( strlen($choice) ){ |
| 33 | + |
| 34 | + $choice = $other_field_value[$field_key[1]]; |
| 35 | + $choices[sanitize_title($choice)] = $choice; |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // placeholder |
| 41 | + if( empty($field['placeholder']) ) { |
| 42 | + $field['placeholder'] = _x('Select', 'verb', 'acf'); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + // add empty value (allows '' to be selected) |
| 47 | + if( empty($value) ) { |
| 48 | + $value = array(''); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + // prepend empty choice |
| 53 | + // - only for single selects |
| 54 | + // - have tried array_merge but this causes keys to re-index if is numeric (post ID's) |
| 55 | + if( $field['allow_null'] && !$field['multiple'] ) { |
| 56 | + $choices = array( '' => "- {$field['placeholder']} -" ) + $choices; |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + // vars |
| 61 | + $select = array( |
| 62 | + 'id' => $field['id'], |
| 63 | + 'class' => $field['class'], |
| 64 | + 'name' => $field['name'], |
| 65 | + 'data-ui' => $field['ui'], |
| 66 | + 'data-ajax' => $field['ajax'], |
| 67 | + 'data-multiple' => $field['multiple'], |
| 68 | + 'data-placeholder' => $field['placeholder'], |
| 69 | + 'data-allow_null' => $field['allow_null'] |
| 70 | + ); |
| 71 | + |
| 72 | + |
| 73 | + // multiple |
| 74 | + if( $field['multiple'] ) { |
| 75 | + |
| 76 | + $select['multiple'] = 'multiple'; |
| 77 | + $select['size'] = 5; |
| 78 | + $select['name'] .= '[]'; |
| 79 | + |
| 80 | + // Reduce size to single line if UI. |
| 81 | + if( $field['ui'] ) { |
| 82 | + $select['size'] = 1; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + // special atts |
| 88 | + if( !empty($field['readonly']) ) $select['readonly'] = 'readonly'; |
| 89 | + if( !empty($field['disabled']) ) $select['disabled'] = 'disabled'; |
| 90 | + if( !empty($field['ajax_action']) ) $select['data-ajax_action'] = $field['ajax_action']; |
| 91 | + |
| 92 | + |
| 93 | + // hidden input is needed to allow validation to see <select> element with no selected value |
| 94 | + if( $field['multiple'] || $field['ui'] ) { |
| 95 | + acf_hidden_input(array( |
| 96 | + 'id' => $field['id'] . '-input', |
| 97 | + 'name' => $field['name'] |
| 98 | + )); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + // append |
| 103 | + $select['value'] = $value; |
| 104 | + $select['choices'] = $choices; |
| 105 | + |
| 106 | + |
| 107 | + // render |
| 108 | + acf_select_input( $select ); |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + /* |
| 113 | + * initialize |
| 114 | + * |
| 115 | + * This function will setup the field type data |
| 116 | + * |
| 117 | + * @type function |
| 118 | + * @date 10/01/2019 |
| 119 | + * @since 5.0.0 |
| 120 | + * |
| 121 | + * @param n/a |
| 122 | + * @return n/a |
| 123 | + */ |
| 124 | + |
| 125 | + function initialize() { |
| 126 | + |
| 127 | + // vars |
| 128 | + $this->name = 'dynamic_select'; |
| 129 | + $this->label = __("Dynamic select",'acf'); |
| 130 | + $this->category = 'choice'; |
| 131 | + |
| 132 | + $this->defaults = array( |
| 133 | + 'multiple' => 0, |
| 134 | + 'allow_null' => 0, |
| 135 | + 'choices' => array(), |
| 136 | + 'default_value' => '', |
| 137 | + 'ui' => 0, |
| 138 | + 'ajax' => 0, |
| 139 | + 'placeholder' => '', |
| 140 | + 'return_format' => 'value' |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + /* |
| 146 | + * render_field_settings() |
| 147 | + * |
| 148 | + * Create extra options for your field. This is rendered when editing a field. |
| 149 | + * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 150 | + * |
| 151 | + * @type action |
| 152 | + * @since 3.6 |
| 153 | + * @date 23/01/13 |
| 154 | + * |
| 155 | + * @param $field - an array holding all the field's data |
| 156 | + */ |
| 157 | + |
| 158 | + function render_field_settings( $field ) { |
| 159 | + |
| 160 | + // allow_null |
| 161 | + acf_render_field_setting( $field, array( |
| 162 | + 'label' => __('Allow Null?','acf'), |
| 163 | + 'instructions' => '', |
| 164 | + 'name' => 'allow_null', |
| 165 | + 'type' => 'true_false', |
| 166 | + 'ui' => 1, |
| 167 | + )); |
| 168 | + |
| 169 | + // multiple |
| 170 | + acf_render_field_setting( $field, array( |
| 171 | + 'label' => __('Select multiple values?','acf'), |
| 172 | + 'instructions' => '', |
| 173 | + 'name' => 'multiple', |
| 174 | + 'type' => 'true_false', |
| 175 | + 'ui' => 1, |
| 176 | + )); |
| 177 | + |
| 178 | + |
| 179 | + // ui |
| 180 | + acf_render_field_setting( $field, array( |
| 181 | + 'label' => __('Stylised UI','acf'), |
| 182 | + 'instructions' => '', |
| 183 | + 'name' => 'ui', |
| 184 | + 'type' => 'true_false', |
| 185 | + 'ui' => 1, |
| 186 | + )); |
| 187 | + |
| 188 | + |
| 189 | + // return_format |
| 190 | + acf_render_field_setting( $field, array( |
| 191 | + 'label' => __('Return Format','acf'), |
| 192 | + 'instructions' => __('Specify the value returned','acf'), |
| 193 | + 'type' => 'select', |
| 194 | + 'name' => 'return_format', |
| 195 | + 'choices' => array( |
| 196 | + 'value' => __('Value','acf'), |
| 197 | + 'label' => __('Label','acf'), |
| 198 | + 'array' => __('Both (Array)','acf') |
| 199 | + ) |
| 200 | + )); |
| 201 | + |
| 202 | + |
| 203 | + // max |
| 204 | + acf_render_field_setting( $field, array( |
| 205 | + 'label' => __('Field key','acf'), |
| 206 | + 'instructions' => __('Specify field key using dot notation, max 2 levels, options supported','acf'), |
| 207 | + 'type' => 'text', |
| 208 | + 'placeholder' => 'repeater.title or options.repeater.title', |
| 209 | + 'name' => 'field_key', |
| 210 | + )); |
| 211 | + |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + acf_register_field_type( 'acf_field_dynamic_select_extension' ); |
| 216 | + |
| 217 | +endif; // class_exists check |
0 commit comments