2

I am using Sitecore 9.3 and working on scaled environment. First I have created tags with two versions English and Spanish for every tag like below "Sketch Tag" enter image description here I needed to tag content Page with two languages Spanish and English with using Tag Cloud Component. When I visited this Page on CD in English version, I found "Sketch Tag" appears in both the languages English and Spanish, the same way when I visited page in Spanish version. What is the way to solve this problem ?

Thanks in advance.

1 Answer 1

1

This is a limitation of OOTB Tag cloud (sxa 93), also noticed this was not corrected in sxa10 as well.

Workaround : Clone the SXA TagCloud component and override TryQueryFacet from TagCloudRepository (Sitecore.XA.Feature.Taxonomy.Repository.TagCloudRepository) to make the query aware of the context Item language.

Example Code:

protected override bool TryQueryFacet(
      Item contextItem,
      string faceFieldName,
      out FacetResults query)
    {
        string contextItemId = contextItem.ID.ToSearchID();
        using (IProviderSearchContext providerSearchContext = this.PrepareSearchContext(this.MultisiteContext.GetSiteItem(contextItem)))
        {
            var expression = (Expression <Func<SearchResultItem, bool>>)PredicateBuilder.True<SearchResultItem>();
            expression = expression.And(i => i["_path"] == contextItemId);
            
            if(contextItem.IsFallback)
            {
                var fallbackLanguage = Sitecore.Data.Managers.LanguageFallbackManager.GetFallbackLanguage(Sitecore.Context.Language, Sitecore.Context.Database);
                expression = expression.And(i => i["_language"] == fallbackLanguage.Name);
            }
            else
            {
                expression = expression.And(i => i["_language"] == Sitecore.Context.Language.Name);
            }

            query = providerSearchContext.GetQueryable<SearchResultItem>().Where<SearchResultItem>(
                (Expression<Func<SearchResultItem, bool>>)expression).FacetOn<SearchResultItem, string>(
                (Expression<Func<SearchResultItem, string>>)(f => f[faceFieldName])
                ).GetFacets<SearchResultItem>();
        }
            
        return query != null;
    }

Notice the expression in snippet above which includes the context Item language and also handles the fallback.

Link to code gist here on the Controller and Repository: https://gist.github.com/beyondsun/bd031960ebc9801450f469d85e42c105

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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