Skip to content

Commit 22e68fc

Browse files
committed
UploadField, CheckboxSetField work in shortcode form fixes #37
1 parent 40929c7 commit 22e68fc

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

code/controllers/ShortcodableController.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ public function ShortcodeForm()
158158
$fields->push(
159159
CompositeField::create($attrFields)
160160
->addExtraClass('attributes-composite')
161+
->setName('AttributesCompositeField')
161162
);
162163
}
163164
}
@@ -176,14 +177,21 @@ public function ShortcodeForm()
176177
->loadDataFrom($this)
177178
->addExtraClass('htmleditorfield-form htmleditorfield-shortcodable cms-dialog-content');
178179

179-
if ($data = $this->getShortcodeData()) {
180-
$form->loadDataFrom($data['atts']);
181-
}
182-
183180
$this->extend('updateShortcodeForm', $form);
184181

185182
$fields->push(LiteralField::create('shortcodablefieldsend', '</div>'));
186183

184+
if ($data = $this->getShortcodeData()) {
185+
$form->loadDataFrom($data['atts']);
186+
187+
// special treatment for setting value of UploadFields
188+
foreach ($form->Fields()->dataFields() as $field) {
189+
if (is_a($field, 'UploadField') && isset($data['atts'][$field->getName()])) {
190+
$field->setValue(array('Files' => explode(',', $data['atts'][$field->getName()])));
191+
}
192+
}
193+
}
194+
187195
return $form;
188196
}
189197

javascript/shortcodable.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,38 @@
8787
if (id) {
8888
attributes['id'] = id;
8989
}
90+
var data = JSON.stringify(this.serializeArray());
91+
9092
var attributesComposite = this.find('.attributes-composite');
9193
if (attributesComposite.length) {
9294
attributesComposite.find(":input").each(function(){
9395
var attributeField = $(this);
9496
var attributeVal = attributeField.val();
97+
var attributeName = attributeField.prop('name');
98+
99+
if(attributeField.is('.checkbox') && !attributeField.is(':checked')) {
100+
return true; // skip unchecked checkboxes
101+
}
102+
95103
if(attributeVal !== ''){
96-
if(attributeField.is('.checkbox')) {
97-
attributes[attributeField.prop('name')] = attributeField.is(':checked') ? 1 : 0;
104+
if (attributeName.indexOf('[') > -1) {
105+
var key = attributeName.substring(0, attributeName.indexOf('['));
106+
if (typeof attributes[key] != 'undefined') {
107+
attributes[key] += ',' + attributeVal;
108+
} else {
109+
attributes[key] = attributeVal;
110+
}
98111
} else {
99-
attributes[attributeField.prop('name')] = attributeVal;
112+
if(attributeField.is('.checkbox')) {
113+
attributes[attributeField.prop('name')] = attributeField.is(':checked') ? 1 : 0;
114+
} else {
115+
attributes[attributeField.prop('name')] = attributeVal;
116+
}
100117
}
101118
}
102119
});
103120
}
104-
121+
console.log(attributes);
105122
return {
106123
'shortcodeType' : this.find(':input[name=ShortcodeType]').val(),
107124
'attributes' : attributes

0 commit comments

Comments
 (0)