How to filter content by multiple tags?
I have a dynamic content type (created in module builder) that I need to be able to filter across multiple tags (ie. the user can select one or more tags on a page and a list of content will filter according to the tag(s) that they have selected.)
What is the easiest way to accomplish this?
Hello,
The categories and tags widgets render the taxonomies in tree view control or as a list of categories/tags. The thing with selecting and filtering for multiple categories and tags is that the built in control allows single selection of an item in the tree/list. It does not save a set of selected categories based on which a query can be made to find all news items that have all selected categories.
To have this functionality a custom control have to be created (for example a list for multiple selection) utilizing the taxonomies API to render a list of all categories/tags and allow selection of a set of taxons based on which a query to be executed and filter the list of content items.
After this you can either create your own control to display the dynamic items (with repeater for example), or create a custom DynamicContentViewMaster class (it is responsible for displaying the list of dynamic items). Then override the InitializeControls method of the class like so:
protected
override
void
InitializeControls(Telerik.Sitefinity.Web.UI.GenericContainer container)
int
? totalCount = 0;
this
.DynamicContentListView.DataSource = GetMatchedResult(tagsCollection);
this
.DynamicContentListView.ItemDataBound +=
new
EventHandler<RadListViewItemEventArgs>(
this
.DynamicContentListView_ItemDataBound);
this
.ConfigurePager(totalCount.Value);
if
(
this
.EnableSocialSharing ==
true
)
AddSocialSharing();
public
List<DynamicContent> GetMatchedResult(
string
[] taxonomies)
var providerName = String.Empty;
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
Type testType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.ModuleTest.Test"
);
var matchedData =
new
List<DynamicContent>();
var dynamicItemsAll = dynamicModuleManager.GetDataItems(testType).Where(i=>i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);
var number = taxonomies.Count();
foreach
(DynamicContent itm
in
dynamicItemsAll)
int
counter = 0;
for
(
int
i = 0; i < number; i++)
var tagsDesc = TypeDescriptor.GetProperties(itm).Find(
"Tags"
,
true
);
if
(tagsDesc !=
null
)
// need to implement a check if tagsDesc contains taxonomies[i] and if so increment the counter
counter++;
else
break
;
if
(counter == number)
matchedData.Add(itm);
return
matchedData;