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] 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); } /**