outl1ne / nova-settings

A Laravel Nova tool for editing custom settings using native Nova fields.
MIT License
273 stars 97 forks source link

About the case when field labels use localization #53

Closed alexrififi closed 1 year ago

alexrififi commented 3 years ago

Example of registering fields from the documentation:

\OptimistDigital\NovaSettings\NovaSettings::addSettingsFields([
    Text::make('Some setting', 'some_setting'),
    Number::make('A number', 'a_number'),
]);

If the field labels use localization, then an example would be like this:

\OptimistDigital\NovaSettings\NovaSettings::addSettingsFields([
    Text::make(__('Some setting'), 'some_setting'),
    Number::make(__('A number'), 'a_number'),
]);

But if localization is determined by middleware (from browser or user settings), then label localization won't work. Because the code from \App\Providers\NovaServiceProvider::boot will work before middleware.

For the fields to become localized, you need to set the settings fields when fired \Laravel\Nova\Events\ServingNova event, like this

use Laravel\Nova\Nova;
use OptimistDigital\NovaSettings\NovaSettings;
...

Nova::serving(function () {
    \OptimistDigital\NovaSettings\NovaSettings::addSettingsFields([
        Text::make(__('Some setting'), 'some_setting'),
        Number::make(__('A number'), 'a_number'),
    ]);
});

This is not a bug, just a note 🙂

diezg commented 2 years ago

I found that casts does not works when you pass as the second parameter:

Nova::serving(function () {
    NovaSettings::addSettingsFields([], $casts);
});

To get it works you should not use second parameter and instead write outside of the Nova::serving() function, like this:

Nova::serving(function () {
    NovaSettings::addSettingsFields([]);
});
NovaSettings::addCasts(['key' => 'boolean']);

Thanks a lot!

Tarpsvo commented 2 years ago

Yeah, if we wait for the Nova::serving to run, the casts might not really be there before that event. That is the correct solution in that case.