This project is read-only.

Language Filter in Find method

Jan 16, 2009 at 4:23 PM
Hi,
I have following situation:

I have Root Node under which I have Eng Strart Page under which I have German Start Page.
Under Eng Start page i have Database search which include children under GER Start page which I want to exclude but don't know how?

This is how structure looks like:
>>Root
>>>>ENG Start Page (type of LanguageRoot)
>>>>>>>ENG Database Search Page
>>>>>>>ENG Page 1
>>>>>>>ENG Page 2
>>>>>>>ENG Page 3

>>>>>>>GER Start Page (type of LanguageRoot)
>>>>>>>>>GER Database Search Page
>>>>>>>>>GER Page 1
>>>>>>>>>GER Page 2
>>>>>>>>>GER Page 3

When I use GER Database search page, search root is GER Start Page and I get only result under GER Start Page but when I use ENG Database Search result from German node is also included (German Page 1/2/3) because they are children of ENG Start page
.
Is there a way to add LanguageFilter in search or to use Find method which will exclude results from GER Start Page (or any LanguageRoot type) and all pages under it in structure?

Thanks!


>>>>>>>>>GER Page 1
Jan 18, 2009 at 11:10 AM
You could create your own filter and use it in a subclass of the database search item overriding the GetFilters method.
Jan 18, 2009 at 3:39 PM
Thanks for reply ....

Actually, that was my question, how to create filter or query to exclude whole branch (in this case LanguageRoot and nodes under it)?

Thanks.
Jan 18, 2009 at 8:16 PM
Just an idea:
  • Create a class and inherit from ItemFilter
  • Pass the language root to the class
  • In the match method call ILanguageGateway.GetLanguageRoot and compare against the language root passed to the class
This assumes there are not too many items in the result set.

Doing this in the database would require saving some additional information on the content item .E.g. a reference to the language root.
Dec 17, 2010 at 6:39 PM

Good idea. I did just that and it seems to work. Here's an untested code fragment:

using [namesapace].Pages;
using N2;
using N2.Collections;
using N2.Engine.Globalization;

namespace [namesapace] {
    public class LanguageFilter : ItemFilter {
        private readonly ILanguageGateway _languageGateway;
        private readonly LanguageRoot _languageRoot;

        public LanguageFilter(ILanguageGateway languageGateway, LanguageRoot languageRoot) {
            _languageGateway = languageGateway;
            _languageRoot = languageRoot;
        }

        public override bool Match(ContentItem item) {
            return _languageGateway.GetLanguage(item).LanguageCode == _languageRoot.LanguageCode;
        }
    }
}

And it's called in AbstractSearch.GetFilters() like so:

filters.Add(new LanguageFilter(Context.Current.Container.Resolve<ILanguageGateway>(), Find.ClosestLanguageRoot));