Sunday, 23 August 2015

Change sort order of languages in the language menu

Leave a Comment
Lately I have been playing more with language menu in Sitecore 8 content editor. Check this post how to enable flag icon in language drop down menu in Sitecore. If you have Sitecore site with many languages then probably you will find one thing most annoying – order of languages in language menu. By default, Sitecore displays the languages in the language selector ordered by the created date but I want to sort languages based on language name in languages menu. Below steps illustrates how to change sort order in which the languages are displayed in the languages menu:
  1. Create a new class and inherit Sitecore.Data.SqlServer.SqlServerDataProvider class.
  2. Override function GetLanguages() and sort languages  using the IComparer interface using a helper class by providing your own implementation for sort logic.
    using Sitecore.Collections;
    using Sitecore.Globalization;
    using System;
    using System.Collections.Generic;
    
    namespace SitecoreCustomizedLanguageOrder
    {
        public class LanguageSortOrder : Sitecore.Data.SqlServer.SqlServerDataProvider
        {
            public LanguageSortOrder(string connectionString) : base(connectionString) { }
    
            private class LanguageComparer : IComparer<Language>
            {
                #region IComparer<Language> Members
                LanguageSortOrder provider;
                public LanguageComparer(LanguageSortOrder dataProvider)
                {
                    this.provider = dataProvider;
    
                }
                public int Compare(Language x, Language y)
                {
                    return string.Compare(x.CultureInfo.EnglishName, y.CultureInfo.EnglishName);
                }
    
                #endregion
            }
            
            public override LanguageCollection GetLanguages()
            {           
                Language[] languages = base.GetLanguages().ToArray();           
                Array.Sort(languages, new LanguageComparer(this));
                LanguageCollection result = new LanguageCollection();
                foreach (Language language in languages)
                {
                    result.Add(language);
                }
                return result;
            }
        }
    }
    
  3. Compile source code and placed dll in bin folder. 
  4. Create new Sitecore Config patch file (CustomDataProvider.config) and provide following data provider definition:
    Old data provider definition
    <main type="Sitecore.Data.$(database).$(database)DataProvider, Sitecore.Kernel">
            <param connectionStringName="$(1)"/>
            <Name>$(1)</Name>
          </main>    
    New data provider definition
    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <sitecore>
        <dataProviders>      
          <main type="SitecoreCustomizedLanguageOrder.LanguageSortOrder,SitecoreCustomizedLanguageOrder" patch:instead="main">
            <param connectionStringName="$(1)"/>
            <Name>$(1)</Name>
          </main>
        </dataProviders>
      </sitecore>
    </configuration>
  5. Deploy CustomDataProvider.config file in \Website\App_Config\Include folder.
  6. Create a custom event handler which will clear the cache when the language sort order is changed. Here’s the code to achieve this:
    using Sitecore;
    using Sitecore.Caching;
    using Sitecore.Data.Items;
    using Sitecore.Events;
    using System;
    
    namespace SitecoreCustomizedLanguageOrder
    {
        public class NotifyLanguageSortOrderChanged
        {
            public void OnItemSortorderChanged(object sender, EventArgs e)
            {
                Item item = Event.ExtractParameter(e, 0) as Item;
                if (item.TemplateID == TemplateIDs.Language)
                {
                    foreach (Cache cache in CacheManager.GetAllCaches())
                    {
                        if (cache.Name == "LanguageProvider - Languages")
                        {
                            cache.Clear();
                        }
                    }
                }
            }
        }
    }
    
    
  7. Create new Sitecore Config patch file (LanguageSortOrderChanged.config ) and add the new event handler.
    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <sitecore>
        <events>
          <event name="item:sortorderchanged" >
            <handler type="SitecoreCustomizedLanguageOrder.NotifyLanguageSortOrderChanged,SitecoreCustomizedLanguageOrder" method="OnItemSortorderChanged" />
          </event>
        </events>
      </sitecore>
    </configuration>
    
  8. Deploy LanguageSortOrderChanged.config file in \Website\App_Config\Include folder.


Source code can be downloaded from github. Comments and suggestions are most welcome. Happy coding!

0 comments :

Post a comment