2

我需要在我的模块迁移中定义一个具有分类字段的新内容类型。我想我需要做这样的事情:

ContentDefinitionManager.AlterTypeDefinition("ContentTypeName",
            cfg => cfg
                .WithPart("TermsPart", builder => builder
                    .WithSetting(...

但我无法让它工作。

4

1 回答 1

8

感谢Giscard 的回答,我终于做到了。关于 Orchard 要了解的重要一点是字段不能附加到内容类型。当您将其附加到管理 UI 中的内容类型时,Orchard 在幕后做了一些魔术来隐藏这一事实,它在该内容类型内创建一个与内容类型同名的内容部分,然后附加该字段( s) 到那个新的内容部分。

所以这里是解决方案:

        //Create new table for the new part
        SchemaBuilder.CreateTable(typeof(SampleRecord).Name, table => table
            .ContentPartRecord()
            .Column("SampleColumn", DbType.String)
        );

        //Attach field to the new part
        ContentDefinitionManager.AlterPartDefinition(
            typeof(SamplePart).Name, 
            cfg => cfg
                .Attachable()
                .WithField("Topic", fcfg => fcfg
                    .OfType("TaxonomyField")
                    .WithDisplayName("Topic")
                    .WithSetting("Taxonomy", "Topics")
                    .WithSetting("LeavesOnly", "true")
                    .WithSetting("SingleChoice", "true")
                    .WithSetting("Required", "true"))
            );

        //Attach part to the new Content Type
        ContentDefinitionManager.AlterTypeDefinition("Sample",
                 cfg => cfg
                     .WithPart(typeof(SamplePart).Name
                ));

我创建了一个包含名为“SampleColumn”的列的表,并为名为“Topics”的分类附加了一个“Topic”字段。希望它可以帮助别人。

于 2013-05-16T15:56:48.593 回答