Professional Documents
Culture Documents
Adding Custom Settings - Orchard Documentation
Adding Custom Settings - Orchard Documentation
About Us
Orchard 1.8 drastically simplifies creation of site settings, removing the previous need for Defining site scope settings (Orchard 1.8 Onw
"Part Records" and migration files. To create new site settings for your module you now The Content Part
only need three classes; A ContentPart, a Handlerand potentially a view file if you want The Handler
the settings to be edited via the "Site Settings" area of Admin. For a real world example The View
look for the RegistrationSettingsPart, RegistrationSetttingsPartHandlerand Using site scope settings
Users.RegistrationSettings.cshtmlfiles in the Orchard.Usersmodule.
Defining site scope settings (Pre-Orchard 1.8)
The Handler
[UsedImplicitly]
public class ShareBarSettingsPartHandler : ContentHandler {
public ShareBarSettingsPartHandler() {
T = NullLocalizer.Instance;
Filters.Add(new ActivatingFilter<ShareBarSettingsPart>("Site"));
Filters.Add(new TemplateFilterForPart<ShareBarSettingsPart>("ShareBarSettings", "Parts/ShareBar.ShareBarSettings",
}
}
}
The View
@model Szmyd.Orchard.Modules.Sharing.Models.ShareBarSettingsPart
<fieldset>
<legend>@T("Content sharing settigs")</legend>
<div>
@Html.LabelFor(m => m.AddThisAccount, @T("AddThis service account"))
@Html.EditorFor(m => m.AddThisAccount)
@Html.ValidationMessageFor(m => m.AddThisAccount, "*")
</div>
</fieldset>
"I want all of my Blog Posts to have ShareBar with the same look."
Imagine that you're writing posts via LiveWriter (like me). Do you want to log in and edit
every post after publishing just to change the share bar? I don't. I want to have it defined
upfront.
The first thing you have to do is to create a class holding your settings:
This class has one property - Mode, which holds the default mode for all ShareBarParts
attached to some content items of a given type. ShareBarModeis just an enum type defining
the display modes. For the sake of brevity, I won't paste the code here. This could be any
type you want. The second property is just used for the display purposes (holds items for
display in drop-down list), as this class is also used as a ViewModel. It is not being persisted.
The second class can be thought of as a kind of a driver for the settings. It's not the Orchard
ContentDriver we wrote previously, but also it's responsible for rendering the edit form and
saving the posted data:
http://docs.orchardproject.net/Documentation/Adding-custom-settings 2/4
8/7/2015 Adding custom settings - Orchard Documentation
model.AvailableModes = Enum.GetValues(typeof(ShareBarMode))
.Cast<int>()
.Select(i =>
new {
Text = Enum.GetName(typeof(ShareBarMode), i),
Value = i
});
If you're done with the code above, there's only one thing left: creating a .cshtml view
(shape) to render our form.
Shapes defining edit forms for content type settings has to be placed under
/Views/DefinitionTemplates/with the name <settingsClassName>.cshtml. So in our
case it'll look like on the picture above. Inside, it's just like any other Razor view file:
@model Szmyd.Orchard.Modules.Sharing.Settings.ShareBarTypePartSettings
<fieldset>
<div>
@Html.LabelFor(m => m.Mode, T("Share bar display style"))
@Html.DropDownListFor(m => m.Mode,
new System.Web.Mvc.SelectList(
Model.AvailableModes, "Value", "Text", (int)Model.Mode))
</div>
</fieldset>
This renders the dropdown list so user can choose one of the predefined Modes.
Model.AvailableModescontains the available ones: we populated the property with
appropriate data in TypePartEditormethod above.
Hooray, we're done!
Now you possibly wonder, where will this edit form be shown? Orchard will render settings
form when you'll try to edit the content type containing your part. The steps are like this:
Content Types -> Edit the type you want -> Add your part -> Voila!There is
a nice arrow image next to your part. If you click it - the form shows.
You can retrieve and use the settings wherever you have access to your part (particularly in
the driver Display/Editor methods, but also shapes, handlers and so on). I used it in the
ShareBarPart driver Display method to change the look of a share bar part.
Home Contact Us About Us Terms Of Use Privacy Statement Hosted and Powered by AppHarbor
http://docs.orchardproject.net/Documentation/Adding-custom-settings 4/4