Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programatically creating a content type in Orchard CMS

Tags:

c#

orchardcms

I'm creating an Orchard module and as part of my migration I need to create a new content type. I am able to inject an instance of IContentManager into my migration class which allows me to create new content items but I haven't been able to figure out how to create a new content type.

Can someone describe how this is done (code examples would be great)?

like image 412
joshb Avatar asked Jan 19 '23 09:01

joshb


2 Answers

You don't need to inject content manager. There are many, many examples of that throughout the code. In fact, you can take pretty much any migration, in any module, and find something like this:

ContentDefinitionManager.AlterTypeDefinition("BlogPost",
    cfg => cfg
           .WithPart("BlogPostPart")
           .WithPart("CommonPart", p => p
           .WithSetting("DateEditorSettings.ShowDateEditor", "true"))
           .WithPart("PublishLaterPart")
           .WithPart("RoutePart")
           .WithPart("BodyPart")
);
like image 146
Bertrand Le Roy Avatar answered Jan 22 '23 00:01

Bertrand Le Roy


Ey, here you have another sample of part and content type definition.

// PART DEFINITION
ContentDefinitionManager.AlterPartDefinition("ProductPart", p => p

.WithDescription("Product part")

    // To select other contents ( on this example "ProductPresentation" content type )
.WithField("ProductPresentation", f => f
    .OfType("ContentPickerField")
    .WithDisplayName("Product presentation")
    .WithSetting("ContentPickerFieldSettings.Required", "True")
    .WithSetting("ContentPickerFieldSettings.Multiple", "True")
    .WithSetting("ContentPickerFieldSettings.ShowContentTab", "True")
    .WithSetting("ContentPickerFieldSettings.Hint", "Please select product's presentation")
    .WithSetting("ContentPickerFieldSettings.DisplayedContentTypes", "ProductPresentation")
)

// To select images
.WithField("ProductImageField", f => f
    .OfType("MediaLibraryPickerField")
    .WithDisplayName("Product image")
    .WithSetting("MediaLibraryPickerFieldSettings.Hint", "Please select product's image")
    .WithSetting("MediaLibraryPickerFieldSettings.Required", "True")
    .WithSetting("MediaLibraryPickerFieldSettings.Multiple", "False")

)

// To add a taxonomy field called "ProductLines"
.WithField("Category", fcfg => fcfg
    .OfType("TaxonomyField")
    .WithDisplayName("Category")
    .WithSetting("TaxonomyFieldSettings.Taxonomy", "ProductLines")
    .WithSetting("TaxonomyFieldSettings.LeavesOnly", "True")
    .WithSetting("TaxonomyFieldSettings.Required", "True")
    .WithSetting("TaxonomyFieldSettings.SingleChoice", "False")
    .WithSetting("TaxonomyFieldSettings.Autocomplete", "False")
    .WithSetting("TaxonomyFieldSettings.AllowCustomTerms", "False")
    .WithSetting("TaxonomyFieldSettings.Hint", "Please select product's category")
    )

.Attachable()
);


// CONTENT TYPE DEFINITION
ContentDefinitionManager.AlterTypeDefinition("Product", cfg => cfg
                .DisplayedAs("Product")
                .WithPart(
                    "CommonPart", c => c
                        .WithSetting("DateEditorSettings.ShowDateEditor", "False")
                        .WithSetting("OwnerEditorSettings.ShowOwnerEditor", "False")
                    )
                    .WithPart("ProductPart")
                    .WithPart("TitlePart", c => c.WithSetting("Hint", "Please enter the product name"))
                    .WithPart("BodyPart", c => c.WithSetting("Hint", "Please enter the product description"))
                    .WithPart("LocalizationPart")

                    .WithPart("AutoroutePart", partBuilder => partBuilder
                    .WithSetting("AutorouteSettings.AllowCustomPattern", "true")
                    .WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "true")
                    .WithSetting("AutorouteSettings.PatternDefinitions", "[{Name:'Product Title', Pattern: 'product/{Content.Slug}', Description: 'product/title-product'}]"))

                .Listable()
                .Draftable()
                .Creatable()
                .Securable()
                .Indexed()
                );
like image 20
Sebastián Guerrero Avatar answered Jan 22 '23 01:01

Sebastián Guerrero