Search code examples
filterorchardcmsprojection

Orchard filter projection query based on another module field


On Orchard 1.6 I have defined a custom content type named Offer, this Offer has a pack field. On the page displaying one offer I want to display a short list of the other offers with the same pack.

To do this I have tried to make a projection but how can I specifiy in the query filter that the pack field must be equals to the pack field of the offer currently displaid?

Thank you.


Solution

  • You can write a content handler to store the currently displayed content item for later use in the request:

    public class MyContentHandler : ContentHandler
    {
        readonly IOrchardServices orchardServices;
    
        public MyContentHandler (
            IOrchardServices orchardServices)
        {
            this.orchardServices = orchardServices;            
        }
    
        protected override void BuildDisplayShape(BuildDisplayContext context)
        {
            if (context.DisplayType == "Detail" && ((IShape)context.Shape).Metadata.Type == "Content" &&
                orchardServices.WorkContext.GetState<ContentItem>("currentContentItem") == null)
            {
                orchardServices.WorkContext.SetState("currentContentItem", context.ContentItem);
            }
        }
    }
    

    Then you can write a projection filter using the content item reference stored in the state. (See Orchard.Tags.Projections.TagsFilter as an example of how to write a projection filter.)