0
votes

Legend: I'm working on a Sitecore project where we have a multi-language site with a single content tree where most items have versions in each supported language. I want to develop a clean way of querying items which will exclude items that don't have a version in the current context language.

Ideas: I was thinking of making all templates inherit from a template with a shared checkbox list field to represent the languages supported for a given page.

Note: Just to be clear, this is different from language fallback. We want to hide items which have not been translated into the context language.

Question: Is there a clean way to do this using Sitecore pipelines which would mean XSLTs and any API code which queries the content tree doesn't have to reference this shared template field every time?

3

3 Answers

4
votes

There's not a good way to query that in Sitecore Query. You can detect and filter it out of the results of a query like so:

IEnumerable<Item> items = // some code
myItems.Where(x => x.Versions.Count > 0);

If you're looking to filter before you have all items returned, your checkbox idea is one possible solution but is of course prone to becoming desynced with the actual languages. You could also look at using Lucene and the Advanced Database Crawler module to pack a DynamicField into the index containing a list of languages that have versions that could then be queried. Of course lucene is not always appropriate for general purpose queries.

1
votes

I've had a similar problem some time ago. In my case I wanted the pages that don't have any version for particular language to return 404 Page Not Found instead of blank page. I've created a blog post about this problem which you can find http://www.cognifide.com/blogs/sitecore/sitecore-displaying-pages-without-a-language-version/

In your case I would say that you don't need to add any checkbox for your pages, just check ig the Item.Versions.Count is greater than 0.

0
votes

You could also use Alex Shyba's Partial Language Fallback Module and set enforceVersionPresence to true for the site, and specify a Base Template guid (for say a base template that is used in every page) in the Fallback.EnforceVersionPresenceTemplates. At the basic GetItem database command, his code will check if there is a language version for the current context language and if not, return a null, which will automatically return a 404 from sitecore.

By using this instead of something that checks in the httpBeginRequest pipeline, it will also prevent the items from being returned when using GetChildren, and a multilist field's GetItems method.

I have a 10 blog series post on the topic of language fallback and enforcing version presence here: http://www.sitecore.net/Community/Technical-Blogs/Elizabeth-Spranzani/Posts/2014/03/Fallback-Series-Post-3.aspx

Thanks, Liz