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