diff --git a/docs/Documentation/overview.md b/docs/Documentation/overview.md index fd03e82..5640197 100644 --- a/docs/Documentation/overview.md +++ b/docs/Documentation/overview.md @@ -115,9 +115,9 @@ Please note, that action validation is not the same as model level validation. T Crud service defines actions and parameters for RESTful crud API. -Nested Crud service gentting parent params from routing system and if it is presents loads Nested extension for all actions. +Nested Crud service getting parent params from routing system and if it is presents loads Nested extension for all actions. -Fallback service is default implemeention of Nested Crud that defines routes for 1-level deep nesting. +Fallback service is default the implementation of Nested Crud that defines routes for 1-level deep nesting. Crud actions define some events that depend on the type of action and more details could be checked in documentation. @@ -125,6 +125,7 @@ Crud actions define some events that depend on the type of action and more detai * Action.Crud.onFindEntities (applied for index action) * Action.Crud.afterFindEntities (applied for index action) * Action.Crud.onFindEntity (applied for view action) +* Action.Crud.afterFindEntity (applied for view action) ### Listing Service. @@ -141,7 +142,7 @@ Any action are decorated by some functionality it is implements during it life f Different extension could return additional info that extends returned by API data. In this case extension append payload data into Result object that used by renderers to build final output. -This way such extensions like pagination or hateoas inteact with caller. +This way such extensions like pagination or hateoas interact with caller. ## Request parser diff --git a/src/Service/Action/CrudAction.php b/src/Service/Action/CrudAction.php index 04825ed..6e9aa5a 100644 --- a/src/Service/Action/CrudAction.php +++ b/src/Service/Action/CrudAction.php @@ -261,7 +261,13 @@ protected function _getEntity($primaryKey) $query = $event->getResult(); } - return $query->firstOrFail(); + $record = $query->firstOrFail(); + $event = $this->dispatchEvent('Action.Crud.afterFindEntity', ['query' => $query, 'record' => $record]); + if ($event->getResult() !== null) { + $record = $event->getResult(); + } + + return $record; } /** diff --git a/tests/TestCase/Service/Action/CrudViewActionTest.php b/tests/TestCase/Service/Action/CrudViewActionTest.php index 16dc49c..211e662 100644 --- a/tests/TestCase/Service/Action/CrudViewActionTest.php +++ b/tests/TestCase/Service/Action/CrudViewActionTest.php @@ -86,14 +86,18 @@ public function testExecuteSuccess() 'id' => 1, ]); - $onFindEntity = false; + $onFindEntity = $afterFindEntity = false; $this->Action->getEventManager()->on('Action.Crud.onFindEntity', function () use (&$onFindEntity) { $onFindEntity = true; }); + $this->Action->getEventManager()->on('Action.Crud.afterFindEntity', function () use (&$afterFindEntity) { + $afterFindEntity = true; + }); $result = $this->Action->execute(); $this->assertTrue($result instanceof EntityInterface); $this->assertTrue($onFindEntity); + $this->assertTrue($afterFindEntity); } /**