Orchard CMS:如何在迁移时将分类法字段添加到内容类型?

seb*_*suy 2 taxonomy orchardcms

我需要在模块的迁移中定义一个具有Taxonomy字段的新Content Type.我想我需要做这样的事情:

ContentDefinitionManager.AlterTypeDefinition("ContentTypeName",
            cfg => cfg
                .WithPart("TermsPart", builder => builder
                    .WithSetting(...
Run Code Online (Sandbox Code Playgroud)

但我无法使其发挥作用.

seb*_*suy 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
                ));
Run Code Online (Sandbox Code Playgroud)

我创建了一个名为"SampleColumn"列的表,并为名为"Topics"的Taxonomy附加了一个字段"Topic".希望它可以帮助别人.

  • 看起来这种方法需要对当前的Orchard版本(我使用1.9)进行轻微修改才能正确应用定义的设置.例如,使用`.WithSetting("Taxonomy","Topics")`在使用`.WithSetting("TaxonomyFieldSettings.Taxonomy","Topics")`确实有效时不起作用. (2认同)