Determining Content Type

Topics: Customizing Orchard, Writing modules, Writing themes
Mar 5, 2011 at 8:19 PM

Based on this post

http://weblogs.asp.net/bleroy/archive/2010/12/14/switching-the-layout-in-orchard-cms.aspx

I was trying to create a feature in my skin to use an alternate layout for a custom Content Type.  I created a Content Type through the Admin interface called AlertPage.  I wanted my skin to be able to utilize Layout-AlertPage.cshtml.  

My strategy was to use the same technique used the article (LayoutFilter.cs) but I can't figure out how to determine the Content Type of the Page (Shape) being rendered.

If I'm headed in the right direction then I likely just need to know how to determine the Content Type.  

Otherwise any alternate strategies for implementing this feature would be much appreciated.

TIA

Coordinator
Mar 9, 2011 at 1:18 AM

This other post should help, except that you'd change the layout alternates: http://weblogs.asp.net/bleroy/archive/2011/01/10/orchard-shapeshifting.aspx

Mar 9, 2011 at 3:47 AM

Thanks, that helped.  I'm not completely satisfied with what I came up with but it seems to work.  Took some playing around to get the order in which the OnDisplaying events were fired for the various sections.  Here is what I came up with:

 

   public class ContentShapeProvider : IShapeTableProvider
   {
      private readonly IWorkContextAccessor _workContextAccessor;
      private bool _alertPage = false;

      public ContentShapeProvider(IWorkContextAccessor workContextAccessor)
      {
         _workContextAccessor = workContextAccessor;
      }

      public void Discover(ShapeTableBuilder builder)
      {
         builder.Describe("Layout").OnDisplaying(displaying => {
            if (_alertPage) displaying.ShapeMetadata.Alternates.Add("Layout__AlertPage");
            });
         
         builder.Describe("Content").OnDisplaying(displaying => {
               if (displaying.ShapeMetadata.DisplayType == "Detail")
               {
                  ContentItem contentItem = displaying.Shape.ContentItem;
                  _alertPage = (contentItem.ContentType == "AlertPage");
               }
            });
      }
   }

Coordinator
Mar 9, 2011 at 4:18 AM

Yes, you should be able to not have to manage that bool there, by accessing the layout from the work context.

Mar 9, 2011 at 5:03 PM

Yep I think this is it.  Thanks.

   public class ContentShapeProvider : IShapeTableProvider
   {
      private readonly IWorkContextAccessor _workContextAccessor;

      public ContentShapeProvider(IWorkContextAccessor workContextAccessor)
      {
         _workContextAccessor = workContextAccessor;
      }

      public void Discover(ShapeTableBuilder builder)
      {
         builder.Describe("Content").OnDisplaying(displaying => {
               if (displaying.ShapeMetadata.DisplayType == "Detail")
               {
                  ContentItem contentItem = displaying.Shape.ContentItem;
                  _workContextAccessor.GetContext().Layout.Metadata.Alternates.Add(string.Format("Layout__{0}", contentItem.ContentType));
               }
            });
      }
   }