Overriding display type in view

Topics: Customizing Orchard
Sep 27, 2011 at 10:58 AM

Hi All,

I am trying to think of a good way around a problem I'm having.

The scenario: I'm overriding a view (in my theme) whose Model object has a list of dynamic content items, which are generated in the driver using IContentManager.BuildDisplay(contentItem, "Summary"). I want to change this to use the Detail view of the contentItem instead. I was thinking along these lines:

@using Orchard.ContentManagement;

@{
    List<object> displayItems = new List<object>();
    
    IContentManager manager = null; //How do I get this?
    
    foreach(var shapeItem in Model.ContentItems)
    {
        displayItems.Add(manager.BuildDisplay(shapeItem.ContentItem));
    }
}

@foreach (var displayItem in displayItems)
{
    @Display(displayItem)
}

but as you can see from my comment I can't work out how to get an instance of IContentManager in the view. I might be going about this in completely the wrong way, if so, suggestions are more than welcome. Changing the driver is an option I suppose, but as it's a module from the gallery, I'd have to change it each time that module was updated. It seems neater to keep the changes in my theme?

Cheers!

Sep 27, 2011 at 11:25 AM

Have figured out how to do this now, but still unsure if this approach is correct so comments are welcome.

@{
    List<object> displayItems = new List<object>();
   
    foreach(var shapeItem in Model.ContentItems)
    {
        displayItems.Add(shapeItem.ContentItem.ContentManager.BuildDisplay(shapeItem.ContentItem, "Detail"));
    }
}
@using Orchard.ContentManagement;

@foreach (var displayItem in displayItems)
{
    @Display(displayItem)
}

Coordinator
Sep 27, 2011 at 8:30 PM

Looks fine to me, except that I would frown a little upon calling into the content manager from the view. Ideally, you would create those shapes from a driver.