Allow overriding form config in arbitrary contexts

This enables arbitrary form contexts to be defined to override the default values when using a form with custom contexts. I.e. `$this->update($id, 'mycustomcontext');` will load the form definition from `mycustomcontext[form]` instead of `update[form]`
This commit is contained in:
Luke Towers 2017-12-12 10:39:03 -06:00 committed by GitHub
parent a2789e6f60
commit d0546599d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 26 deletions

View File

@ -127,17 +127,7 @@ class FormController extends ControllerBehavior
/*
* Each page can supply a unique form definition, if desired
*/
$formFields = $this->config->form;
if ($context == self::CONTEXT_CREATE) {
$formFields = $this->getConfig('create[form]', $formFields);
}
elseif ($context == self::CONTEXT_UPDATE) {
$formFields = $this->getConfig('update[form]', $formFields);
}
elseif ($context == self::CONTEXT_PREVIEW) {
$formFields = $this->getConfig('preview[form]', $formFields);
}
$formFields = $this->getConfig("{$context}[form]", $this->config->form);
$config = $this->makeConfig($formFields);
$config->model = $model;
@ -209,7 +199,7 @@ class FormController extends ControllerBehavior
try {
$this->context = strlen($context) ? $context : $this->getConfig('create[context]', self::CONTEXT_CREATE);
$this->controller->pageTitle = $this->controller->pageTitle ?: $this->getLang(
'create[title]',
"{$this->context}[title]",
'backend::lang.form.create_title'
);
@ -255,7 +245,7 @@ class FormController extends ControllerBehavior
$this->controller->formAfterSave($model);
$this->controller->formAfterCreate($model);
Flash::success($this->getLang('create[flashSave]', 'backend::lang.form.create_success'));
Flash::success($this->getLang("{$this->context}[flashSave]", 'backend::lang.form.create_success'));
if ($redirect = $this->makeRedirect('create', $model)) {
return $redirect;
@ -280,7 +270,7 @@ class FormController extends ControllerBehavior
try {
$this->context = strlen($context) ? $context : $this->getConfig('update[context]', self::CONTEXT_UPDATE);
$this->controller->pageTitle = $this->controller->pageTitle ?: $this->getLang(
'update[title]',
"{$this->context}[title]",
'backend::lang.form.update_title'
);
@ -322,7 +312,7 @@ class FormController extends ControllerBehavior
$this->controller->formAfterSave($model);
$this->controller->formAfterUpdate($model);
Flash::success($this->getLang('update[flashSave]', 'backend::lang.form.update_success'));
Flash::success($this->getLang("{$this->context}[flashSave]", 'backend::lang.form.update_success'));
if ($redirect = $this->makeRedirect('update', $model)) {
return $redirect;
@ -349,7 +339,7 @@ class FormController extends ControllerBehavior
$this->controller->formAfterDelete($model);
Flash::success($this->getLang('update[flashDelete]', 'backend::lang.form.delete_success'));
Flash::success($this->getLang("{$this->context}[flashDelete]", 'backend::lang.form.delete_success'));
if ($redirect = $this->makeRedirect('delete', $model)) {
return $redirect;
@ -374,7 +364,7 @@ class FormController extends ControllerBehavior
try {
$this->context = strlen($context) ? $context : $this->getConfig('preview[context]', self::CONTEXT_PREVIEW);
$this->controller->pageTitle = $this->controller->pageTitle ?: $this->getLang(
'preview[title]',
"{$this->context}[title]",
'backend::lang.form.preview_title'
);
@ -489,14 +479,15 @@ class FormController extends ControllerBehavior
*/
protected function getRedirectUrl($context = null)
{
$redirects = [
'default' => $this->getConfig('defaultRedirect', ''),
'create' => $this->getConfig('create[redirect]', ''),
'create-close' => $this->getConfig('create[redirectClose]', ''),
'update' => $this->getConfig('update[redirect]', ''),
'update-close' => $this->getConfig('update[redirectClose]', ''),
'preview' => $this->getConfig('preview[redirect]', ''),
];
$redirectContext = explode('-', $context, 2)[0];
$redirectSource = ends_with($context, '-close') ? 'redirectClose' : 'redirect';
// Get the redirect for the provided context
$redirects = [$context => $this->getConfig("{$redirectContext}[{$redirectSource}]", '')];
// Assign the default redirect afterwards to prevent the
// source for the default redirect being default[redirect]
$redirects['default' => $this->getConfig('defaultRedirect', '')];
if (!isset($redirects[$context])) {
return $redirects['default'];
@ -789,7 +780,6 @@ class FormController extends ControllerBehavior
/**
* Called after the form fields are defined.
* @param Backend\Widgets\Form $host The hosting form widget
* @param array All form fields in the form of ['fieldName' => $fieldObject]
* @return void
*/
public function formExtendFields($host, $fields)