6

I have a blog website, and a page where I can view and search in all the blog posts I have so far. Each blog is tagged with 1-2 tags in the "tagging" field in their respective item on Sitecore 9.

I'm using the "Search Results" SXA component to view all the search results for all blog posts, and a rendering variant to show specific fields from the blog posts, e.g. "title","date" stuff like that.

My question: Is there a way I can show the tags of each individual blog post in the search results using a rendering variant for the search results? If not, is there any other way I can implement this?

7

You can do this. Use VariantReference and then VariantField or VariantToken items and display tag name or field value:

enter image description here

Thanks to a number of extension points in SXA it's fairly easy to implement a custom solution for this.

There is resolveVariantTokens pipeline which contains, for example, such processors:

<resolveVariantTokens>
    <processor type="Sitecore.XA.Foundation.Variants.Abstractions.Pipelines.ResolveVariantTokens.ResolveIFileTypeIcon, Sitecore.XA.Foundation.Variants.Abstractions" resolve="true" />
    <processor type="Sitecore.XA.Foundation.Variants.Abstractions.Pipelines.ResolveVariantTokens.ResolveItemId, Sitecore.XA.Foundation.Variants.Abstractions" resolve="true" />
    <processor type="Sitecore.XA.Foundation.Variants.Abstractions.Pipelines.ResolveVariantTokens.ResolveItemName, Sitecore.XA.Foundation.Variants.Abstractions" resolve="true" />
    <processor type="Sitecore.XA.Foundation.Variants.Abstractions.Pipelines.ResolveVariantTokens.ResolveSize, Sitecore.XA.Foundation.Variants.Abstractions" resolve="true" />
</resolveVariantTokens>

Investigate one or two of them. You will notice that it's quite easy to add your own token called for example $tags and then use it in Token variant item like this:

enter image description here

Your custom processor would simply gets tags out of an item and just return tags name or <span class="tag-something>TAG_NAME</span>.

  • Will the Reference in the variant definition work with a multilist field? Wasn't sure about that one as the documentation only mentions fields linking to a single item. – Gatogordo May 24 '18 at 13:00
  • 1
    @Gatogordo It should handle FielField, ImageField, LinkField, ReferenceField, LookupField and MultilistField. So if you have multiple tags it should render all of them. – Dawid Rutkowski May 24 '18 at 13:20
  • I used this to render Department tags in search results. Worked like a charm! – Michael West Apr 5 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.