CMS action related extension points (#9340)

* CMS action related extension points

* Refactor to use fewer extension points

* Remove explicit return type

Co-authored-by: Aaron Carlino <unclecheese@leftandmain.com>
This commit is contained in:
Mojmir Fendek 2020-01-15 14:24:49 +13:00 committed by Aaron Carlino
parent 0567dce72f
commit acbbf80d14
2 changed files with 25 additions and 4 deletions

View File

@ -225,25 +225,26 @@ class FormRequestHandler extends RequestHandler
// First, try a handler method on the controller (has been checked for allowed_actions above already)
$controller = $this->form->getController();
$args = [$funcName, $request, $vars];
if ($controller && $controller->hasMethod($funcName)) {
$controller->setRequest($request);
return $controller->$funcName($vars, $this->form, $request, $this);
return $this->invokeFormHandler($controller, ...$args);
}
// Otherwise, try a handler method on the form request handler.
if ($this->hasMethod($funcName)) {
return $this->$funcName($vars, $this->form, $request, $this);
return $this->invokeFormHandler($this, ...$args);
}
// Otherwise, try a handler method on the form itself
if ($this->form->hasMethod($funcName)) {
return $this->form->$funcName($vars, $this->form, $request, $this);
return $this->invokeFormHandler($this->form, ...$args);
}
// Check for inline actions
$field = $this->checkFieldsForAction($this->form->Fields(), $funcName);
if ($field) {
return $field->$funcName($vars, $this->form, $request, $this);
return $this->invokeFormHandler($field, ...$args);
}
} catch (ValidationException $e) {
// The ValdiationResult contains all the relevant metadata
@ -515,4 +516,20 @@ class FormRequestHandler extends RequestHandler
{
return $this->form->forTemplate();
}
/**
* @param $subject
* @param string $funcName
* @param HTTPRequest $request
* @param array $vars
* @return mixed
*/
private function invokeFormHandler($subject, string $funcName, HTTPRequest $request, array $vars)
{
$this->extend('beforeCallFormHandler', $request, $funcName, $vars, $this->form, $subject);
$result = $subject->$funcName($vars, $this->form, $request, $this);
$this->extend('afterCallFormHandler', $request, $funcName, $vars, $this->form, $subject, $result);
return $result;
}
}

View File

@ -1133,7 +1133,11 @@ class GridField extends FormField
}
try {
$this->extend('beforeCallActionURLHandler', $request, $action);
$result = $component->$action($this, $request);
$this->extend('afterCallActionURLHandler', $request, $action, $result);
} catch (HTTPResponse_Exception $responseException) {
$result = $responseException->getResponse();
}