This project is read-only.

TaxonomyField in a ContentType

Topics: Customizing Orchard, Troubleshooting
Aug 23, 2011 at 10:20 AM

Hello All!

I'm able to programatically create the TaxonomyField under a ContentPart with the below code, but I would like it to appear instead on the ContentType under "Fields" section.

                    part => part
                        .WithField("ArticleTaxonomy", field => field.OfType("TaxonomyField"))
                            .WithSetting("TaxonomyFieldSettings.TaxonomyId", articleTaxo.Id.ToString())
                            .WithSetting("TaxonomyFieldSettings.LeavesOnly", "False")
                            .WithSetting("TaxonomyFieldSettings.SingleChoice", "False")

Is there another approach I can use to build a TaxonomyField standalone and then associate it with the ContentType?

Basically, I want to do through code what Bertrand does manually in his video tutorial at 4:25-4:50 -

Aug 23, 2011 at 1:50 PM

This came up on Twitter yesterday. I think the upshot of the whole thing is that Fields can only be added to Parts. The Content Types UI is doing some trick to make it look like the Field is attached to the Type when you add them dynamically through the UI. But what it's really doing is adding it to a Part with the same name as the Type. At least that's my understanding - Bertrand might correct me on that. Theoretically then, it should be possible to add it through code in the same way that the UI does it so that it displays the same way on the page. I haven't tried it yet though so I'm not sure if that's true.

Aug 23, 2011 at 7:54 PM

That is correct.

Aug 26, 2011 at 3:44 AM

Thanks guys for explaining that.

I went and studied what data/records get written to the different tables when it is done manually and I recreated what takes place.

The following adds a part with same name as the type - as kevink suggested:

                    def => def
                        .WithSetting("ContentPartSettings.Attachable", "True")
                            field => field.OfType("TaxonomyField")
                                .WithSetting("TaxonomyFieldSettings.TaxonomyId", articleTaxo.Id.ToString())
                                .WithSetting("TaxonomyFieldSettings.LeavesOnly", "False")
                                .WithSetting("TaxonomyFieldSettings.SingleChoice", "False"))

So now if you were to compare the database tables (mainly the [settings_*] tables) between the manual way and code way of adding the TaxonomyField to a ContentType - they are identical.

I was a bit disappointed to find out that the Field was still not showing up on the ContentType.


Do I need to allow something (module?) or perform a refresh/restart for the UI to reuptake the table modifications?

That alter takes place outside of the datamigrations class so that the taxonomy ID can be inserted into the setting.

Aug 26, 2011 at 7:35 PM

If you are using the dev branch for Taxonomies, then you need another setting: "TaxonomyFieldSettings.Hint"

ContenDefinition properties are cached, but they are invalidated when you alter them, which is what you are doing.

Aug 26, 2011 at 8:41 PM

Thanks for the hint! What value should the setting take? true/false?

Aug 26, 2011 at 9:36 PM

A string ... it's a hint displayed to the user when selecting the field values

Aug 26, 2011 at 9:44 PM

So there is no way to re-validate the ContentDefinition properties?