Skip to content

Commit 1a0c916

Browse files
committed
API Add $changedFields to edit extension hooks
1 parent 091e69c commit 1a0c916

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,16 +418,16 @@ Notes:
418418

419419
- If your extension hook updates the DataObject or another DataObject then it is likely you should use a different extension hook such as `onAfterWrite()` on the Dataobject itself rather than on the endpoint. This is because it usually shouldn't matter whether the object was created/updated/deleted via the API or a different way. These hooks are intended to facilitate the implementation of API specific code such as logging operations done via the API.
420420
- For the `onView*()` hooks if you are adding extra data to the JSON for the response, remember to call `canView()` for any DataObjects being added as required.
421-
- For the `onEdit*Write()` hooks you may find it useful to get the fields changed in the operations with `$obj->getChangedFields()`.
421+
- For both of the `onEdit*Write()` hooks the `$changedFields` param is return value of `$obj->getChangedFields()` before the object was written to.
422422

423423
| Extension hook | Description |
424424
| - | - |
425425
| `onViewOne(DataObject $obj)` | Called during `GET` requests to view a single record before rendering JSON for response |
426426
| `onViewMany(array $objs)` | Called during `GET` requests to view many records before rendering JSON for response |
427427
| `onCreateBeforeWrite(DataObject $obj)` | Called during `POST` requests before calling `$obj->write()` |
428428
| `onCreateAfterWrite(DataObject $obj)` | Called during `POST` requests after calling `$obj->write()` |
429-
| `onEditBeforeWrite(DataObject $obj)` | Called during `PATCH` requests before calling `$obj->write()` |
430-
| `onEditAfterWrite(DataObject $obj)` | Called during `PATCH` requests after calling `$obj->write()` |
429+
| `onEditBeforeWrite(DataObject $obj, array $changedFields)` | Called during `PATCH` requests before calling `$obj->write()` |
430+
| `onEditAfterWrite(DataObject $obj, array $changedFields)` | Called during `PATCH` requests after calling `$obj->write()` |
431431
| `onDeleteBeforeDelete(DataObject $obj)` | Called during `DELETE` requests before calling `$obj->write()` |
432432
| `onDeleteAfterDelete(DataObject $obj)` | Called during `DELETE` requests after calling `$obj->write()` |
433433
| `onBeforeAction(DataObject $obj, string $action)` | Called during `ACTION` requests before running the action |

src/Controllers/RestApiEndpoint.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,13 +539,14 @@ private function apiEdit(): HTTPResponse
539539
// update dataObject
540540
$obj = $this->dataObjectFromRequest($dataClass);
541541
$this->updateDataObjectWithData($obj, $data, self::EDIT);
542-
$this->invokeWithExtensions('onEditBeforeWrite', $obj);
542+
$changedFields = $obj->getChangedFields();
543+
$this->invokeWithExtensions('onEditBeforeWrite', $obj, $changedFields);
543544
try {
544545
$obj->write();
545546
} catch (ValidationException $e) {
546547
return $this->error($e->getMessage(), 422);
547548
}
548-
$this->invokeWithExtensions('onEditAfterWrite', $obj);
549+
$this->invokeWithExtensions('onEditAfterWrite', $obj, $changedFields);
549550
return $this->success($this->jsonData($obj), 200);
550551
}
551552

tests/Controllers/RestApiEndpointTest/TestApiEndpoint.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ protected function onCreateAfterWrite(DataObject $obj): void
158158
self::$hooksCalled['onCreateAfterWrite'] = true;
159159
}
160160

161-
protected function onEditBeforeWrite(DataObject $obj): void
161+
protected function onEditBeforeWrite(DataObject $obj, array $changedFields): void
162162
{
163163
self::$hooksCalled['onEditBeforeWrite'] = true;
164164
}
165165

166-
protected function onEditAfterWrite(DataObject $obj): void
166+
protected function onEditAfterWrite(DataObject $obj, array $changedFields): void
167167
{
168168
self::$hooksCalled['onEditAfterWrite'] = true;
169169
}

0 commit comments

Comments
 (0)