mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #5815 from kinglozzer/form-buttonclicked-duplicate-code
Refactor duplicate code in Form
This commit is contained in:
commit
a73f08bd88
@ -480,13 +480,9 @@ class Form extends RequestHandler {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Always allow actions which map to buttons. See httpSubmission() for further access checks.
|
||||
$fields = $this->fields->dataFields() ?: array();
|
||||
$actions = $this->actions->dataFields() ?: array();
|
||||
|
||||
$fieldsAndActions = array_merge($fields, $actions);
|
||||
foreach ($fieldsAndActions as $fieldOrAction) {
|
||||
if ($fieldOrAction instanceof FormAction && $fieldOrAction->actionName() === $action) {
|
||||
$actions = $this->getAllActions();
|
||||
foreach ($actions as $formAction) {
|
||||
if ($formAction->actionName() === $action) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -1648,23 +1644,33 @@ class Form extends RequestHandler {
|
||||
* @return FormAction
|
||||
*/
|
||||
public function buttonClicked() {
|
||||
$fields = $this->fields->dataFields() ?: array();
|
||||
$actions = $this->actions->dataFields() ?: array();
|
||||
|
||||
if(!$actions && !$fields) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$fieldsAndActions = array_merge($fields, $actions);
|
||||
foreach ($fieldsAndActions as $fieldOrAction) {
|
||||
if ($fieldOrAction instanceof FormAction && $this->buttonClickedFunc === $fieldOrAction->actionName()) {
|
||||
return $fieldOrAction;
|
||||
$actions = $this->getAllActions();
|
||||
foreach ($actions as $action) {
|
||||
if ($this->buttonClickedFunc === $action->actionName()) {
|
||||
return $action;
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
* available.
|
||||
|
Loading…
Reference in New Issue
Block a user