Migrating settings to BaseConfig and using custom configs #12686
-
I'm migrating some module settings to use BaseConfig and realizing that things reconcile in a different way. Previously, we could check if a property existed in a file-based config and if it did not, assume that we could fallback to the default on the settings model property. Now, as both the file-based config and default settings properties are an object, all properties exist once the object is created and merging the two objects just results in the file-based config object overwriting everything on the default config. It looks like Craft has a method to merge these objects for general and db configs using If we pass in the value 'custom' as the category as the method seems to want, the switch statement on line 135 works but the custom config file must be named Lines 130 to 147 in a935d09 Am I missing something here? Do all file-based config overrides need to use a I believe this method does what I need if I can target a custom config that is not named custom:
And if the default case return the config that got created (which it currently does not): default:
return $config;
break; Or am I misunderstanding how the new BaseConfig custom config stuff works now? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 3 replies
-
Or perhaps I'm mistaken about the default here. Perhaps what I actually want is that default to allow me to define my own config class: default
$configClass = MyCustomModuleSettings::class;
$envPrefix = '';
break; And that would allow me to also benefit from the rest of the method. |
Beta Was this translation helpful? Give feedback.
-
Ok, I was just told this isn't even supported for plugins yet so perhaps I'm jumping the gun. My use case is a module, and it's mostly working, but I'm struggling to get file-based overrides working without a ridiculous amount of overhead. The issue I am seeing can be tested with the following:
The difference in behavior from the array-based file config overrides is that objects return ALL values on their models with defaults, so, without something like a dirtyAttributes check on the model, it's not straightforward to know if a file-based setting takes precedence over a project-config based setting. |
Beta Was this translation helpful? Give feedback.
-
For fluent configs to be supported in plugins, P&T would need to change a few lines in the https://github.com/craftcms/cms/blob/develop/src/services/Plugins.php#L932 Basically this: $settings = array_merge(
$info['settings'] ?? [],
Craft::$app->getConfig()->getConfigFromFile($handle)
); needs to be something like this: $pluginSettings = Craft::$app->getConfig()->getConfigFromFile($handle);
if ($pluginSettings instanceof BaseConfig) {
$pluginSettings = $pluginSettings->toArray();
}
$settings = array_merge(
$info['settings'] ?? [],
pluginSettings
); untested, but roughly something like that |
Beta Was this translation helpful? Give feedback.
-
What I would do in this instance, and perhaps P&T could implement it at a base layer for plugins at least, would be in the Then to tell if a value is being overridden by a config file value (such as to display a warning in the CP to let the user know), you'd just compare the value from Project Config to the value from the config file If they are different, then the value is being overridden by the config file If the values are the same, then the value is not being overridden by the config file (even if technically it is). I think |
Beta Was this translation helpful? Give feedback.
-
We aren’t ready for plugins to start supporting fluent configs yet. It will be possible in Craft 5, though. |
Beta Was this translation helpful? Give feedback.
-
@brandonkelly In craft 5, will implementing fluent configs for plugins be a matter of extending from |
Beta Was this translation helpful? Give feedback.
We aren’t ready for plugins to start supporting fluent configs yet. It will be possible in Craft 5, though.