0
votes

I am attempting to add a taxonomy to a custom type in orchard cms.

ContentDefinitionManager.AlterPartDefinition("ExpertPart", 
                     b => b
                    .WithField("ExpertOf", fld => fld            
                                                .OfType("TaxononmyField")
                                                .WithDisplayName("Expert Of")
                                                .WithSetting("TaxonomyFieldSettings.Taxonomy", "ExpertOf")
                                                .WithSetting("TaxonomyFieldSettings.LeavesOnly", "false")
                                                .WithSetting("TaxonomyFieldSettings.SingleChoice", "true")
                                                .WithSetting("TaxonomyFieldSettings.Required", "true")));

When i run this code the taxonomy shows up under "parts" and not "fields" in the content definition. I can manually add this to fields and it works fine. What is the new migration code for Orchard 1.10 that allows you to add a taxonomy field programatically to a custom content type?

Thanks for your help with this! So I have tried this.

ContentDefinitionManager.AlterTypeDefinition("Expert",
                                b => b.WithPart("ExpertPart"));

ContentDefinitionManager.AlterPartDefinition("ExpertPart",
                     b => b
                    .WithField("ExpertOf", fld => fld
                        .OfType("TaxononmyField")
                        .WithDisplayName("ExpertOf")
                        .WithSetting("TaxonomyFieldSettings.Taxonomy", "ExpertOf")
                        .WithSetting("TaxonomyFieldSettings.LeavesOnly", "false")
                        .WithSetting("TaxonomyFieldSettings.SingleChoice", "true")
                        .WithSetting("TaxonomyFieldSettings.Required", "true")));

When i run this migration, then go into the content definition of the part. The expert of is not listed under "fields" in the content definition. It it listed under "Parts". You can not get to the taxonomy settings. This is what happens

Orchard Problems

Also, the content edit screen does not have the taxonomy listed. So i cannot attach the expert part to the taxonomy.

1
You have a typo in your code: .OfType("TaxononmyField")Lawyerson
Thanks for that! It was by problem all along.Jeff

1 Answers

0
votes

You do this:

ContentDefinitionManager.AlterPartDefinition("ExpertPart", ...

So you say: Add the field to the ExpertPart. When you add a field manually in the dashboard to the 'Fields' section of a content type, Orchard adds it to the part with the same name of the content type (which is implicitly created with a content type). NOTE: This part does not really exists, but orchard creates it on the fly when the type is loaded.

So, assuming your custom content type is called 'Expert', Orchard adds the field in the dashboard to the part called 'Expert'.

Therefore, to let your field show up under Fields instead of under a part, your migrations should look like this:

// Orchard can only handle these migrations if you explicitly add the
// Expert part to the Expert content type
ContentDefinitionManager.AlterTypeDefinition("Expert", type => type
    .WithPart("Expert"));

// Add the field to the part
ContentDefinitionManager.AlterPartDefinition("Expert", part => part
    .WithField("ExpertOf", fld => fld
        .OfType("TaxononmyField"));