Overriding the BuildDisplayContext DisplayType

Topics: Customizing Orchard, Writing themes
Nov 13, 2013 at 3:13 PM
I'm trying to set the BuildDisplayContext DisplayType, based on the URL so that I can show a different layout depending on whether I'm on Product detail page, or Product reviews page.

I thought I'd be able to achieve this by calling OnGetDisplayShape and updating the DisplayType, but I see that BuildDisplayContext.DisplayType has a private set method. Can anyone show me how I can set the DisplayType?

Here's the code it tried to use:
OnGetDisplayShape<ProductPart>((context, part) =>
            {
                // If Url ends in 'Reviews' then set DisplayType as Review
                if (_httpCtxAccessor.Current().Request.FilePath.EndsWith("Reviews"))
                {
                    context.DisplayType = "Review";
                }
            });
and a simplified version of my Placement.info:
  <Match DisplayType="Detail">
    <Place Parts_Reviews="-" />
  </Match>

    <Match DisplayType="Review">
        <Place Parts_Reviews="Content:1" />
    </Match>
Coordinator
Nov 14, 2013 at 12:07 AM
Why don't you change it on the shape's Metadata?
Nov 14, 2013 at 3:09 PM
BertrandLeRoy wrote:
Why don't you change it on the shape's Metadata?
I did think of that, and tried changing it when the Content is displaying as below:
public void Discover(ShapeTableBuilder builder)
        {
            // Execute when the 'Content' Shape is about to be displayed
            builder.Describe("Content").OnDisplaying(displaying =>
            {
                var contentItem = displaying.Shape.ContentItem;

                // If ContentItem is a Product, and Url ends in 'Reviews' then use the Content.Reviews.cshtml template
                if (contentItem != null && contentItem.ContentType == "Product" && _httpCtxAccessor.Current().Request.FilePath.EndsWith("Reviews"))
                {
                    displaying.Shape.Metadata.DisplayType = "Review";
                }
            });
        }
Shape tracing does confirm that the Content Display Type is now set to "Review", but the layout on the page is that of 'Detail', which made me think that the Display Type is only updated after the Placement has been decided.
Nov 14, 2013 at 3:22 PM
I've found a simple workaround that by changing the URL from /{ProductCode}/Reviews to /Reviews/{ProductCode} I can match on path, rather than display type in Placement.info:
 <Match DisplayType="Detail">
    <Place Parts_Reviews="-" />
  </Match>

    <Match Path="/Reviews/*">
        <Place Parts_Reviews="Content:1" />
    </Match>
Solves my problem for the time being :)

Thanks.
Coordinator
Nov 16, 2013 at 6:53 AM
Yes, Displaying may be already too late. Placement is a good workaround however.