Ticket

Tokenize Taxonomy Term filter

How do you tokenize the taxonomy term filter? Bertrand states in this post that it shouldn't be too hard, but I am not having any luck.

Re: Tokenize Taxonomy Term filter

Tried with a recent dev branch.

Here, for testing, i've done some changes directly in TermsFilter.cs and TermsFilterForms.cs, but based on this example, you will be able to write your own IFilterProvider and IFormProvider...

So, in TermsFilterForms.cs, in the Describe() method where the form is defined, try to add this:

                    ...
                    ),
                    _ProductType: Shape.TextBox(
                        Id: "product-type", Name: "ProductType",
                        Title: T("Product Type"),
                        Classes: new[] { "text medium", "tokenized" },
                        Description: T("Enter the product type.")
                        )
                    );
                    ...

Then, when editing your filter, you will see a new input field that can be tokenized and where e.g you can put:

    {Request.QueryString:product-type}

Then, in TermsFilter.cs, you can inject a tokenizer:

    ...
    private readonly ITokenizer _tokenizer;

    public TermsFilter(ITaxonomyService taxonomyService, ITokenizer tokenizer) {
        _taxonomyService = taxonomyService;
        T = NullLocalizer.Instance;
        _tokenizer = tokenizer;
    }
    ...

Then in the ApplyFilter(dynamic contex) method, you can tokenize your product-type field like this:

        var termName = _tokenizer.Replace((string)context.State.ProductType, null);

Here, for testing, in the query string product-type parameter, i expect only one value (not an array) that is a term name (not an id). But you can change the code according to your needs...

Then, by using the taxonomy service, you can do things like that:

        var taxoPart = _taxonomyService.GetTaxonomyByName("Product Type");
        var termPart = _taxonomyService.GetTermByName(taxoPart.Id, termName);

And then you can use e.g the termPart.Id to update the context.Query (see in the code)...

Best

Monday, January 4, 2016 5:39:28 AM byjtkech
  • jtkech
  • Lv. 13 Rookie
  • Total EXP: 1028

Post a reply

You need to be signed in to post a reply.

Sign In