Adding contentFields to a content type during a migration

Topics: Customizing Orchard, Writing modules
Mar 11, 2012 at 5:53 AM

Hi:

I am in the process of needing to add data to the user part to use with the profile module.

I'm modelling Gender so am wanting to use an enum field.

How do I go about setting this up during a migration?

I'd like to alter the display mode to radio buttons, make it required and change the name of the field along with it's options.

Any help apreciated.

Regards

Sean. 

Mar 12, 2012 at 3:34 PM

Hi Sean,

Fields are added to Parts in Orchard. So you want to use the AlterPartDefinition method of the ContentDefinitionManager in your migration. The other key piece to know is that field settings are stored in XML strings in the Settings_ContentPartFieldDefinitionRecord table in Orchard. So the API for defining a field in the migrations is very string-based. The basic pattern is to look for the *Settings class that corresponds to the field type that you want. For example, you're wanting to setup an EnumerationField so there is a corresponding class called EnumerationFieldSettings. You set the properties on that class (as strings) to setup the field how you want in your migration. Here's what the code would look like for your situation:

string options = string.Format("Male{0}Female", Environment.NewLine);

ContentDefinitionManager.AlterPartDefinition("ProfilePart", builder =>
    builder.WithField("Gender", cfg =>
        cfg
            .OfType("EnumerationField")
            .WithDisplayName("Gender")
            .WithSetting("EnumerationFieldSettings.Required", "True")
            .WithSetting("EnumerationFieldSettings.ListMode", "Radiobutton")
            .WithSetting("EnumerationFieldSettings.Options", options)
    )
);
In this case, the Options property is a little weird. The options are separated by new lines, so I used the string.Format to set that up. Hopefully this helps.

Mar 12, 2012 at 3:40 PM

Hi:

This does help tremendously, thanks. Out of interest, where did you find that snipit of info, in the docs?

Cheers

Sean.

From: kevink [email removed]
Sent: 12 March 2012 15:34
To: Sean Farrow
Subject: Re: Adding contentFields to a content type during a migration [orchard:348150]

From: kevink

Hi Sean,

Fields are added to Parts in Orchard. So you want to use the AlterPartDefinition method of the ContentDefinitionManager in your migration. The other key piece to know is that field settings are stored in XML strings in the Settings_ContentPartFieldDefinitionRecord table in Orchard. So the API for defining a field in the migrations is very string-based. The basic pattern is to look for the *Settings class that corresponds to the field type that you want. For example, you're wanting to setup an EnumerationField so there is a corresponding class called EnumerationFieldSettings. You set the properties on that class (as strings) to setup the field how you want in your migration. Here's what the code would look like for your situation:

string options = string.Format("Male{0}Female", Environment.NewLine);
 
ContentDefinitionManager.AlterPartDefinition("ProfilePart", builder =>
    builder.WithField("Gender", cfg =>
        cfg
            .OfType("EnumerationField")
            .WithDisplayName("Gender")
            .WithSetting("EnumerationFieldSettings.Required", "True")
            .WithSetting("EnumerationFieldSettings.ListMode", "Radiobutton")
            .WithSetting("EnumerationFieldSettings.Options", options)
    )
);

In this case, the Options property is a little weird. The options are separated by new lines, so I used the string.Format to set that up. Hopefully this helps.

Mar 12, 2012 at 3:50 PM

No, just experience. :-)

I'm actually not sure if this is documented anywhere. It's just something that I've done before and probably learned by seeing it done in another module somewhere. One tip if you're not sure how to format the property names and values in those WithSetting() calls is to just add the field with the settings you want to some content part using the dashboard UI. Then go look in that table I mentioned (Settings_ContentPartFieldDefinitionRecord) to see how it's formatted there.