Skip to content

Commit

Permalink
Merge pull request #106 from chav170/cherry-pick/crud-view-afterFindE…
Browse files Browse the repository at this point in the history
…ntity-event

Cherry pick/crud view after find entity event
  • Loading branch information
skie authored Jan 31, 2024
2 parents 582934a + d1a3c90 commit 114bff9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
7 changes: 4 additions & 3 deletions docs/Documentation/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,17 @@ 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.

* Action.Crud.onPatchEntity (applied for add/edit actions)
* 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 All @@ -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

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 @@ -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;
}

/**
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 114bff9

Please sign in to comment.