OrchardCMS - 未以编程方式将内容字段添加到自定义内容类型
OrchardCMS - Not added content field to custom content type programmatically
通过迁移创建新的内容类型,添加到他的内容字段,但它们没有显示在仪表板中。
仪表板中的内容类型视图:
我的代码有什么问题?
public int UpdateFrom2() {
var name = "ProductViaCode";
ContentDefinitionManager.AlterPartDefinition(
string.Format("{0}Part", name),
b => b
.Attachable()
.WithField("ProductId", cfg => cfg
.OfType("InputField")
.WithDisplayName("Product Id")));
ContentDefinitionManager.AlterTypeDefinition(
name, cfg => cfg
.WithPart(typeof(CommonPart).Name)
.WithPart(typeof(AutoroutePart).Name)
.WithPart(typeof(BodyPart).Name)
.WithPart(typeof(TitlePart).Name)
.WithPart(typeof(MenuPart).Name)
.Creatable()
.Draftable()
.Listable()
.Securable());
return 3;
}
正如@Xceno 所说,您没有将该部分添加到您的内容类型中。但是,通过这样做,它不会显示在 'Fields' 部分下,而是显示在 'Parts' 部分下,在 'ProductViaCode' 下。这是因为你用 'Part'.
作为后缀
要使其出现在类型的'Fields'部分下,可以将该字段添加到类型的同名部分,然后添加那部分适合你的类型:
var name = "ProductViaCode";
ContentDefinitionManager.AlterPartDefinition(
// Without 'Part' postfix
name,
b => b
.Attachable()
.WithField("ProductId", cfg => cfg
.OfType("InputField")
.WithDisplayName("Product Id")));
// Don't forget to add the part to the type
ContentDefinitionManager.AlterTypeDefinition(name, cfg => cfg
.WithPart(name));
通过迁移创建新的内容类型,添加到他的内容字段,但它们没有显示在仪表板中。
仪表板中的内容类型视图:
我的代码有什么问题?
public int UpdateFrom2() {
var name = "ProductViaCode";
ContentDefinitionManager.AlterPartDefinition(
string.Format("{0}Part", name),
b => b
.Attachable()
.WithField("ProductId", cfg => cfg
.OfType("InputField")
.WithDisplayName("Product Id")));
ContentDefinitionManager.AlterTypeDefinition(
name, cfg => cfg
.WithPart(typeof(CommonPart).Name)
.WithPart(typeof(AutoroutePart).Name)
.WithPart(typeof(BodyPart).Name)
.WithPart(typeof(TitlePart).Name)
.WithPart(typeof(MenuPart).Name)
.Creatable()
.Draftable()
.Listable()
.Securable());
return 3;
}
正如@Xceno 所说,您没有将该部分添加到您的内容类型中。但是,通过这样做,它不会显示在 'Fields' 部分下,而是显示在 'Parts' 部分下,在 'ProductViaCode' 下。这是因为你用 'Part'.
作为后缀要使其出现在类型的'Fields'部分下,可以将该字段添加到类型的同名部分,然后添加那部分适合你的类型:
var name = "ProductViaCode";
ContentDefinitionManager.AlterPartDefinition(
// Without 'Part' postfix
name,
b => b
.Attachable()
.WithField("ProductId", cfg => cfg
.OfType("InputField")
.WithDisplayName("Product Id")));
// Don't forget to add the part to the type
ContentDefinitionManager.AlterTypeDefinition(name, cfg => cfg
.WithPart(name));