Content Type Events

Topics: Customizing Orchard, Writing modules
Aug 9, 2012 at 2:49 PM

I’m attempting to leverage Orchard to be a search provider (kind of) for an external data source.  Essentially I'm just attempting to create a content type that would basically just save the search parameters and then load data from an external data source when the content is viewed on the front end.

I've started out following Piotr's suggestion here which works very well, requirements differ in that I’d like to be able to create a content type that can be comprised of one or more “search criteria parts” and because of this the search results need to be tied to the content type, not the content part(s) since there will be more than one.

That said, I’m not quite sure if/when/where I can handle an event like "onload" for a content type.  Is it possible to create handlers/drivers for content types like you can for content parts?  I can’t seem to find any documentation about content type handlers, they all seem to be related to content parts.

Any help would be greatly appreciated!


Aug 9, 2012 at 7:12 PM

Yes, you can create a content handler that handles content events. The part related features are actually just helpers.

In your case, all you would need to do is implement a new class deriving from ContentHandler and override the Loading or Loaded method. You will receive a context parameter that has all the information you need: the content item, its type, and more.

Aug 9, 2012 at 10:40 PM

Thanks for the reply!

I think I understand what you are saying, but what is confusing me is that the content (Saved Search) doesn't have a concrete class in my current implementation, it is only created in the migrations.cs file like so:

                cfg => cfg
                    .Named("Saved Search")
If I were to add a handler, what would TPart be for the OnLoaded<TPart> method that is overridden?

Aug 9, 2012 at 11:27 PM

I think I got it working!  This is what I did:

OnLoaded<IContent>((ctx, part) =>
    if (part.ContentItem.ContentType.Equals("Saved Search"))
        ...stuff to do if it is a saved search...

Is an acceptable way to achieve this functionality?

Thanks again!

Aug 10, 2012 at 10:54 AM

Looks good to me, although I wouldn't have come up with that :) Instead, I would have overridden the OnLoaded virtual method (outside of the constructor).  But I suppose it doesn't make a big difference.