Merge pull request #5815 from kinglozzer/form-buttonclicked-duplicate-code

Refactor duplicate code in Form
This commit is contained in:
Daniel Hensby 2016-07-18 10:13:37 +01:00 committed by GitHub
commit a73f08bd88

View File

@ -480,13 +480,9 @@ class Form extends RequestHandler {
return true; return true;
} }
// Always allow actions which map to buttons. See httpSubmission() for further access checks. $actions = $this->getAllActions();
$fields = $this->fields->dataFields() ?: array(); foreach ($actions as $formAction) {
$actions = $this->actions->dataFields() ?: array(); if ($formAction->actionName() === $action) {
$fieldsAndActions = array_merge($fields, $actions);
foreach ($fieldsAndActions as $fieldOrAction) {
if ($fieldOrAction instanceof FormAction && $fieldOrAction->actionName() === $action) {
return true; return true;
} }
} }
@ -1648,23 +1644,33 @@ class Form extends RequestHandler {
* @return FormAction * @return FormAction
*/ */
public function buttonClicked() { public function buttonClicked() {
$fields = $this->fields->dataFields() ?: array(); $actions = $this->getAllActions();
$actions = $this->actions->dataFields() ?: array(); foreach ($actions as $action) {
if ($this->buttonClickedFunc === $action->actionName()) {
if(!$actions && !$fields) { return $action;
return null;
}
$fieldsAndActions = array_merge($fields, $actions);
foreach ($fieldsAndActions as $fieldOrAction) {
if ($fieldOrAction instanceof FormAction && $this->buttonClickedFunc === $fieldOrAction->actionName()) {
return $fieldOrAction;
} }
} }
return null; return null;
} }
/**
* Get a list of all actions, including those in the main "fields" FieldList
*
* @return array
*/
protected function getAllActions() {
$fields = $this->fields->dataFields() ?: array();
$actions = $this->actions->dataFields() ?: array();
$fieldsAndActions = array_merge($fields, $actions);
$actions = array_filter($fieldsAndActions, function($fieldOrAction) {
return $fieldOrAction instanceof FormAction;
});
return $actions;
}
/** /**
* Return the default button that should be clicked when another one isn't * Return the default button that should be clicked when another one isn't
* available. * available.