Projection Filter with token picker to retrieve form querystring

Jan 9, 2012 at 3:28 PM

Could it be possible to have a 'has terms' filter for projection but with a token picker instead of a list of terms to be able to filter with a string retrieved from the query string ?

Jan 9, 2012 at 5:34 PM

Yes, interesting. Actually you should not inject the term name in this case, but the term id.

If you implement it, I'd be happy to include it. Should be very easy:
- Provide a check box to optionally enter a term id
- Show the textbox when checked
- add a .tokenized to the textbox

Look at the examples in the Projector Filters, in Integers, the form is built and also injects some javascript files in the shape. 

Mar 8, 2012 at 9:18 PM

Antoine, did you end up implementing this?

I have some projections that I would like the ability for the user to filter further by other taxonomy terms.  I think having the ability to add an additional filters via the querystring would be a solution.  The ability to have more than one filter is also a requirement.

Mar 8, 2012 at 10:18 PM

I duplicated the 2 files in Contrib.Taxonomies\Projections : TermsFilter.cs and TermsFilterForms.cs => TermIdFilter.cs and TermIdFilterForm.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Contrib.Taxonomies.Models;
using Contrib.Taxonomies.Services;
using Orchard.ContentManagement;
using Orchard.Events;
using Orchard.Localization;

namespace Contrib.Taxonomies.Projections {
    public class TermIdFilter : IFilterProvider {
        private readonly ITaxonomyService _taxonomyService;

        public TermIdFilter(ITaxonomyService taxonomyService) {
            _taxonomyService = taxonomyService;
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        public void Describe(dynamic describe) {
            describe.For("Taxonomy", T("Taxonomy"), T("Taxonomy"))
                .Element("HasTermId", T("Has Term Id"), T("An associated term Id"),
                    (Action<dynamic>)ApplyFilter,
                    (Func<dynamic, LocalizedString>)DisplayFilter,
                    "SelectTermId"
                );
        }

        public void ApplyFilter(dynamic context) {
            string termId = Convert.ToString(context.State.TermId);
            if (!String.IsNullOrEmpty(termId)) {
                //_taxonomyService.GetTerm(Convert.ToInt32(termId));

                Action<IAliasFactory> selector = alias => alias.ContentPartRecord<TermsPartRecord>().Property("Terms", "terms").Property("TermRecord", "termRecord");
                Action<IHqlExpressionFactory> filter = x => x.Eq("Id", termId);
                context.Query.Where(selector, filter);
            }
        }

        public LocalizedString DisplayFilter(dynamic context) {
            string termId = Convert.ToString(context.State.TermId);

            if (String.IsNullOrEmpty(termId)) {
                return T("Any term");
            }
            
            return T("Categorized with {0}", termId);
        }
    }

}

 

 

using System;
using System.Web.Mvc;
using Contrib.Taxonomies.Helpers;
using Contrib.Taxonomies.Services;
using Orchard.DisplayManagement;
using Orchard.Events;
using Orchard.Localization;
using Orchard.UI.Resources;
using Orchard.Environment;
using Orchard.ContentManagement;

namespace Contrib.Taxonomies.Projections {
    public class TermIdFilterForm : IFormProvider {
        public const string FormName = "SelectTermId";
        private readonly ITaxonomyService _taxonomyService;
        private readonly Work<IResourceManager> _resourceManager;
        protected dynamic Shape { get; set; }
        public Localizer T { get; set; }

        public TermIdFilterForm(
            IShapeFactory shapeFactory,
            ITaxonomyService taxonomyService, 
            Work<IResourceManager> resourceManager) {
            _taxonomyService = taxonomyService;
            Shape = shapeFactory;
            T = NullLocalizer.Instance;
            _resourceManager = resourceManager;
        }

        public void Describe(dynamic context) {
            Func<IShapeFactory, object> form =
                shape => {
                    var f = Shape.Form(
                        Id: "TermIdFilter",                        
                        _TermId: Shape.TextBox(
                            Id: "termid", Name: "TermId",
                            Title: T("Term Id"),
                            Classes: new[] { "tokenized" }
                            )
                        );

                    return f;
                };

            _resourceManager.Value.Require("script", "jQuery");

            context.Form(FormName, form);
        }
    }
}

Mar 8, 2012 at 10:37 PM

Great stuff, thanks Antoine.

I could take this further by using a comma separated list from the querystring and build up an And predicate for the query.

Thanks again.

Feb 4, 2013 at 9:18 PM
Hi everyone. Great work here. I was just wondering if the code to accept taxonomy terms in the querystring for filtering a projection has been released? I'm using Orchard v. 1.6, and I don't see an option to add a term ID for a token under filtering.

Thanks,
Justine
Feb 14, 2014 at 5:49 AM
Hi all,
I also want to use terms as a querystring parameter. Is this built in?