Skip to content

Commit

Permalink
Add the afterFindEntity to the CrudViewAction
Browse files Browse the repository at this point in the history
  • Loading branch information
chav170 committed Jan 30, 2024
1 parent f92ab2c commit f1b9558
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/Documentation/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 7 additions & 1 deletion src/Service/Action/CrudAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,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;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion tests/TestCase/Service/Action/CrudViewActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit f1b9558

Please sign in to comment.