Provide an accessor for the form widget of the Settings controller (#5212)

Co-authored-by: Luke Towers <github@luketowers.ca>
This commit is contained in:
Ben Thomson 2020-07-15 14:04:01 +08:00 committed by GitHub
parent 4fb4e318f1
commit 8fd1ddf7aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 51 additions and 2 deletions

View File

@ -2,6 +2,8 @@
use Lang; use Lang;
use Flash; use Flash;
use Config;
use Request;
use Backend; use Backend;
use BackendMenu; use BackendMenu;
use System\Classes\SettingsManager; use System\Classes\SettingsManager;
@ -139,6 +141,22 @@ class Settings extends Controller
return $this->formWidget->render($options); return $this->formWidget->render($options);
} }
/**
* Returns the form widget used by this behavior.
*
* @return \Backend\Widgets\Form
*/
public function formGetWidget()
{
if (is_null($this->formWidget)) {
$item = $this->findSettingItem();
$model = $this->createModel($item);
$this->initWidgets($model);
}
return $this->formWidget;
}
/** /**
* Prepare the widgets used by this action * Prepare the widgets used by this action
* Model $model * Model $model
@ -169,10 +187,22 @@ class Settings extends Controller
} }
/** /**
* Locates a setting item for a module or plugin * Locates a setting item for a module or plugin.
*
* If none of the parameters are provided, they will be auto-guessed from the URL.
*
* @param string|null $author
* @param string|null $plugin
* @param string|null $code
*
* @return array
*/ */
protected function findSettingItem($author, $plugin, $code) protected function findSettingItem($author = null, $plugin = null, $code = null)
{ {
if (is_null($author) || is_null($plugin)) {
[$author, $plugin, $code] = $this->guessSettingItem();
}
$manager = SettingsManager::instance(); $manager = SettingsManager::instance();
$moduleOwner = $author; $moduleOwner = $author;
@ -187,4 +217,23 @@ class Settings extends Controller
return $item; return $item;
} }
/**
* Guesses the requested setting item from the current URL segments provided by the Request object.
*
* @return array
*/
protected function guessSettingItem()
{
$segments = Request::segments();
if (!empty(Config::get('cms.backendUri', 'backend'))) {
array_splice($segments, 0, 4);
} else {
array_splice($segments, 0, 3);
}
// Ensure there's at least 3 segments
return array_pad($segments, 3, null);
}
} }