Skip to content

Custom Editor Settings for Editor Templates

yohsii edited this page Sep 17, 2019 · 7 revisions

when developing editor templates for your ViewModels, you may require customisation for your editor templates. for example, the PuckPicker editor template, which is used for content picking, has editor settings to determine things such as how many items a user is allowed to pick.

these settings are then edited in the Settings section of the admin backoffice on the "Editor Parameters" page. settings are regular classes that implement the I_Puck_Editor_Settings interface which is in the puck.core.Abstract namespace. these settings are then saved against a ViewModel type and optionally against a Property of that ViewModel.

let's use the PuckPicker editor template again as a reference, in the source code for this editor template, settings are retrieved at the top of the file with this code (which is found here - /areas/admin/views/shared/editortemplates/puckpicker.cshtml):

var settings = this.PuckEditorSettings<PuckPickerEditorSettings>()
        ??new PuckPickerEditorSettings(){
            AllowDuplicates=false,AllowUnpublished=true,MaxPick=2,SelectionType="both",StartPath=null
        };
    

as you can see, settings of type PuckPickerEditorSettings are requested and if they don't exist, defaults are set. your editor settings can be any regular class, it just needs to implement I_Puck_Editor_Settings for it to be editable in the backoffice.

there are some important things to note about how these settings are retrieved. when this.PuckEditorSettings<T>() is called, it will attempt to retrieve the stored settings of type T for the type of ViewModel being edited by the user. so if the user is editing a Homepage ViewModel, it will try to find editor settings of type T stored against the ViewModel type Homepage. it will also take into account the current property being edited, so if the property being edited is called RelatedContent it will try to retrieve the settings of type T for the ViewModel Homepage and the property RelatedContent. it works recursively. if a setting is not found for the current ViewModel and Property being edited, it will look for just the ViewModel and if that returns nothing, it will look for the base type of the ViewModel until it gets all the way to BaseModel.

so a good way to save global settings for all ViewModels is to store the editor settings against the BaseModel type.

Clone this wiki locally