Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a Field to a ContentType in a Orchard DataMigration

Tags:

orchardcms

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 not a ContentPart), and in the data migration it only seems possible to add a field to a ContentPart (and not a ContentType).

I would like to add the field to the ContentType in the migration, so I can control it's placement using placement.info.

Perhaps that's not important, and there is another way to achieve adding a field in the migration and then being able to control where it is placed using placement.info and how it looks using a template.

like image 767
Richard Garside Avatar asked Jan 28 '13 18:01

Richard Garside


1 Answers

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.

like image 99
Giscard Biamby Avatar answered Dec 31 '22 09:12

Giscard Biamby