Here are some helpers to use when adding fields to content parts.

Topics: Announcements, Customizing Orchard
Jul 31, 2014 at 1:07 AM
Edited Jul 31, 2014 at 1:10 AM
These helpers are useful when creating a lot of content parts that have fields. I became tired of typing out the string definitions of the field settings. Here is how to use the helpers.
ContentDefinitionManager.AlterPartDefinition(typeName, builder => builder
.WithDescription("Contains fields for the News Item Content Type.")
.WithField(
  "Date", 
  BuildDateTimeField("Date", DateTimeFieldDisplays.DateAndTime, "Add the date.", false)));
Here are the helpers:
private Action<ContentPartFieldDefinitionBuilder> BuildLinkField(string displayName, string hint, bool required, TargetMode targetMode, LinkTextMode linkTextMode, string staticText)
{
    return new Action<ContentPartFieldDefinitionBuilder>(field => field
        .OfType("LinkField")
        .WithDisplayName(displayName)
        .WithSetting("LinkFieldSettings.Hint", hint)
        .WithSetting("LinkFieldSettings.Required", required.ToString(CultureInfo.InvariantCulture))
        .WithSetting("LinkFieldSettings.TargetMode", targetMode.ToString())
        .WithSetting("LinkFieldSettings.LinkTextMode", linkTextMode.ToString())
        .WithSetting("LinkFieldSettings.StaticText", staticText));
}

private Action<ContentPartFieldDefinitionBuilder> BuildTextField(string displayName, string flavor, bool required, string hint)
{
    return new Action<ContentPartFieldDefinitionBuilder>(field => field
        .OfType("TextField")
        .WithDisplayName(displayName)
        .WithSetting("TextFieldSettings.Flavor", flavor)
        .WithSetting("TextFieldSettings.Required", required.ToString(CultureInfo.InvariantCulture))
        .WithSetting("TextFieldSettings.Hint", hint));
}

private Action<ContentPartFieldDefinitionBuilder> BuildMediaLibraryPickerField(string displayName, string hint, bool required, bool multiple, string displayedContentTypes)
{
    return new Action<ContentPartFieldDefinitionBuilder>(field => field
        .OfType("MediaLibraryPickerField")
        .WithDisplayName(displayName)
        .WithSetting("MediaLibraryPickerFieldSettings.Hint", hint)
        .WithSetting("MediaLibraryPickerFieldSettings.Required", required.ToString(CultureInfo.InvariantCulture))
        .WithSetting("MediaLibraryPickerFieldSettings.Multiple", multiple.ToString(CultureInfo.InvariantCulture))
        .WithSetting("MediaLibraryPickerFieldSettings.DisplayedContentTypes", displayedContentTypes));
}

private Action<ContentPartFieldDefinitionBuilder> BuildInputField(string displayName, string type, string title, string hint, bool required, bool autoFocus, bool autoComplete, string placeHolder, string editorCssClass, int maxLength, string pattern)
{
    return new Action<ContentPartFieldDefinitionBuilder>(field => field
        .OfType("InputField")
        .WithDisplayName(displayName)
        .WithSetting("InputFieldSettings.Type", type)
        .WithSetting("InputFieldSettings.Title", title)
        .WithSetting("InputFieldSettings.Hint", hint)
        .WithSetting("InputFieldSettings.Required", required.ToString(CultureInfo.InvariantCulture))
        .WithSetting("InputFieldSettings.AutoFocus", autoFocus.ToString(CultureInfo.InvariantCulture))
        .WithSetting("InputFieldSettings.AutoComplete", autoComplete.ToString(CultureInfo.InvariantCulture))
        .WithSetting("InputFieldSettings.PlaceHolder", placeHolder)
        .WithSetting("InputFieldSettings.EditorCssClass", editorCssClass)
        .WithSetting("InputFieldSettings.MaxLength", maxLength.ToString(CultureInfo.InvariantCulture))
        .WithSetting("InputFieldSettings.Pattern", pattern));
}

private Action<ContentPartFieldDefinitionBuilder> BuildDateTimeField(string displayName, DateTimeFieldDisplays display, string hint, bool required)
{
    return new Action<ContentPartFieldDefinitionBuilder>(field => field
            .OfType("DateTimeField")
            .WithDisplayName(displayName)
            .WithSetting("DateTimeFieldSettings.Display", display.ToString())
            .WithSetting("DateTimeFieldSettings.Hint", hint)
            .WithSetting("DateTimeFieldSettings.Required", required.ToString(CultureInfo.InvariantCulture)));
}

private Action<ContentPartFieldDefinitionBuilder> BuildTaxonomyField(string displayName, bool allowCustomTerms, string taxonomy, bool leavesOnly, bool singleChoice, bool autoComplete, string hint, string taxonomies, bool required)
{
    return new Action<ContentPartFieldDefinitionBuilder>(field => field
            .OfType("TaxonomyField")
            .WithDisplayName(displayName)
            .WithSetting("TaxonomyFieldSettings.AllowCustomTerms", allowCustomTerms.ToString(CultureInfo.InvariantCulture))
            .WithSetting("TaxonomyFieldSettings.Taxonomy", taxonomy)
            .WithSetting("TaxonomyFieldSettings.LeavesOnly", leavesOnly.ToString(CultureInfo.InvariantCulture))
            .WithSetting("TaxonomyFieldSettings.SingleChoice", singleChoice.ToString(CultureInfo.InvariantCulture))
            .WithSetting("TaxonomyFieldSettings.AutoComplete", autoComplete.ToString(CultureInfo.InvariantCulture))
            .WithSetting("TaxonomyFieldSettings.Hint", hint)
            .WithSetting("TaxonomyFieldSettings.Taxonomies", taxonomies)
            .WithSetting("TaxonomyFieldSettings.Required", required.ToString(CultureInfo.InvariantCulture)));
}
Aug 6, 2014 at 3:21 AM
Outstanding stuff. Thanks!
Coordinator
Aug 6, 2014 at 7:19 PM
I think this kind of extensions should be somewhere in orchard by default.
  • They should be extension methods so they integrate in the current migration system.
  • They should be in their corresponding modules, so that devs would just reference the Fields module to add fields
  • They should be documented because people are always asking how to create these fields from code/migrations
Shaun, feel free to start a PR for that, and we would comment on this.

In theory, everything which require settings should have extension method helpers.
Coordinator
Aug 6, 2014 at 7:20 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Developer
Aug 17, 2014 at 10:32 PM
@ShaunLuttin, send in that pull request.
Sep 16, 2014 at 5:17 PM
Sorry I hadn't replied to this. My email notifications were broken. I fixed them today and found a whole bunch of Codeplex stuff that I missed.