Content Types (OrchardCore.ContentTypes)
内容类型(OrchardCore.ContentTypes)
View Components
查看组件
SelectContentTypes
SelectContentTypes
Renders an editor to select a list of content types.
呈现编辑器以选择内容类型列表。
It can optionally filter content types of a specific stereotype.
它可以选择过滤特定构造型的内容类型。
The editor returns the selection as a string[]
on the model.
编辑器将选择作为模型上的string []
返回。
Parameters
参数
| Parameter | Type | Description |
|参数|输入|说明|
| --------- | ---- | ----------- |
| --------- | ---- | -----------
| selectedContentTypes
| string[]
| The list of content types that should be marked as selected when rendering the editor. |
| selectedContentTypes
| string []
|在呈现编辑器时应标记为已选择的内容类型列表。 |
| htmlName
| string
| The name of the model property to bind the result to.
| htmlName
| string
|要将结果绑定到的model属性的名称。
| stereotype
(optional) | string
| A stereotype name to filter the list of content types to be able to select. |
| stereotype
(可选)| string
|用于过滤可以选择的内容类型列表的构造型名称。 |
Sample
样品
@await Component.InvokeAsync("SelectContentTypes", new { selectedContentTypes = Model.ContainedContentTypes, htmlName = Html.NameFor(m => m.ContainedContentTypes) })
<font color=#0099ff size=4 face="黑体">@await Component.InvokeAsync(“SelectContentTypes”,new {selectedContentTypes = Model.ContainedContentTypes,htmlName = Html.NameFor(m => m.ContainedContentTypes)})</font>
<font color=#0099ff size=4 face="黑体"></font>
Migrations
迁移
Migration classes can be used to alter the content type definitions, like by adding new types, or configuring their parts and fields.
迁移类可用于更改内容类型定义,例如添加新的__types__,或配置其__parts__和__fields__。
IContentDefinitionManager
IContentDefinitionManager
This service provides a way to modify the content type definitions. From a migrations class, we can inject an instance of this interface.
此服务提供了一种修改内容类型定义的方法。从迁移类中,我们可以注入此接口的实例。
public class Migrations : DataMigration
<font color=#0099ff size=4 face="黑体">公共类迁移:DataMigration</font>
{
<font color=#0099ff size=4 face="黑体">{</font>
IContentDefinitionManager _contentDefinitionManager;
<font color=#0099ff size=4 face="黑体"></font>
public Migrations(IContentDefinitionManager contentDefinitionManager)
{
_contentDefinitionManager = contentDefinitionManager;
}
<font color=#0099ff size=4 face="黑体"></font>
public int Create()
{
// This code will be run when the feature is enabled
<font color=#0099ff size=4 face="黑体"></font>
return 1;
}
}
<font color=#0099ff size=4 face="黑体">}</font>
Creating a new Content Type
创建新的内容类型
The following example creates a new Content Type named Product
.
以下示例创建名为“Product”的新内容类型。
_contentDefinitionManager.AlterTypeDefinition("Product");
<font color=#0099ff size=4 face="黑体">_contentDefinitionManager.AlterTypeDefinition( “产品”);</font>
Changing the metadata of a Content Type
更改内容类型的元数据
To change specific properties of the content type, an argument can be used to configure it:
要更改内容类型的特定属性,可以使用参数来配置它:
_contentDefinitionManager.AlterTypeDefinition("Product", type => type
<font color=#0099ff size=4 face="黑体">_contentDefinitionManager.AlterTypeDefinition(“Product”,type => type</font>
// content items of this type can have drafts
.Draftable()
// content items versions of this type have saved
.Versionable()
// this content type appears in the New menu section
.Creatable()
// permissions can be applied specifically to instances of this type
.Securable()
);
<font color=#0099ff size=4 face="黑体">);</font>
Adding Content Parts to a type
将内容部件添加到类型中
The following example adds the TitlePart
content part to the Product
type.
以下示例将TitlePart
内容部分添加到Product
类型。
_contentDefinitionManager.AlterTypeDefinition("Product", type => type
<font color=#0099ff size=4 face="黑体">_contentDefinitionManager.AlterTypeDefinition(“Product”,type => type</font>
.WithPart("TitlePart")
);
<font color=#0099ff size=4 face="黑体">);</font>
Each part can also be configured in the context of a type. For instance the AutoroutePart
requires a Liquid template as its pattern to generate custom routes. It's defined in a custom setting for this part.
每个部分也可以在类型的上下文中配置。例如,AutoroutePart
需要__Liquid__模板作为其模式来生成自定义路由。它是在此部件的自定义设置中定义的。
_contentDefinitionManager.AlterTypeDefinition("Product", type => type
<font color=#0099ff size=4 face="黑体">_contentDefinitionManager.AlterTypeDefinition(“Product”,type => type</font>
.WithPart("Product", part => part
// sets the position among other parts
.WithPosition("2")
// sets all the settings on the AutoroutePart
.WithSettings(new AutoroutePartSettings { Pattern = "{{ ContentItem | display_text | slugify }}" })
)
);
<font color=#0099ff size=4 face="黑体">);</font>
For a list of all the settings each type can use, please refer to their respective documentation pages.
有关每种类型可以使用的所有设置的列表,请参阅各自的文档页面。
Adding Content Fields to a part
将内容字段添加到零件
Fields can not be attached directly to a Content Type. To add fields to a content type, create a part using with the same name as the type and add fields to this part.
字段无法直接附加到内容类型。要将字段添加到内容类型,请使用与该类型相同的名称创建零件,并向该零件添加字段。
_contentDefinitionManager.AlterTypeDefinition("Product", type => type
<font color=#0099ff size=4 face="黑体">_contentDefinitionManager.AlterTypeDefinition(“Product”,type => type</font>
.WithPart("Product", part => part
.WithField("Image", field => field
.OfType("MediaField")
.WithDisplayName("Main image"))
.WithField("Price", field => field
.OfType("NumericField")
.WithDisplayName("Price"))
)
);
<font color=#0099ff size=4 face="黑体">);</font>
When added to a part, fields can also have custom settings which for instance will define how the editor will behave, or validation rules. Also refer to their respective documentation pages for a list of possible settings.
当添加到零件时,字段还可以具有自定义设置,例如将定义编辑器的行为方式或验证规则。另请参阅各自的文档页面以获取可能的设置列表。