Content Types with Title Part and ITitleAspect in Orchard 1.3

Content Types with Title Part and ITitleAspect in Orchard 1.3

I've been having fun discovering and using the new features in Orchard 1.3. Three features presented so far include: Markdown Support for Pages, Blog Posts, and other Content Items in Orchard, Delete Content Types in Orchard 1.3  as well as the ability to Preview Blog Posts, Pages, and other Content Types in Orchard. Another new feature in Orchard 1.3 that will be appreciated by Orchard Developers is the new Title Part and ITitleAspect Interface to help add titles to Content Type Items when displayed in the Orchard Dashboard, etc. This new feature is subtle, but if you've been developing numerous Custom Orchard Modules with Custom Types that did not use the Route Part in Orchard, you have no doubt stumble upon the fact that your Content Type Items were displayed by the name of the Content Type in the Admin Summary ( as an example ) as opposed to something more useful. Let's dive into these subtle, but very useful, Title Part and ITitleAspect in Orchard 1.3. Don't forget to also view my other Orchard Tutorials.

The Problem - Displaying Title for Non-Routable Content Types

Here's the rub. For demonstration purposes, I have created a new Orchard Module that creates a new Book Content Type. Book consists of 3 Parts:

  1. Book Part
  2. Common Part
  3. PublishLater Part

Notice that there is no Route Part, and this is important!

ContentDefinitionManager.AlterTypeDefinition("Book",
    cfg => cfg
        .WithPart("BookPart")
        .WithPart("CommonPart")
        .WithPart("PublishLaterPart")
        .Draftable()
    );

The Book Part is just a couple of simple fields for demonstration purposes:

  • Name
  • Author
public class BookPart : ContentPart<BookPartRecord>{
    public string Name {
        get { return Record.Name; }
        set { Record.Name = value; }
    }

    public string Author {
        get { return Record.Author; }
        set { Record.Author = value; }
    }
}

In addition, I have created all the necessary Drivers, Handlers, Views, Permissions, Navigation Providers, and everything else to get a fully functioning interface to successfully add a library of books to my Orchard Website.

Here is an example of a new Orchard Book I am interested in helping author as part of the Orchard Community so it gets added first to my book library.

Orchard CMS Development - Create New Content Type

After I publish the Orchard Book and navigate back to the list of Books via the Books Menu Item, I am greeted not with the title of the book, Orchard Book, but the name of the Content Type, Book! I know. I know. This isn't good :) All books will be labeled as "Book" and there will be no way to tell one book from another!

Orchard Web Development Displaying Content Types in Orchard Dashboard

There were ways to solve this problem before Orchard 1.3, but now with ITitleAspect and the new Title Part in Orchard 1.3 we have more elegant ways to get this non-routable Content Type to display a more useful title as opposed to the name of the Content Type.

ITitleAspect Interface in Orchard 1.3

One way to solve this problem is to just implement the ITitleAspect Interface in Orchard 1.3 on one of the Content Parts that make up your Content Type. In this case, the easiest way to do this is implement ITitleAspect on BookPart. Let's change the code to do just that. First, ITitleAspect is as follows:

public interface ITitleAspect : IContent {
    string Title { get; }
}

Implementing ITitleAspect on BookPart can be as simple as follows:

public class BookPart : ContentPart<BookPartRecord>, ITitleAspect {
    public string Name {
        get { return Record.Name; }
        set { Record.Name = value; }
    }

    public string Author {
        get { return Record.Author; }
        set { Record.Author = value; }
    }

    public string Title {
        get { return Record.Name; }
    }
}

Now when we add the Orchard Book to our library of books using the new code, the book is displayed properly in the Admin Summary with the title of the Orchard Book.

Develop Custom Content Types and Custom Orchard Modules

Realistically you would just drop the Name Property and modify your ContentPart ( and corresponding ContentPartRecord ) to include a Title Property as follows:

public class BookPart : ContentPart<BookPartRecord>, ITitleAspect {
    public string Title {
        get { return Record.Title; }
        set { Record.Title = value; }
    }

    public string Author {
        get { return Record.Author; }
        set { Record.Author = value; }
    }
}

Title Part in Orchard 1.3

The other option is to add the new Title Part to the Book Content Type. In this case we could remove the Name ( or Title ) Property from the Book Part and instead add the Title Part directly to the Book Content Type making things even easier:

ContentDefinitionManager.AlterTypeDefinition("Book",
    cfg => cfg
      .WithPart("TitlePart")
      .WithPart("BookPart")
      .WithPart("CommonPart")
      .WithPart("PublishLaterPart")
      .Draftable()
    );
public class BookPart : ContentPart<BookPartRecord>{
    public string Author {
        get { return Record.Author; }
        set { Record.Author = value; }
    }
}

The Title Part implements ITitleAspect to provide you the convenience of implementing it yourself in another Content Part.

Conclusion

Wow! That is a lot of code, images, and dialog on such a simple concept as ITitleAspect and Title Part in Orchard 1.3. However, unless you have been in the Orchard Trenches developing custom modules, types, and parts since Orchard 1.0 you would never appreciate just how important this feature is when extending Orchard.

Please check out my other Orchard Tutorials, and as always, please consider hiring me for your next Orchard Website and development needs.

Hire Me