mirror of
https://github.com/discourse/discourse.git
synced 2025-10-03 17:21:20 +08:00
DEV: Introduce a value transformer front-end plugin API (#27090)
This commit introduces the `valueTransformer`API to safely override values defined in Discourse. Two new plugin APIs are introduced: - `addValueTransformerName` which allows plugins and theme-components to add a new valid transformer name if they want to provide overridable values; - `registerValueTransformer` to register a transformer to override values. It also introduces the function `applyValueTransformer` which can be imported from `discourse/lib/transformer`. This function marks the desired value as overridable and applies the transformer logic. How does it work? ## Marking a value as overridable: To mark a value as overridable, in Discourse core, first the transformer name must be added to `app/assets/javascripts/discourse/app/lib/transformer/registry.js`. For plugins and theme-components, use the plugin API `addValueTransformerName` instead. Then, in your component or class, use the function `applyValueTransformer` to mark the value as overridable and handle the logic: - example: ```js export default class HomeLogo extends Component { @service session; @service site; ... get href() { return applyValueTransformer("home-logo-href", getURL("/")); } ``` ## Overriding a value in plugins or themes To override a value in plugins, themes, or TCs use the plugin API `registerValueTransformer`: - Example: ```js withPluginApi("1.34.0", (api) => { api.registerValueTransformer("example-transformer", ({ value }) => { return "new-value"; }); }); ```
This commit is contained in:
parent
e5dac3b422
commit
9668592aab
12 changed files with 744 additions and 47 deletions
|
@ -7,6 +7,11 @@ in this file.
|
|||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [1.34.0] - 2024-06-06
|
||||
|
||||
- Added `registerValueTransformer` which allows registering a transformer callback to override values defined in Discourse modules
|
||||
- Added `addValueTransformerName` which allows plugins/TCs to register a new transformer to override values defined in their modules
|
||||
|
||||
## [1.33.0] - 2024-06-06
|
||||
|
||||
- Added `addCustomUserFieldValidationCallback` which allows to set a callback to change the validation and user facing message when attempting to save the signup form.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue