|
138 | 138 | ]; |
139 | 139 | } |
140 | 140 | ], |
| 141 | + 'dreamform/form-fields' => [ |
| 142 | + 'load' => function () { |
| 143 | + $fieldKey = App::instance()->request()->get('field'); |
| 144 | + $formId = App::instance()->request()->get('form'); |
| 145 | + |
| 146 | + // get form from referrer if not provided |
| 147 | + if (!$formId) { |
| 148 | + $path = App::instance()->request()->header('x-fiber-referrer'); |
| 149 | + if ($path && preg_match('/pages\/([^\/]+\+[^\/]+)/', $path, $matches)) { |
| 150 | + $formId = Str::replace($matches[1], '+', '/'); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + $options = []; |
| 155 | + $currentValue = null; |
| 156 | + |
| 157 | + if ($formId && $formPage = App::instance()->site()->find($formId)) { |
| 158 | + // get available fields from form |
| 159 | + foreach ($formPage->fields() as $field) { |
| 160 | + if (!$field::hasValue() || $field->block()->type() === 'file-upload-field') { |
| 161 | + continue; |
| 162 | + } |
| 163 | + |
| 164 | + $key = $field->key(); |
| 165 | + $label = $field->label() ?: $key; |
| 166 | + $type = Str::replace($field->block()->type(), '-field', ''); |
| 167 | + |
| 168 | + // store both key and label in value for JS to use |
| 169 | + $options[] = [ |
| 170 | + 'value' => json_encode(['key' => $key, 'label' => $label]), |
| 171 | + 'text' => $label, |
| 172 | + 'info' => $type |
| 173 | + ]; |
| 174 | + |
| 175 | + // set current value if field matches |
| 176 | + if ($fieldKey === $key) { |
| 177 | + $currentValue = json_encode(['key' => $key, 'label' => $label]); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + // default to first option if no selection |
| 183 | + if (!$currentValue && !empty($options)) { |
| 184 | + $currentValue = $options[0]['value']; |
| 185 | + } |
| 186 | + |
| 187 | + return [ |
| 188 | + 'component' => 'k-form-dialog', |
| 189 | + 'props' => [ |
| 190 | + 'fields' => [ |
| 191 | + 'field' => [ |
| 192 | + 'type' => 'select', |
| 193 | + 'label' => t('dreamform.writerNodes.selectField'), |
| 194 | + 'options' => $options, |
| 195 | + 'required' => true, |
| 196 | + 'empty' => empty($options) ? t('dreamform.common.noFields') : false |
| 197 | + ] |
| 198 | + ], |
| 199 | + 'value' => [ |
| 200 | + 'field' => $currentValue |
| 201 | + ], |
| 202 | + 'submitButton' => [ |
| 203 | + 'text' => $fieldKey ? t('dreamform.common.update') : t('dreamform.common.insert') |
| 204 | + ] |
| 205 | + ] |
| 206 | + ]; |
| 207 | + }, |
| 208 | + 'submit' => function () { |
| 209 | + return true; |
| 210 | + } |
| 211 | + ] |
141 | 212 | ] |
142 | 213 | ] |
143 | 214 | ]; |
0 commit comments