Widget / Part Settings persistence

Feb 14, 2011 at 2:24 PM

Hi,

How are settings for Widgets persisted?

I have the following in my Driver for reading/updating the settings:

        protected override DriverResult Editor(FourCornerWidgetPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            return ContentShape(
                "Parts_TheFA_Orchard_Widgets_FourCornerWidget_Edit", () =>
                {
                    var model = part.Settings.GetModel<FourCornerWidgetPartSettings>();

                    if (updater != null) {
                        updater.TryUpdateModel(model, Prefix, null, null);
} return shapeHelper.EditorTemplate(TemplateName: "Parts/TheFA.Orchard.Widgets.FourCornerWidget", Model: model, Prefix: Prefix); }); }

I've created the following Editor Events Class looking at existing code, but it never gets called... Any ideas on what is likely missing?

    public class FourCornerWidgetPartEditorEvents : ContentDefinitionEditorEventsBase
    {
        public override IEnumerable<TemplateViewModel> PartEditorUpdate(ContentPartDefinitionBuilder builder, IUpdateModel updateModel)
        {
            if (builder.Name != "FourCornerWidgetPart")
                yield break;

            var model = new FourCornerWidgetPartSettings();
            updateModel.TryUpdateModel(model, "FourCornerWidgetPart", null, null);
            builder.WithSetting("FourCornerWidgetPartSettings.TechnicalUrl", model.TechnicalUrl);
            builder.WithSetting("FourCornerWidgetPartSettings.PsychologicalUrl", model.TechnicalUrl);
            builder.WithSetting("FourCornerWidgetPartSettings.PhysicalUrl", model.TechnicalUrl);
            builder.WithSetting("FourCornerWidgetPartSettings.SocialUrl", model.TechnicalUrl);
            yield return DefinitionTemplate(model);
        }
    }
Thanks,

 

 

 

 

 

 

Developer
Feb 14, 2011 at 3:38 PM

Hi!

The settings for content parts (widgets also) are persisted in the DB in the XML data column within the record of the defined content type (<prefix>_Settings_ContentPartTypeDefinitionRecord table).

You don;t have to save/update the settings within the driver, as the driver corresponds to the instance of content part itself, and the settings you define to the content type the part is defined on. In other word - theta are two different things. Each instance of a content part you create has the settings of it's content type it's part of.

The settings won't update inside the driver, as the TryUpdateModel just tries to update your part and it's underlying record, but not settings, because they are in completely different scope.

If you want to edit/update settings than override the second method:

public override IEnumerable<TemplateViewModel> TypePartEditor(ContentTypePartDefinition definition)
        {
            if (definition.PartDefinition.Name != "YourPart")
                yield break;
            var model = definition.Settings.GetModel<YourTypePartSettings>();
            yield return DefinitionTemplate(model);
        }

 And provide the corresponding shape for the editor under /Views/DefinitionTemplates/YourTypPartSettings.cshtml

Think of the class you overridden:

public class FourCornerWidgetPartEditorEvents : ContentDefinitionEditorEventsBase
    {
...
}

 as of a driver for the settings - basically it's just that. The editor for your settings will show when you try to edit the content item and add/remove parts from it.

Cheers, Piotr