Set new theme setting to default on theme update Working


We have a custom base theme.
There are custom theme settings with default values.
When the theme is initially installed, the theme settings are correctly set as default. Good. 
This custom base theme is now on our many live sites.


Not working


We added a new custom theme setting to the base theme, and set a default value.
When we update the base theme on a site, the new theme setting is there. Good.
However, the default value is not filled in.


Question
How do I set the default value of a new theme setting when the theme is updated?
From what I can tell, themes do not process update hooks since they do not have a schema value tracked in the key_value table. So a hook_update_N is not possible in this case.

This means that you have a couple of options.

Option 1

If the theme is going to be updated in code, but not updated right away through the UI form, you will probably want run a drush command as part of the theme update.

drush config-set THEME.settings NEW_SETTING NEW_SETTING_VALUE'

Reference: https://drushcommands.com/drush-8x/config/config-set/

This will allow you to set it once and then any future updates/overrides can be handled as usual through the configuration system.

Option 2

If you are talking about a theme that is part of a distribution, and you don't have access to the system to run the drush command, then you could set a default value on the hook_form_system_theme_settings_alter function. This would not provide the value to the system when it loads the config, but it would provide a default to users when they update the theme and save the settings form.

If you did it that way, you would need to provide a fallback in your theme for if/when that value is not present to prevent unexpected behavior if the new setting value isn't yet set in the site's settings file.

Option 3

If you have access to the codebases for the affected systems, like in a site factory, then you could technically provide the new value in the exported configuration file itself and then it would be picked up upon config import.