How to determine Display Type from within field alternate

Topics: Customizing Orchard, Writing themes
May 16, 2013 at 2:12 AM
I have a field on a content type and wish to render this field differently when viewing the content item in detail view (as opposed to summary).

Firstly I thought I might be able to define an alternate for the field based on display type, but it seems alternates based on display type can only be defined for content types, not fields within a content type? Something like Fields.Numeric-MyField.Detail.cshtml would be nice?

So failing this, I'm looking at defining an alternate for the field that has conditional formatting based on the display type. So my alternate would be Fields.Numeric-MyField.cshtml, and within I wish to determine if the Display Type is Detail. Is there a way within a view to determine the display type?
May 16, 2013 at 2:24 AM
Nevermind this question, I figured it out. I forgot about defining alternates via placement.info.

I just defined a new alternate for the field with a match on DisplayType in the placement.info and it works a treat!
May 16, 2013 at 3:07 AM
I think I jumped the gun a little here - The new alternate that I defined in placement.info shows up in the Shape Tracing tool as an available alternate, however when I define this alternate (named exactly as specified) it's being ignored.

Has anyone had any luck defining alternates for fields specifically for a content type and display type?

This is what's in my placement.info...
  <Match ContentType="MyContentType">
    <Match DisplayType="Detail">
      <Place Fields_Numeric-MyField="Content:after;Alternate=Fields_Numeric__MyContentTypeMyFieldDetail"/>
    </Match>
</Match>
The shape tracing tool then shows that I have the following alternate available

~/Themes/MyTheme/Views/Fields.Numeric-MyContentTypeMyFieldDetail.cshtml

I've created this alternate and added it to my project, however it is ignored when reloading the page.
May 16, 2013 at 4:02 AM
Found a solution from this post

http://stackoverflow.com/questions/14707053/orchard-taxonomies-shape-alternates-by-displaytype

Adding the following factory class did the trick...
public class PartContentTypeAlternateFactory : ShapeDisplayEvents {
    public override void Displaying(ShapeDisplayingContext context) {
        context.ShapeMetadata.OnDisplaying(displayedContext => {
            var alternates = displayedContext.ShapeMetadata.Alternates.Select(a => a + "__" + displayedContext.ShapeMetadata.DisplayType);
            displayedContext.ShapeMetadata.Alternates = displayedContext.ShapeMetadata.Alternates.Union(alternates).ToList();
        });
    }
}
Is this something worth putting into the core product?
Coordinator
May 16, 2013 at 5:52 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.