Show message when projection query has zero results

Topics: Administration, Customizing Orchard, Troubleshooting, Writing modules
May 16, 2012 at 4:34 PM

Does anyone know how to show a message when a projection query comes back with zero results?

Cheers,

Jeff

Coordinator
May 16, 2012 at 5:40 PM

There's an option in there. Edit the layout, use properties, add a property and modify the "no results" section.

Coordinator
May 16, 2012 at 5:44 PM

I think it is for the property itself when there is a content item, but the property is empty ...

May 16, 2012 at 6:29 PM

Mmmm yeah ok see that but I am using the Content display mode though, I want to use my existing shape templates?

May 16, 2012 at 7:46 PM

Couldn't you use a ShapeTable to provide a zero item alternate?

You could probably do something similar by driving the ProjectionPart.

Coordinator
May 17, 2012 at 8:08 AM

Yeah, either you use the property mode and there's a feature in there allowing it, or you choose to use your own templates, where you can easily handle it...

May 17, 2012 at 3:36 PM

reverand,

Thanks, how would the ShapeTable solution work?  I haven't tried this before, can you provide an alternative to be used on a certain condition, such as zero results?  How do you know the results count?

I have tried overriding the ProjectionPart, but it seems to not hit the template if there are no results, so I dont get the opportunity to output anything different, again i'm not 100% how you check the result count.

bertrand,

Thanks, I would like to use my own templates, but as above, the template never gets executed if there are no results, so don't get a chance to output anything!

May 17, 2012 at 4:11 PM
Edited May 17, 2012 at 4:12 PM

You could use the ShapeTable to examine the shape to determine if it's empty, then add the alternate. I haven't done much with them, but it seems to me this would be a viable option.

 

public class EmptyShapes : IShapeTableProvider {

        public void Discover(ShapeTableBuilder builder) {
            builder.Describe("Parts_ProjectionPart")
                .OnDisplaying(displaying => {
                    var shape = displaying.Shape;
                    if (!shape.List.Items.Count() == 0) {
                        shape.Metadata.Alternates.Add("Parts_ProjectionPart__Empty");
                    }
                });
        }
    }

 

(That's off the top of my head, so it may need a tweak or 3)

Now in terms of the driver, did you suppress the driver or just create an additional driver (you can drive a part any number of times)? After thinking about it some more I think the driver approach is probably not optimal, the only way to know if there are no results would be to execute the query again. Not a good plan.

To get at Betrand's answer: You could just as easily as doing the above simply override the view and check for the Shape being empty and then display your desired empty message.

Coordinator
May 17, 2012 at 9:22 PM

If you override the list shape, that never gets called?

May 18, 2012 at 11:25 AM

So it seems as though the ProjectionPart template not getting displayed at all if there are no items.

I have found out why....

I am using a query with a 2 groups, this results in a query on each group and then a 3rd query which attempts to use the HqlRestrictions.InG method to build up a SQL IN statement to get distinct rows.  This method is throwing an exception as the collection sent to it is empty, as there are no results in either of the preceding queries.  This exception is causing the ProjectionPart rendering to fail, as is why I was never getting any of my alternatives to work! I have raised an issue for this http://orchard.codeplex.com/workitem/18697

reverand, thanks for your code, nice one, only a couple of tweeks ;)

public class EmptyShapes : IShapeTableProvider {

        public void Discover(ShapeTableBuilder builder) {
            builder.Describe("Parts_ProjectionPart")
                .OnDisplaying(displaying => {
                    var shape = displaying.Shape;
                    if (shape.List.Items.Count == 0) {
                        shape.Metadata.Alternates.Add("Parts_ProjectionPart__Empty");
                    }
                });
        }
    }

I tried it, but because of the above it wasn't getting called, however it does work with a single group query, so thanks, I've learnt something :)

bertrand, thanks for your help.

May 18, 2012 at 1:06 PM

I had a feeling there was something else going there. It seemed off that other alternates weren't working.

Glad the code worked for you, pulling code off the cuff I do sometimes forget that you can't dynamically dispatch an extension method. :)

If you want to learn more about shapes I'd recommend you give \Core\Shapes\CoreShapes.cs a read. Shapes are more amazing than is apparent from their general usage. My mind got blown when I read the line where the Zone shape takes on a base type. (line 70) Again I don't get a chance too often to really use the power of shapes, but it's nice to know if I wanna do something crazy that shapes are a versatile tool at my disposal.

Jun 3, 2012 at 12:52 PM
Edited Jun 3, 2012 at 12:56 PM

unfortunately properties are not an option for me and add an alternate doesn't work either because the projection part shape isn't shown when there are not results. Is there an other solution for this problem?

Coordinator
Jun 4, 2012 at 5:59 PM

Was a bug filed for this?

Jun 4, 2012 at 6:33 PM

Now it is: http://orchard.codeplex.com/workitem/18730