Patrick’s Bytes

Techie for the past 30 years…

Hello Orchard Part 5–Attaching a part to content type

One of the coolest feature in Orchard is the ability to compose different content part together to make a complete content type. This promotes composite applications e.g. the comment content part with its logic and UI can be reused in different part of the application.

Part 5 of this tutorial will shows you how to attach a Common part to the Product content type where this Common part contains properties like Published Data and Owner.

1. Add a reference to Orchard.Core

image

2. Go to the ProductRecord.cs file and update it to include these namespaces:

using System.ComponentModel.DataAnnotations;using Orchard.Core.Common.Models;

Now, let’s illustrate the power of the data composition story… We are going to add a “Common” part to the “Product” content type. The “Common” part is an Orchard extension which automatically keeps track of Owner, Container, CreationDate, etc. for content items.

Here is the definition of the ICommonAspect interface (no need to type this – it’s already defined by Orchard in the Orchard.Core.Common.Models namespace). Once we add this part to our Product type, the CommonAspect data will be saved (as a “Content Part”) with each instance of “Product”.

public interface ICommonAspect : IContent {

IUser Owner { get; set; }

IContent Container { get; set; }

DateTime? CreatedUtc { get; set; }

DateTime? PublishedUtc { get; set; }

DateTime? ModifiedUtc { get; set; }

DateTime? VersionCreatedUtc { get; set; }

DateTime? VersionPublishedUtc { get; set; }

DateTime? VersionModifiedUtc { get; set; }

}

3. Change the ProductHandler class to add a “CommonPart” (was called CommonAspect) part to the “Product” content type:

using Orchard.ContentManagement.Handlers;using Orchard.ContentManagement;using Orchard.Commerce.Models;using Orchard.Data;using Orchard.Core.Common.Models;

namespace Orchard.Commerce.Handlers{    public class ProductHandler : ContentHandler    {        public readonly static ContentType ContentType = new ContentType        {            Name = "product",            DisplayName = "Product"        };

        public ProductHandler(IRepository<ProductRecord> repository)        {            Filters.Add(new ActivatingFilter<Product>(ProductHandler.ContentType.Name));            Filters.Add(new ActivatingFilter<Orchard.Core.Common.Models.CommonPart>(ProductHandler.ContentType.Name));            Filters.Add(StorageFilter.For(repository));        }

    }}

That’s it! Now, whenever a “Product” is going to be manipulated by the application, a “CommonAspect” will go with it. Under the hood, the content manager will activate the “CommonAspect” part when “Products” are activated. The “CommonAspect” implementation will keep track of creation date, modification date and publication dates automatically for us.

3. Let’s illustrate that by displaying the “Created”, “Published” date for each product in the Index.ascx view. Add these 2 lines at the beginning of the file:

<%@ Import Namespace="Orchard.Core.Common.Models"%><%@ Import Namespace="Orchard.ContentManagement"%>

a. Add “Created” and “Published” columns:

<th>   Created</th><th>   Published</th>

b. Access to the “Created” and “Published” dates as follows:

<td>   <%= Html.Encode(String.Format("{0}", item.As<CommonPart>().CreatedUtc))%></td><td>   <%= Html.Encode(String.Format("{0}", item.As<CommonPart>().PublishedUtc)) %></td>

4. Type Ctrl-F5 to build and run in Visual Studio.

Note: If you don’t empty the database at this point, the product created until now will have no “Common” data value. Orchard doesn’t have a story for database upgrade for now. To reset the database (destroy all data for the site), delete the App_Data/Sites folder in your project. You may need to “Show All Files” in Visual Studio’s Solution Explorer before you can see this folder. After deleting the database, you’ll need to walk through the Orchard setup screen again.

Previously the tutorial would ask you to empty the database and recreate the website again. But with the latest data migration facility you need not do so. Just go to Site Configuration –> Dev Tools and trigger the Update Database command.

Notice how the table now shows created and published dates (they have been setup automatically by the content manager, stored in a “Common” aspect table).

clip_image002

Let’s look at what happens when a product instance is created. Set a debug breakpoint in “AdminController.Create” action, on the line of code that invokes _contentManager.Create(product). Type F5 in Visual Studio to begin debugging, and then create a new product using the admin panel in order to hit the breakpoint:

clip_image004

The “Product” content item has 2 parts: the Product part and the “CommonPart” part. If we look at the data maintained by the “CommonPart”, we see that everything is taken care of internally. Here is the “Product” instance after the call to “Create” of content manager:

clip_image006

No Responses to “Hello Orchard Part 5–Attaching a part to content type”

RSS feed for comments on this post. TrackBack URL

Leave a Response