Skip to Content
Module developmentModule basics

Module settings

How a module describes its settings with #[AdminSettings], where the owner finds them, and how code reads the saved values.

By the end your module has its own settings page in the admin panel: the core draws the fields, validates and saves the values, and the module reads them with config(). You write no form and no save handler.

Minimal example

Two files: the defaults and the field descriptions.

app/Modules/Example/Resources/config/example.php
<?php

return [
    'enabled' => true,
    'title' => '',
    'per_page' => 20,
    'mode' => 'manual',
    'api_key' => '',
];
app/Modules/Example/Surfaces/ExampleSettings.php
<?php

declare(strict_types = 1);

namespace Flute\Modules\Example\Surfaces;

use Flute\Admin\UI\Settings\SettingDefinition;
use Flute\Admin\UI\Settings\SettingsProviderInterface;
use Flute\Admin\UI\Settings\SettingsRegistry;
use Flute\Admin\UI\Settings\SettingsSection;
use Flute\Core\Surfaces\Attributes\AdminSettings;

#[AdminSettings(order: 90, permission: 'admin.example')]
final class ExampleSettings implements SettingsProviderInterface
{
    public const SECTION = 'example';

    private const T = 'example-admin.settings.';

    public function register(SettingsRegistry $registry): void
    {
        $registry->addSection(
            SettingsSection::make(self::SECTION)
                ->label(self::T . 'title')
                ->description(self::T . 'description')
                ->icon('sl.linear.settings'),
        );

        $registry->add(
            $this->def('enabled', 'access')->control('switch')->default(true),
            $this->def('title', 'look')->translatable()->props(['maxlength' => 80])->default(''),
            $this
                ->def('per_page', 'look')
                ->control('number')
                ->props(['min' => 5, 'max' => 100, 'step' => 1])
                ->default(20),
            $this
                ->def('mode', 'access')
                ->control('select')
                ->options(['manual' => self::T . 'mode.manual', 'auto' => self::T . 'mode.auto'])
                ->default('manual')
                ->when('example_enabled', '1'),
            $this->def('api_key', 'access')->control('password')->sensitive()->advanced(),
        );
    }

    private function def(string $key, string $group): SettingDefinition
    {
        return SettingDefinition::make('example.' . $key)
            ->section(self::SECTION)
            ->input('example_' . $key)
            ->group(self::T . 'groups.' . $group)
            ->label(self::T . $key)
            ->help(self::T . $key . '_help');
    }
}

Reading a value in module code:

use Flute\Core\Localization\LocaleText;

$perPage = (int) config('example.per_page', 20);
$title = LocaleText::configText('example.title');

Where the owner finds them

The module gets its own page, /admin/modules/<Key>/settings; for the example above that is /admin/modules/Example/settings. Every section registered by the module's classes lands there.

If the module has a sidebar row with several screens, the settings become its last tab, "Settings". A module with a single screen gets a "Settings" button in that screen's header instead. A module with no screens in the sidebar opens its settings from its card in AdminExtensionsModules. Adding a sidebar row is covered in module admin.

A section with ->navGroup('<group key>') moves off the module page into the site-wide AdminSettings, under the group with that name. You rarely need it: owners look for a module's settings next to the module.

How it works

The class with the #[AdminSettings] attribute lives in the module's Surfaces/ folder. bootstrapModule() scans that folder and puts the class into SurfaceRegistry under the flute.admin.settings key. The class itself is built only when the admin needs the settings list: when a settings page opens, during admin search, when the "Settings" button is checked. So register() runs in admin web requests and not in the console or on the public site.

register() can run several times per request, so keep it cheap. Pass a heavy option list as a closure, ->options(fn () => ...), and it is called only when the field is drawn.

The module keeps its defaults in Resources/config/<name>.php. The file name becomes the first part of the key: example.php gives example.* keys. The core never writes that file. A save writes only the changed keys into config/example.php at the site root, and on load they are laid over the module's values. After a save the core dispatches SettingsSavedEvent::NAME (flute.settings.saved) once per changed config file.

Permissions

permission on the attribute does two things: the class registers no sections for someone without that permission, and its sections take that permission as their own. Without permission, only holders of admin.system can edit the section. A section can require a separate permission with ->permission('admin.example.settings'); the section's own permission wins over the attribute's.

Reference

SettingDefinition::make(string $configKey), with a key like example.per_page:

MethodDefaultWhat it does
section(string $id)''which section the field belongs to
input(string $name)last part of the keyform field name. Prefix it with the module: example_per_page
group(string $key), label(), help(), placeholder()''translation keys: group subheading, label, hint, placeholder
control(string $control)inputfield type, list below
options(array|callable $options)[]choices for select, multiselect, chips, buttongroup. Labels may be translation keys
props(array $props)[]field props: min, max, step, maxlength, source
default(mixed $value)nullvalue used when the config has none
required()falsethe field is required
sensitive()falsechanging it asks for the password again
translatable()falseone text per site language, read with LocaleText::configText()
advanced()basic tierthe field goes under "advanced"
when(string $input, ?string $value)noneshown while field $input equals $value. For a switch the value is '1'
within(string $input, ?string $value)nonean outer condition around when()
lockedWhen(string $input, ?string $value, string $hintKey)nonethe field stays visible but locked, with a hint
address(string $default)nonea section address field: checked for collisions, the old address keeps redirecting

control types: input, number, url, email, password, switch, select, multiselect, chips, buttongroup, list, repeater, textarea, richtext, color, date, datetime, icon, media. A select or multiselect with ->props(['source' => 'roles']) becomes a picker over the site's records.

SettingsSection::make(string $id): label(), description(), icon(), order(int), permission(string), navGroup(string). Also banner($input, $value, $messageKey, $tone) for a warning while a field equals a value, pill($input, $okKey, $offKey) for a status next to a switch, and action(new SettingsAction(...)) for a button that runs an action and stores nothing.

The attribute is #[AdminSettings(order: int = 0, permission: ?string = null)].

Common mistakes

The module writes settings into its own Resources/config/*.php at runtime. A module update replaces the folder whole, and what was written is gone. Saves always go to config/<name>.php, through the settings page or config()->set() followed by config()->save().

A password field is never drawn back. An empty field on save keeps the old value; the only way to remove it is the clear checkbox, which posts <input>__clear.

The save refuses, with an error next to the field, a fraction in a field with an integer step, or a single value instead of a list for multiselect or chips. Values outside options() of a closed multiselect or chips are dropped.

A new class in Surfaces/ does not show up. Outside debug mode the module's folder list is cached; run php flute cache:clear.

React to a save with a SettingsSavedEvent::NAME listener. Saves come from the admin, the console and the API, and the listener sees all three.

Learn more

On this page