Incorrect implementation of TitlePartHandler?

Topics: Core, Customizing Orchard, Troubleshooting, Writing modules
Developer
Apr 13, 2012 at 2:53 PM

As David Hayden pointed out to me, I should be able to implement ITitleAspect on any of my parts in order to control how it's displayed in the dashboard (see my post as well as his post on the matter).

However, I can't seem to get it to work unless I either:

a) create a contenthandler that sets the metadata.DisplayText, or
b) change the implementation of the TitlePartHandler (by casting the content item to an ITitleAspect instead of an TitlePart)

Is this a bug in the TitlePartHandler, or am I misunderstanding how ITitlePart works?

Developer
Apr 14, 2012 at 12:07 PM
Edited Apr 14, 2012 at 12:35 PM

Hmm, the TitlePartHandler in my installation (v1.4) is this:

 

    public class TitlePartHandler : ContentHandler {

        public TitlePartHandler() {
            OnIndexing<ITitleAspect>((context, part) => context.DocumentIndex.Add("title", part.Title).RemoveTags().Analyze());
        }

        protected override void GetItemMetadata(GetContentItemMetadataContext context) {
            var part = context.ContentItem.As<ITitleAspect>();

            if (part != null) {
                context.Metadata.DisplayText = part.Title;
            }
        }
    }

This properly uses the interface instead of the part.

Developer
Apr 14, 2012 at 12:31 PM

Hi Piedone, thanks for checking! I guess I have a slightly older version of that source file. I will manually update it.
Intersting note: looking at the latest code file: http://orchard.codeplex.com/SourceControl/changeset/view/97b1b83a943a#src%2fOrchard.Web%2fCore%2fTitle%2fHandlers%2fTitlePartHandler.cs , the GetItemMetadata method has been removed.

Developer
Apr 14, 2012 at 12:39 PM

That's interesting I wonder why? The DisplayText should be set somewhere otherwise serious problems will happen...

Apr 16, 2012 at 12:25 PM

for neweast

http://orchard.codeplex.com/SourceControl/changeset/view/a1f00fd194b8#src%2fOrchard.Web%2fCore%2fTitle%2fHandlers%2fTitlePartHandler.cs

it is

using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Core.Title.Models;
using Orchard.Data;

namespace Orchard.Core.Title.Handlers {
    public class TitlePartHandler : ContentHandler {

        public TitlePartHandler(IRepository<TitlePartRecord> repository) {
            Filters.Add(StorageFilter.For(repository));
            OnIndexing<TitlePart>((context, part) => context.DocumentIndex.Add("title", part.Title).RemoveTags().Analyze());
        }

        protected override void GetItemMetadata(GetContentItemMetadataContext context) {
            var part = context.ContentItem.As<TitlePart>();

            if (part != null) {
                context.Metadata.DisplayText = part.Title;
            }
        }
    }
}