From 913b13d93cfee66ccbb523e4c87bcd291533f490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20P=C3=A9rez?= Date: Tue, 30 Jan 2024 13:17:52 +0000 Subject: [PATCH 1/2] Add the afterFindEntity to the CrudViewAction --- docs/Documentation/overview.md | 1 + src/Service/Action/CrudAction.php | 8 +++++++- tests/TestCase/Service/Action/CrudViewActionTest.php | 6 +++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/Documentation/overview.md b/docs/Documentation/overview.md index fd03e82..9485f12 100644 --- a/docs/Documentation/overview.md +++ b/docs/Documentation/overview.md @@ -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. 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); } /** From d1a3c90db0ec257e6be38c2b4f804fd1dbcbda0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20P=C3=A9rez?= Date: Tue, 30 Jan 2024 15:17:24 +0000 Subject: [PATCH 2/2] Fyx typos in docs --- docs/Documentation/overview.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/Documentation/overview.md b/docs/Documentation/overview.md index 9485f12..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. @@ -142,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