Adding a Field to a ContentType in a Orchard DataMigration

前端 未结 1 2167
太阳男子
太阳男子 2021-02-20 06:18

I\'ve created a ContentType in my data migration that welds several ContentParts together.

On the Orchard Site Content Admin I can add a field to the ContentType (but no

相关标签:
1条回答
  • 2021-02-20 06:34

    You actually can't attach a field to a content type. When you attach it to a content type in the admin UI, Orchard does some magic behind the scenes to hide this fact from you -- it creates a content part inside that content type, with the same name as the content type, and then attaches the field(s) to that new content part.

    You can verify this by attaching a field via the admin UI, and then going to Import/Export, and exporting the metadata for your content types.

    To attach a field via a Migration, do the same thing. If you don't have a content part that is a good place to attach the field, the convention I use is tocreate one with the same name as the Content Type, bug suffixed with "Part". So let's say your content type is "VideoGame":

    ContentDefinitionManager.AlterPartDefinition(
        "VideoGamePart"
        , b => b
            .Attachable()
            .WithField("ThumbnailImage", cfg => cfg.OfType("MediaPickerField").WithDisplayName("Video game box cover image"))
    );
    // Type: 
    ContentDefinitionManager.AlterTypeDefinition(
        "VideoGame"
        , cfg =>
            cfg
                .WithPart("VideoGamePart")
                .WithPart("IdentityPart")
                .WithPart("TitlePart")
                .WithPart("CommonPart")
                .Creatable()
    );
    

    All fields are attached to Parts, not Types, so you naturally can control placement with placement.info and templates using this migration method, as you can if you define the field through the UI.

    0 讨论(0)
提交回复
热议问题