Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry pick/crud view after find entity event #106

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading