Skip to content

Commit 1cc9015

Browse files
committed
Shortcut for AddLink
1 parent 7ac5f23 commit 1cc9015

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

src/ManyField.php

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use SilverStripe\Control\Director;
1111
use SilverStripe\Control\Controller;
1212
use SilverStripe\Forms\FormField;
13+
use SilverStripe\Security\SecurityToken;
14+
use SilverStripe\Core\Injector\Injector;
1315
use SilverStripe\Control\HTTPResponse;
1416

1517
class ManyField extends CompositeField
@@ -258,6 +260,14 @@ public function createNewRecord()
258260
return $response;
259261
}
260262

263+
public function AddLink()
264+
{
265+
return Controller::join_links(
266+
$this->AbsoluteLink('createNewRecord'),
267+
'?SecurityID='. SecurityToken::inst()->getValue()
268+
);
269+
}
270+
261271
/**
262272
* Return the list of fields. We'll create a row for each of the values if
263273
* they exist otherwise we'll only return
@@ -350,36 +360,42 @@ public function updateRelation(DataObjectInterface $record, $delete = true)
350360
$existing->removeById($row->ID);
351361
}
352362
}
363+
}
353364

365+
if (isset($this->value['ID'])) {
354366
foreach ($this->value['ID'] as $key => $id) {
355367
if ($id) {
356368
$idKeyMap[$key] = $id;
357369
}
358370
}
359-
360-
foreach ($this->value as $col => $values) {
361-
if ($col == 'ID') {
362-
continue;
363-
}
364-
365-
foreach ($values as $key => $value) {
366-
if (!isset($updatedData[$key])) {
367-
$updatedData[$key] = [];
368-
}
371+
}
372+
373+
foreach ($this->value as $col => $values) {
374+
if ($col == 'ID') {
375+
continue;
376+
}
369377

370-
$updatedData[$key][$col] = $value;
378+
foreach ($values as $key => $value) {
379+
if (!isset($updatedData[$key])) {
380+
$updatedData[$key] = [];
371381
}
382+
383+
$updatedData[$key][$col] = $value;
372384
}
385+
}
373386

374-
foreach ($updatedData as $key => $data) {
375-
if (isset($idKeyMap[$key])) {
376-
$existing->find('ID', $idKeyMap[$key])->update($row);
377-
} else {
378-
$create = Injector::inst()->create($existing->dataClass());
379-
$create->update($data);
380-
$create->write();
381-
$existing->add($create);
382-
}
387+
foreach ($updatedData as $key => $data) {
388+
if (isset($idKeyMap[$key])) {
389+
$record = $existing->find('ID', $idKeyMap[$key]);
390+
391+
$record->update($data);
392+
$record->write();
393+
} else {
394+
$create = Injector::inst()->create($existing->dataClass());
395+
$create->update($data);
396+
397+
$create->write();
398+
$existing->add($create);
383399
}
384400
}
385401

templates/ManyField.ss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<% if canAdd %>
1919
<div class="manyfield__add">
20-
<a class="btn btn-primary" href="$AbsoluteLink(createNewRecord)?SecurityID={$SecurityID}">$AddLabel +</a>
20+
<a class="btn btn-primary" href="$AddLink">$AddLabel +</a>
2121
</div>
2222
<% end_if %>
2323

templates/ManyField_holder.ss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<% if canAdd %>
1919
<div class="manyfield__add">
20-
<a class="btn btn-primary" href="$AbsoluteLink(createNewRecord)?SecurityID={$SecurityID}">$AddLabel +</a>
20+
<a class="btn btn-primary" href="$AddLink">$AddLabel +</a>
2121
</div>
2222
<% end_if %>
2323

0 commit comments

Comments
 (0)