Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: module settings vs. plugin settings #3314

Closed
bertoost opened this issue Sep 19, 2018 · 10 comments
Closed

Feature request: module settings vs. plugin settings #3314

bertoost opened this issue Sep 19, 2018 · 10 comments
Labels
enhancement improvements to existing features extensibility 🔌 features related to plugin/module dev

Comments

@bertoost
Copy link
Contributor

Description

I know modules are not plugins, but sometimes you want to add configurable settings for your module, so the client can adjust the module to their needs.
I also know that it's possible to create your own section. But that's weird in some situations.

For example;
A lot of the time I create a "members" module, which contains a customizations on top of users. Because the client wants some special stuff, relational data, external parties linked to users etc. etc.
In this case I do not want an extra navigation item on the left, but just an extra settings icon/button to give the client the ability to change the module's behavior.

Another example;
My "members" plugin does add a couple of notifications about things that happen. I want my client to be able to change the notification email address or let him toggle things to notify about etc. etc.

Solution

A simple solution would be to create an event on the Settings-dashboard to add-in new custom buttons.

Another solution is to register modules as plugins of type "module" (in the plugins table). The pro for this is, you can use the settings like plugins can do too.

Like to hear your point of view.

@bertoost
Copy link
Contributor Author

For now I am able to override the route, since it's a template-route

// Register route
Event::on(
    UrlManager::class,
    UrlManager::EVENT_REGISTER_CP_URL_RULES,
    function (RegisterUrlRulesEvent $event) {
        $event->rules['settings'] = 'my-module/settings/index';
    }
);
// Controller action
public function actionIndex(array $variables = [])
{
    return $this->renderTemplate('my-module/settings', $variables);
}
{% extends 'settings/index' %}

{% block content %}
    {{ parent() }}

    <hr>

    <h2>Modules</h2>

    <ul class="icons">
        <li>
            <a href="">
                <div class="icon">
                    <!-- todo -->
                </div>
                NAME
            </a>
        </li>
    </ul>
{% endblock %}

But a build in feature is quicker I guess

@brandonkelly
Copy link
Member

I think having settings sortof goes against the point of using a module… at that point maybe it would be better off as a plugin.

If you really want it to be a module, you can define settings in a utility.

@bertoost
Copy link
Contributor Author

Hm okay, but if it is about project specific code, it shouldn't be a plugin, that's the beauty of modules, they are always there and no option to uninstall. But somehow you should be able to define settings, and you should be able to create settings which can be modified.
A utility is also not an option in my opinion.

Now using above solution, which is working fine. Together with using the system settings

// getting it
$settings = \Craft::$app->getSystemSettings()->getSettings('my-module');

// do things...

// setting at save
\Craft::$app->getSystemSettings()->saveSettings('my-module', $settings);

Works good.

@brandonkelly
Copy link
Member

Yeah OK fair enough. I can add an event to craft\web\twig\variables\Cp::settings() that you can use to add links to the page, but you will still be responsible for defining the settings form and the controller to save them. Modules are a Yii concept, not Craft, so don’t want to add any code that will be specifically for giving modules the ability to add settings.

@brandonkelly brandonkelly reopened this Sep 19, 2018
@brandonkelly brandonkelly added enhancement improvements to existing features extensibility 🔌 features related to plugin/module dev labels Sep 19, 2018
@bertoost
Copy link
Contributor Author

bertoost commented Sep 20, 2018

Sounds like a plan 😄 thanks 👍
Maybe I can make a PR in these days.. would love to help!

@bertoost
Copy link
Contributor Author

I also understand your point of modules, but that's such a great improvement for project's codebase instead of plugins. Really love that feature. So a little base class to speed up development could not hurt in my opinion. But maybe in the future.

brandonkelly added a commit that referenced this issue Jan 9, 2019
@brandonkelly
Copy link
Member

Added this for the next Craft 3.1 release.

@j-greig
Copy link
Contributor

j-greig commented Aug 26, 2020

Realise I've missed the boat on this thread, but as a sub-par PHP developer I would love an option to create a settings page for modules that was less involved.

@ArtDepartmentMJ
Copy link

@brandonkelly I know this is very old but would it be possible to expand on this and also the use of
EVENT_REGISTER_CP_SETTINGS?

@brandonkelly
Copy link
Member

@ArtDepartmentMJ Just updated the event docs with an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement improvements to existing features extensibility 🔌 features related to plugin/module dev
Projects
None yet
Development

No branches or pull requests

4 participants