0

I read the docs multiple times but I'm stuck. I need to create a module that shows a form in the CP where an admin can input and save answers to 2 questions. It will always be just one single form. I should also be able to read the saved values from within my code.

This is where I am: I can display a link in the sidebar, and I can access the form when I click the link. And then my troubles begin:

  1. I know that I can get a model to populate the form with previously saved data. But I still haven't figure how to get the saved data. And haven't figured how to avoid error messages the first time the form is accessed and nothing has been saved yet.
  2. The docs show an example, adding 2 routes (new and save) to EVENT_REGISTER_CP_URL_RULES. I imagine I only need one (for saving edits). But my attempts to register the rules haven't succeeded.

What I have so far is:

Main Class:


// CP Routes
        Event::on(
            UrlManager::class,
            UrlManager::EVENT_REGISTER_CP_URL_RULES,
            function(RegisterUrlRulesEvent $event) {
                $event->rules['password-global'] = 'password-global/settings/index';
            }
        );
// Registers CP Template
        Event::on(
            View::class, 
            View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, 
            function (RegisterTemplateRootsEvent $e) {
            if (is_dir($baseDir = $this->getBasePath() . DIRECTORY_SEPARATOR . 'templates')) {
                $e->roots[$this->id] = $baseDir;
                }
            }
        );
        
// Registers CP Section
        Event::on(
            Cp::class,
            Cp::EVENT_REGISTER_CP_NAV_ITEMS,
            function(RegisterCpNavItemsEvent $event) {
                $event->navItems[] = [
                    'url' => 'password-global/settings',
                    'label' => 'Global Password Protection',
                    'icon' => '@passwordGlobal/assets/password-lock.svg'
                ];
            }
        );

There's also:

  1. A model: src/models/Settings.php
  2. A form template: src/templates/settings/index.html
  3. A controller: src/controllers/SettingsController.php

The controller has a couple actions I defined, but they are kind of useless.

Thank you so much for any help!

1 Answer 1

2

Where do you want to store your data? Looks like you're trying to create a settings form for a module? Your trouble probably comes from trying to build a module like you would a plugin.

Plugin settings

Plugins come with a lot of magic to define, store and read plugin settings (see the documentation for details). The settings are stored in the project config (and a dedicated table) and provided to the plugin on intialization. Modules, however, don't work the same way, so you have to do some steps manually, like finding a place to store your content. Take a look at the last section below (Custom storage solutions) for some pointers if you want to go that way.

Sidenote: If you in fact want to build a plugin, the documentation linked above has everything you need to define your setting fields with default values. Make sure you understand how those interact with the Project Config as well. See Supporting Project Config for details.

Use Global sets for module settings

Modules are site-specific, so if you're building a module, the assumption is that you have control over all aspects of the site (which you don't when building a plugin). You can, for instance, create Global Sets with custom fields that act as settings for your module. The advantage is that you're using built-in components and user interfaces, so you don't have to write your own model, interface or storage solution.

For example, let's say your module requires two fields, global_username and global_password. You can just create those as regular text fields, then create a global set (global_password_settings) with those two fields.

Then you can access the global set from your module and do whatever you want with the fields:

$passwordSettings = \Craft::$app->getGlobals()->getSetByHandle('global_password_settings');
$username = $passwordSettings->global_username;
$password = $passwordSettings->global_password;

Custom storage solutions

Just some quick pointers if you're looking to store more custom data.

  • You can create Custom Elements Types which allow you to use built-in fields and field layouts. Depending on your needs, your custom elements can store their content in the built-in content table or you can roll your own.
  • If you just need a custom database table without any interface (or you want to build a completely custom interface), you can use the Active Record pattern. Yii2 has some base classes you can extend to make this easier. Craft also uses active records for many components that aren't elements.
  • If you want to create your own custom database table from scratch, you can do that in the special enter link description hereplugin install migration.

All those solutions are pretty excessive if all you need is to store two strings.

1
  • 1
    thank you for this clarification. It helps a lot. I indeed had the approach that you mentioned (using global variables) already in place and it's working. But since I'm trying to learn more of craft I decided, as an exercise, to go this route, which implied transferring all the work to the module (including the setting up of the needed fields). I appreciate your notes very much because it gives me a better picture of where a module hits a functionality limit. I think I'll rewind and settle with what I had before. I'm bookmarking your answer! Thanks.
    – mauricio
    Oct 27, 2021 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.