Show Terms of a specific Taxonomy from a Razor view

Topics: Customizing Orchard, General, Nederlands (Dutch)
Aug 1, 2013 at 3:25 PM
How can I generate a list with Terms from a specific Taxonomy inside a Razor view?
The Razor view is a custom Shape for a Projection.

I have access to the Terms that are bound to the specific content type inside this projection, but I also need a full list of available Terms to have the ability to filter on the frontend.

Is this possible?

Thanks,
Daniel
Coordinator
Aug 1, 2013 at 8:20 PM
It's possible (through WorkContext you can get an instance of anything, such as a taxonomy service or content manager) but the view is not the right place to do this. If you can, one possibility is to write a little shape table provider that intercepts your special shape and extends it with the additional information.
Aug 1, 2013 at 9:01 PM
Ok, that makes sense. Not sure how to do that.

I have this class:
public class ProductOverviewHelper : IShapeTableProvider {

        public void Discover(ShapeTableBuilder builder) {

            builder.Describe("ProductOverview").OnDisplaying(displaying => {

                if (!displaying.ShapeMetadata.DisplayType.Contains("Detail")) {
                    
                }
            });
        }
    }
Where should I inject the additional information?
I would need a variable that contains the Terms, so that I can add those to the dropdownlist.
Coordinator
Aug 1, 2013 at 9:18 PM
Through constructor injection. You can find examples throughout the code... Open pretty much any class in Orchard and you'll see.
Aug 1, 2013 at 9:44 PM
Okay, that is clear.
I changed my class to this:
public class ProductOverviewHelper : IShapeTableProvider {

        private readonly ITaxonomyService _taxonomyService;

        public ProductOverviewHelper(ITaxonomyService taxonomyService) {
            _taxonomyService = taxonomyService;
        }

        public void Discover(ShapeTableBuilder builder) {

            builder.Describe("Content").OnDisplaying(displaying => {

                if (!displaying.ShapeMetadata.DisplayType.Contains("Detail")) {

                    displaying.Shape.Departments = _taxonomyService.GetTaxonomyByName("Department").Terms;

                }
            });
        }
    }
I'm not quite sure how to use this. Am I going to add the Terms I want to a variable on the Shape? Or do I pass this TaxonomyService to the Shape? Can I add a new variable to the Model that the Shape uses?
Coordinator
Aug 1, 2013 at 9:57 PM
Edited Aug 1, 2013 at 9:59 PM
No, you're already done. Now your view code can access Model.Departments.

(except that I doubt that the shape is "Content")
Aug 1, 2013 at 10:13 PM
Edited Aug 1, 2013 at 10:44 PM
Ah... I see :) Thanks. Got it working.

Now the last challenge I have with this is that I add the Departments for each shape that has:
  • ShapeType == Content
  • DisplayType == Detail
If I check the Shapetracing tool, I can see that the Shape is Content, so that is why I think I can't filter it on any more specific?
Now I have 'Departments' for every Content > Detail page :)

Is that something to configure elsewhere?

Thanks,
Daniel

EDIT: Sorry, did not see the last line of your reply. So your doubt is deservedly.
Coordinator
Aug 2, 2013 at 12:05 AM
I think the problem here is that you are modifying the Content shape. Why do you think you need to do that?
Aug 2, 2013 at 8:51 AM
Yeah, you're right. I was thinking that I needed to change the structure of my Widgets inside CSS. I really want to leave the CSS intact as much as possible, but I could solve it by putting all the needed functionality into one shape.

Thanks,
Daniel