Skip to content

创建自定义内容类型-Creating Custom Content Types

Although Orchard includes the Page and Blog Post content types by default, it is very easy to create a custom content type (or even extend the definition of an existing content type) using the admin panel. By default, the Content Types feature is enabled. This feature must be enabled to create a custom content types. If needed, you can manually enable the feature in the Manage Features page.

尽管Orchard默认包含_Page_和_Blog Post_内容类型,但使用管理面板创建自定义内容类型(甚至扩展现有内容类型的定义)非常容易。默认情况下,启用内容类型功能。必须启用此功能才能创建自定义内容类型。如果需要,您可以在管理功能页面中手动启用该功能。

To create a content type, click Content Definition and select the Content Types link in the admin panel.

要创建内容类型,请单击内容定义,然后在管理面板中选择内容类型链接。

On this screen, you can see the available content types in the system. Notice that it is possible to create and list content items of some of these types (such as Page), whereas others only allow you to edit the definition of the type here (such as Comments and Widgets, since these have a dedicated/custom admin experience for creating and listing these items instead).

在此屏幕上,您可以看到系统中的可用内容类型。请注意,可以创建和列出某些类型的内容项(例如_Page_),而其他类型只允许您在此处编辑类型的定义(例如_Comments_和_Widgets_,因为它们具有专用/自定义管理员改为创建和列出这些项目的经验)。

If you click List Items, to list the items of the Page type, you can see the available content items of this type in the site, similar to the Manage Content screen in the admin menu).

如果单击列表项,要列出_Page_类型的项目,您可以在站点中看到此类型的可用内容项,类似于管理菜单中的管理内容屏幕)。

You can also edit the definition of the Page type by clicking Edit for this type.

您还可以通过单击此类型的编辑来编辑_Page_类型的定义。

A content type in Orchard is made up of fields and parts. Basic Orchard Concepts provides a good overview of these concepts. A field is something specific to the type; for example, a Product type might have SKU and Price fields. A part, however, is a reusable component that can be attached to one or more types. For example, the Autoroute part gives a type the ability to be addressed on the front-end via a route/URL. In some ways, you can think of a type as having fields, and being made up of one or more parts. This is actually reflected in the underlying code in Orchard as well. To treat a blog post as a AutoroutePart and access its AutoroutePart.Slug property, you would write something like this: post.As<AutoroutePart>.Slug. Fortunately you don't have to write code to have fun with types and parts. We are going to look at this in more detail by way of example in the next section.

Orchard中的内容类型由_fields_和_parts_组成。 [基本果园概念](Basic-Orchard-Concepts)提供了对这些概念的良好概述。 _field_是该类型特有的东西;例如,_Product_类型可能具有_SKU_和_Price_字段。但是,_part_是可重用的组件,可以附加到一个或多个类型。例如,_Autoroute_部​​分为类型提供了通过路径/ URL在前端寻址的能力。在某些方面,您可以将类型视为_having_字段,而_being_则由一个或多个部分组成。这实际上也反映在Orchard的底层代码中。要将博客文章视为“AutoroutePart”并访问其“AutoroutePart.Slug”属性,您可以编写如下内容:post.As <AutoroutePart> .Slug。幸运的是,您不必编写代码来获得类型和部件的乐趣。我们将在下一节中以示例的方式更详细地介绍这一点。

Defining a New Content Type

定义新的内容类型

Let's define a custom content type. Suppose you wanted to define an Event type, for listing events with location and date fields. To do this in the Manage Content Types screen, click on Create new type.

我们定义一个自定义内容类型。假设您要定义_Event_类型,以列出具有位置和日期字段的事件。要在管理内容类型屏幕中执行此操作,请单击创建新类型

Type the name "Event" for the content type. The Content Type Id field is automatically populated with "Event" which you can keep.

为内容类型键入名称“Event”。 内容类型ID 字段自动填充“事件”,您可以保留。

Click Add to add a field.

单击添加以添加字段。

Currently Orchard only includes a single field type (TextField), but more can be created as extensions to Orchard (for example, CheckBoxField, EmailField, TextAreaField, DateTimeField, etc), and a number of additional fields are available under Gallery > Modules as optional downloads.

目前Orchard只包含一个字段类型(TextField),但[可以创建更多](Creating-a-custom-field-type)作为Orchard的扩展(例如,CheckBoxFieldEmailFieldTextAreaFieldDateTimeField等),以及 Gallery > Modules 下的许多其他字段可用作可选下载。

Type "Location" for the name of the field, and click Save.

键入“Location”作为字段名称,然后单击 Save

The field is now listed in the Event type screen.

该字段现在列在_Event_类型屏幕中。

Go ahead and repeat the previous two steps to add a second field named "Date".

继续并重复前两个步骤以添加名为“Date”的第二个字段。

To add a part to your type, click Add in the Parts section of the Event type.

要向您的类型添加零件,请单击_Event_类型的 Parts 部分中的 Add

Here you can see the available parts in Orchard (as of the current release). For our Event type, we want to be able to comment on the event (Comments part), tag the event (Tags part), access the event from the front-end via a URL/route (Autoroute part), add the event to the main menu (Menu part), and be able to publish the event immediately, on a schedule, or as a draft (PublishLater part).

在这里,您可以看到Orchard中的可用部件(截至当前版本)。对于我们的_Event_类型,我们希望能够对事件(“评论”部分)进行评论,标记事件(“标签”部分),通过URL /路径(“Autoroute”部分)从前端访问事件),将事件添加到主菜单(“菜单”部分),并能够立即,按计划或作为草稿(“PublishLater”部分)发布事件。

Also add the Common part so that your items can appear in lists of content items.

还要添加“Common”部分,以便您的项目可以显示在内容项列表中。

Types, fields and parts can have settings as well. The specific settings that are available on the fields or parts is determined by the features that are activated in Orchard. If we have enabled the Indexing feature, there is a setting to "Index this content type for search" and on each field, a setting to "Include in the index". Select these options for the "Location" field of the custom Event type. This will enable visitors of your site to search by location on the front-end (when the Search feature is enabled).

类型,字段和部分也可以有_settings_。字段或部件上可用的特定设置由Orchard中激活的功能确定。如果我们启用了_Indexing_功能,则会设置为“为搜索索引此内容类型”,并在每个字段上设置“包含在索引中”。为custom _Event_类型的“Location”字段选择这些选项。这将使您网站的访问者能够按前端位置进行搜索(启用_Search_功能时)。

Hit Save.

点击保存

Now that we have defined our custom content type, let's create a new item of this type. Notice the Create New Event link in Manage Content Types.

现在我们已经定义了自定义内容类型,让我们创建一个这种类型的新项目。请注意管理内容类型中的创建新事件链接。

Similarly, there is a new admin menu link entitled Event under New. Click either one of these links to create a new Event content item.

同样,在 New 下有一个名为 Event 的新管理菜单链接。单击其中一个链接以创建新的_Event_内容项。

We can see that the editor for our Event type has all the fields and parts we defined. It has a Title because of the TitlePart and Permalink because of the AutoroutePart, a Location because of the fields we added, a Tags input from the Tags part, a Show on main menu checkbox from the Menu part, and the ability to enable comments from the Comments part. The fact that we can Publish Now, Publish Later or Save As Draft is given by the Publish Later part. Once you have filled in these fields, go ahead and publish the event.

我们可以看到_Event_类型的编辑器包含我们定义的所有字段和部分。它有一个 Title ,因为'TitlePart和** Permalink **因为'AutoroutePart位置因为我们添加的字段,标签输入来自标签部分,a 在主菜单上显示从菜单部分复选框,以及从“评论”部分启用评论的功能。我们可以**发布**,** Publish Later **或** Save as Draft **这一事实由Publish Later`部分给出。填写完这些字段后,继续发布该事件。

Looking at the Manage Content screen in the Orchard admin panel, we can see our event item listed among the pages in the site!

查看Orchard管理面板中的 Manage Content 屏幕,我们可以在网站的页面中看到我们的活动项目!

On the site's front-end notice the event has been added to the main menu (as expected), and that our fields and parts are being displayed correctly here as well.

在网站的前端通知中,事件已添加到主菜单中(如预期的那样),并且我们的字段和部件也在此处正确显示。

It is possible to customize the way the event appears and template its rendering. See Template File Syntax Guide for more information on how to do that.

可以自定义事件的显示方式并模拟其呈现。有关如何执行此操作的详细信息,请参阅[模板文件语法指南](模板文件语法指南)。

Let's try out the Search capability against our new content type. Make sure you have first enabled the Indexing, Search, and Lucene features in the Features admin screen. Now visit the Search Index page to view the available fields that are indexed. You should see the event-location field in the index as expected (if not, just rebuild the index and you'll see it).

让我们针对新的内容类型尝试搜索功能。确保您首先在功能管理界面中启用了索引搜索 Lucene 功能。现在访问搜索索引页面以查看索引的可用字段。您应该按预期在索引中看到 event-location 字段(如果不是,只需重建索引,您就会看到它)。

We can tell the Search feature to query this field by going to the Settings admin screen and adding this field to the Search settings.

我们可以通过转到设置管理界面并将此字段添加到搜索设置来告诉_Search_功能查询此字段。

On the front-end type a keyword that matches the location of your event.

在前端键入与您的活动位置匹配的关键字。

Search indexing has indexed our "Location" field and our event appears in search results as expected!

搜索索引已将我们的“位置”字段编入索引,我们的活动会按预期显示在搜索结果中!

This concludes the tutorial on custom content types. If you are interested in delving further, check out the related tutorials on this site for how to build a custom type, field or part using code.

关于自定义内容类型的教程到此结束。如果您有兴趣进一步深入研究,请查看本网站上的相关教程,了解如何使用代码构建自定义类型,字段或部件。

Change History

改变历史

  • Updates for Orchard 1.8

  • Orchard 1.8的更新 *

    • 9-6-14: Updated all screen shots for creating custom content type.