discourse/app/services/themes/create.rb
Martin Brennan 19af83d39e
FEATURE: Themeable site settings (#32233)
This commit introduces the concept of themeable site settings,
which is a new tool for theme authors that lives alongside theme
modifiers and theme settings. Here is a quick summary:

* Theme settings - These are custom settings used to control UI and functionality within your theme or component and provide configuration options. These cannot change core Discourse functionality.
* Theme modifiers - Allows a theme or a component to modify selected server-side functionality of core Discourse as an alternative to building a plugin.
* Themeable site settings (new) - Allows a theme (not components) to override a small subset of core site settings, which generally control parts of the UI and other minor functionality. This allows themes to have a greater control over the full site experience.

Themeable site settings will be shown for all themes, whether the theme
changes
the value or not, and have a similar UI to custom theme settings.

We are also introducing a new page at
`/admin/config/theme-site-settings` that
allows admins to see all possible themeable site settings, and which
themes
are changing the value from the default.

### Configuration

Theme authors can configure initial values themeable site settings using
a section in the `about.json` file like so:

```json
"theme_site_settings": {
  "search_experience": "search_field"
}
```

These values will not change when the theme updates, because we cannot
know if admins have manually changed them.

### Limitations

Themeable site settings are only really intended to control elements of
the UI, and when retrieving their value we require a theme ID, so these
limitations apply:

- Themeable site settings cannot be used in Sidekiq jobs
- Themeable site settings cannot be used in markdown rules
- Themeable site settings will be cached separately to client site
settings using theme ID as a key
- Themeable site settings will override keys on the `siteSettings`
service on the client using the application preloader
- `SiteSetting.client_settings_json` will not include themeable site
settings, instead you can call `SiteSetting.theme_site_settings_json`
with a theme ID

### Initial settings

There are only two site settings that will be themeable to begin with:

* `enable_welcome_banner`
* `search_experience`

And our new Horizon theme will take advantage of both. Over time, more
settings that control elements of the UI will be exposed this way.
2025-07-16 11:00:21 +10:00

91 lines
2.8 KiB
Ruby

# frozen_string_literal: true
# Creates a new theme with the provided parameters. Themes can be created
# with various attributes including name, user selectability, color scheme,
# and theme fields.
#
# Also used to create theme components.
#
# The theme can optionally be set as the default theme, overriding SiteSetting.default_theme_id.
# The theme will then be used for all users on the site who haven't specifically set their
# theme preference.
#
# @example
# Themes::Create.call(
# guardian: guardian,
# params: {
# name: "My Theme",
# user_selectable: true,
# color_scheme_id: 1,
# component: false,
# theme_fields: [
# { name: "header", target: "common", value: "content", type_id: 1 }
# ],
# default: false
# }
# )
#
class Themes::Create
include Service::Base
# @!method self.call(guardian:, params:)
# @param [Guardian] guardian
# @param [Hash] params
# @option params String :name The name of the theme
# @option params Integer :user_id The ID of the user creating the theme
# @option params [Boolean] :user_selectable Whether the theme can be selected by users
# @option params [Integer] :color_scheme_id The ID of the color palette to use
# @option params [Boolean] :component Whether this is a theme component. These cannot be user_selectable or have a color_scheme_id
# @option params [Array] :theme_fields Array of theme field attributes
# @option params [Boolean] :default Whether to set this as the default theme
# @return [Service::Base::Context]
params do
attribute :name, :string
attribute :user_id, :integer
attribute :user_selectable, :boolean, default: false
attribute :color_scheme_id, :integer
attribute :component, :boolean, default: false
attribute :theme_fields, :array
attribute :default, :boolean, default: false
validates :name, presence: true
validates :user_id, presence: true
validates :theme_fields, length: { maximum: 100 }
end
policy :ensure_remote_themes_are_not_allowlisted
transaction do
model :theme, :create_theme
step :update_default_theme
step :log_theme_change
end
step :refresh_site_setting_cache
private
def ensure_remote_themes_are_not_allowlisted
Theme.allowed_remote_theme_ids.nil?
end
def create_theme(params:)
Theme.create(
params.slice(:name, :user_id, :user_selectable, :color_scheme_id, :component),
) { |theme| params.theme_fields.to_a.each { |field| theme.set_field(**field.symbolize_keys) } }
end
def update_default_theme(params:, theme:)
theme.set_default! if params.default
end
def log_theme_change(theme:, guardian:)
StaffActionLogger.new(guardian.user).log_theme_change(nil, theme)
end
def refresh_site_setting_cache
SiteSetting.refresh!(refresh_site_settings: false, refresh_theme_site_settings: true)
end
end