📜 ⬆️ ⬇️

Orchard CMS extension: creating content types

This is a continuation of a series of articles on the development of your own sites based on the Orchard CMS content management system. The first articles in this series can be found at the following links:This article focuses on the expansion of the site based on Orchard CMS through the creation of content elements known as content types.

Creating content types


Although Orchard CMS already includes a set of several content types, such as Page (Page) and Blog Post (Blog Post), you can easily create your own content type (or even extend an existing type) using the administration panel. By default, the feature content types feature is enabled in Orchard. It must be enabled to successfully create your own content types. If required, you can activate the function on the Modules page in the administration panel.

image
')
In order to create your own content type, go to the Content section and select the Content Types link in the administration panel. On this page you will see a list of the content types available on your system. Please note that it is possible to edit and list for some content types (for example, List).

image

If you click on the “List Items” link, you will receive a list of available content elements for this type on your site.

image

You can edit the definition of the content type via the Edit link.

image

A content type in Orchard is created based on fields and parts. A description of these concepts can be found in this article . Fields (fields) - this is something specific to the content type, for example, the content type Product can have the fields SKU and Price. The content part, on the other hand, is a reusable component that can be added to many content types. For example, the content part of Route may offer any content type the opportunity to have an address / url.

You can assume that the content type contains fields and is built from content parts. This mechanism is also reflected in the Orchard code. For example, in order to use a blog entry as an element with an address (RoutePart) and access the value of the RoutePart.Slug property, you need to write the following code post.As <RoutePart> .Slug. Fortunately, you don’t need to write code at all to work with content types and parts. Consider how you can create a new content type.

Creating content type


Let's define our own content type. Suppose we need to create a content type that defines some kind of event or event (Event). To add this type, go to the Manage Content Types section and click Create new type.

image

Call the content type the name Event, after which the identifier will automatically take the value Event.

image

The content type will be created and you will be prompted to select the content parts available in the system to add them to the content type.

image

In the new window you can see the available content parts installed in Orchard. For our content type Event, we may want to choose the content part of Comments to comment on the event, the Tags part to be able to tag the event and Route to be able to get a separate URL to the event. In addition, we would like to be able to postpone the publication of an event on the site, for this we use the PublishLater part.

It is also necessary to add the Common part so that event elements can be displayed in the list of content types. Click Save to save the content type.

Then, click Add Field to add a new field to the content type.

image

Currently, Orchard will offer you only one field type (TextField), but the list of field types can be significantly expanded using the Orchard extension, for example, you can create field types CheckBoxField, EmailField, TextAreaField, DateTimeField (described in more detail in this article ). In addition, you can load some field types as separate modules from the gallery in the Gallery -> Modules section.

Enter Location as the name of the new field.

image

After that, the field will be displayed in the Event type content window.

image

Repeat the steps to create another “Date” field.

Content types, fields and parts can have settings. Specific settings available for fields or parts are determined by functions activated in Orchard. If you have activated the Indexing function (you can add it via the module gallery in the administration panel, along with the Lucene and Search modules), then the “Index this content type for search” setting will be added for each content type, and the “Include in the index” setting for each field will be added. ". Select these options for the Location field in our content type Event. This will allow visitors to search the site by the name of the location of the event (if the Search feature is enabled on the site).

image

Now that we have defined our own content type, let's create an instance of it. Notice the Create New Event link on the Manage Content Types page.

image

A similar link will appear in the administration panel in the New section under the name Event. Click on any of them to create a new event item.

image

As we can see, the editor for creating a new event element (Event) will contain all the necessary controls for the task of the values ​​of the fields we specified. In addition, there is a Title and Permalink here since we have included the Route part, there is a Location and Date, there is a field for entering tags and others. Fill in these fields and post an event.

image

Go to the Manage Content page of the administration panel and make sure that your entry has been created.

image

Please note that the site has added a new event to the main menu, as expected, and all of our content fields and parts are displayed correctly. It is possible to determine the appearance of the event and how it will be displayed on the page. For more information on how to do this, use this material .

image

Let's try the search capabilities on the site in order to find our new content type. Make sure that the Indexing, Search and Lucene functions are enabled on the website in the Features section of the administration panel. You may need to add a search widget through the Widgets section of the control panel, if it is not already installed for your pages. Now go to the Search Index page to see which fields are to be indexed. You should see that the event-location field is in the list of indexed fields.

image

We can tell the search engine the need to search for data among the Location field through the Settings section.

image

Now enter in the search box on your site the value of the event location and make sure that the site gives the correct search result.

image

Conclusion


This article describes the creation of content types for the Orchard CMS content management system. In the following parts of the guide, we will consider the creation of widgets and modules for expanding the functionality of sites based on Orchard.

Source: https://habr.com/ru/post/125077/


All Articles