This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

maraerben
maraerben

Hello, I have (maybe trivial) problem with my plugin Settings model. I can create it so I can see it in Setting module in backend. But can I insert this form anywhere else in Backend?

Thank you for your advice!

mjauvin
mjauvin

What do you mean by "can I insert this form anywhere else in Backend" ?

Those settings can be referenced anywhere in backend with Settings::get('my-setting')

use \Author\PluginName\Models\Settings;

Settings::get('my-setting');
maraerben
maraerben

Hello, thanks for your reply. I know how to get values from the Settings model. My problem is different.

I have my custom plugin, e.g. Foo. It contains some controller, e.g. Bar and some view file, e.g. baz, so URL of this page will be something like backend/author/foo/bar/baz. And on this page I want to render the whole Settings form like each other form. (So I can see values, edit them, save them) Something like <?= $this->formRender() ?> with classic model form.

Last updated

mjauvin
mjauvin

Ok, I see what you mean now... I don't know how to do this.

mjauvin
mjauvin

I found a way:

<?php namespace StudioAzura\Test\Controllers;

use BackendMenu;
use Redirect;
use Request;

class Settings extends \System\Controllers\Settings
{
    protected $author = 'StudioAzura';
    protected $plugin = 'Test';
    protected $code = 'settings';
    protected $mainMenuId = 'test-main';
    protected $sideMenuId = 'side-menu-item2';

    public function __construct()
    {   
        parent::__construct();

        BackendMenu::setContext(sprintf('%s.%s', $this->author, $this->plugin), $this->mainMenuId, $this->sideMenuId);
    }

    public function index()
    {   
        $url = sprintf('%s/update/%s/%s/%s', Request::url(), $this->author, $this->plugin, $this->code);

        return Redirect::to($url);
    }
}

Just replace your author, plugin and code with what you used in your plugin's registerSettings.

Of course, update the namespace with yours.

mjauvin
mjauvin

Oh, and you need to copy ~/modules/system/controllers/settings/update.htm into your controller's partial folder

mjauvin
mjauvin

This version is better and requires less config (no need to copy parent's view file update.htm:

<?php namespace StudioAzura\Test\Controllers;

use BackendMenu;
use Redirect;
use Request;

class Settings extends \System\Controllers\Settings
{   
    protected $author;
    protected $plugin;
    protected $code = 'settings';
    protected $mainMenuId = 'test-main';
    protected $sideMenuId = 'side-menu-item2';

    public function __construct()
    {   
        parent::__construct();
        $this->viewPath = base_path().'/modules/system/controllers/settings';

        $params = explode('\\', get_class($this));
        $this->author = $params[0];
        $this->plugin = $params[1];

        BackendMenu::setContext(sprintf('%s.%s', $this->author, $this->plugin), $this->mainMenuId, $this->sideMenuId);
    }

    public function index()
    {   
        $url = sprintf('%s/update/%s/%s/%s', Request::url(), $this->author, $this->plugin, $this->code);

        return Redirect::to($url);
    }
}
maraerben
maraerben

Thank you, sir! Your second snippet is exactly what I was looking for. :-) Very simple and elegant solution!

Last updated

mjauvin

1-9 of 9

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.