Skip to content

Commit f00ce71

Browse files
authored
Merge pull request #105 from chav170/feature/crud-view-afterFindEntity-event
Add the afterFindEntity to the CrudViewAction
2 parents f92ab2c + f1b9558 commit f00ce71

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

docs/Documentation/overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ Crud actions define some events that depend on the type of action and more detai
125125
* Action.Crud.onFindEntities (applied for index action)
126126
* Action.Crud.afterFindEntities (applied for index action)
127127
* Action.Crud.onFindEntity (applied for view action)
128+
* Action.Crud.afterFindEntity (applied for view action)
128129

129130

130131
### Listing Service.

src/Service/Action/CrudAction.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,13 @@ protected function _getEntity($primaryKey)
260260
$query = $event->getResult();
261261
}
262262

263-
return $query->firstOrFail();
263+
$record = $query->firstOrFail();
264+
$event = $this->dispatchEvent('Action.Crud.afterFindEntity', ['query' => $query, 'record' => $record]);
265+
if ($event->getResult() !== null) {
266+
$record = $event->getResult();
267+
}
268+
269+
return $record;
264270
}
265271

266272
/**

tests/TestCase/Service/Action/CrudViewActionTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,18 @@ public function testExecuteSuccess()
8686
'id' => 1,
8787
]);
8888

89-
$onFindEntity = false;
89+
$onFindEntity = $afterFindEntity = false;
9090
$this->Action->getEventManager()->on('Action.Crud.onFindEntity', function () use (&$onFindEntity) {
9191
$onFindEntity = true;
9292
});
93+
$this->Action->getEventManager()->on('Action.Crud.afterFindEntity', function () use (&$afterFindEntity) {
94+
$afterFindEntity = true;
95+
});
9396

9497
$result = $this->Action->execute();
9598
$this->assertTrue($result instanceof EntityInterface);
9699
$this->assertTrue($onFindEntity);
100+
$this->assertTrue($afterFindEntity);
97101
}
98102

99103
/**

0 commit comments

Comments
 (0)