Skip to content

Commit 8113e97

Browse files
committed
If inline save disabled then don't add buttton
1 parent e3f3cd7 commit 8113e97

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

client/js/ManyField.src.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
$(elem).find('.manyfield__row').each(function (r, row) {
1313
if (canRemove) {
1414
if (!$(row).find('.manyfield__remove').length) {
15-
var href = $(this).data('inline-save').replace('saveRecord', 'deleteRecord');
15+
var href = $(this).data('inline-save');
1616

17-
href = href + '?ID='+ $(row).find('[name=ID]').val();
17+
if (href) {
18+
href.replace('saveRecord', 'deleteRecord');
19+
href = href + '?ID='+ $(row).find('[name=ID]').val();
1820

19-
$(row).prepend('<a class="btn btn-sm btn-danger manyfield__remove" href="' + href + '"><i class="fa fa-times"></i></a>');
21+
$(row).prepend('<a class="btn btn-sm btn-danger manyfield__remove" href="' + href + '"><i class="fa fa-times"></i></a>');
22+
}
2023
}
2124
} else {
2225
field.find('.manyfield__remove').remove();

src/ManyField.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use SilverStripe\Core\Injector\Injector;
1515
use SilverStripe\Control\HTTPResponse;
1616
use Exception;
17+
use SilverStripe\ORM\SS_List;
1718

1819
class ManyField extends CompositeField
1920
{
@@ -379,15 +380,19 @@ public function saveRecord()
379380
$class = $this->value->dataClass();
380381
}
381382

382-
if (!$index || !$class) {
383-
throw new Exception('saveRecord() must be passed an ID and ClassName');
383+
if (!$class) {
384+
throw new Exception('saveRecord() must be passed a ClassName');
384385
}
385386

386387
if ($this->manyFieldDataClass && $class !== $this->manyFieldDataClass) {
387388
throw new Exception('Invalid ClassName passed');
388389
}
389390

390-
$record = $class::get()->byId($index);
391+
if (!$index) {
392+
$record = $class::create();
393+
} else {
394+
$record = $class::get()->byId($index);
395+
}
391396

392397
if (!$record || !$record->canEdit()) {
393398
return Controller::curr()->httpError(400);
@@ -649,10 +654,17 @@ public function generateRow($index, $value = null, $prefixName = true)
649654

650655
public function createPhysicalRecord()
651656
{
652-
$create = Injector::inst()->create($this->value->dataClass());
657+
if ($this->manyFieldDataClass) {
658+
$create = Injector::inst()->create($this->manyFieldDataClass);
659+
} else {
660+
$create = Injector::inst()->create($this->value->dataClass());
661+
}
662+
653663
$create->write();
654664

655-
$this->value->add($create);
665+
if ($this->value instanceof SS_List) {
666+
$this->value->add($create);
667+
}
656668

657669
return $create;
658670
}
@@ -675,6 +687,7 @@ public function updateRelation(DataObjectInterface $record, $delete = true)
675687
}
676688

677689
$existing = $record->{$this->name}();
690+
678691
// if no value then we should clear everything out
679692
if (!$this->value && $this->canRemove) {
680693
if ($delete) {
@@ -713,6 +726,8 @@ public function updateRelation(DataObjectInterface $record, $delete = true)
713726
}
714727
}
715728

729+
$updatedData = [];
730+
716731
foreach ($this->value as $col => $values) {
717732
if ($col == 'ID') {
718733
continue;

0 commit comments

Comments
 (0)