Sitecore 8.1 Language Dropdown Sorting in Content Editor

If you have any little experience with Sitecore, you know that the languages available for the website can be maintained under System->Languages, and that they can be seen in the Content Editor, in a dropdown on the right side. But in what order are they displayed in the dropdown and how to change this if we want? My project is built on Sitecore XP 8.1 rev. 160302, and this is actually the first version where Sitecore provides an easy way to change the language ordering.

Below I have the language dropdown with the initial order:

LanguageDropdownUnsorted

There is a setting in Sitecore.config, suggestively called ContentEditor.SortLanguages, which by default is set to false. This means that by default, the dropdown displays the languages in the order they were created. If I change this setting to true, the display order in the dropdown will be the same as the order from System->Languages, which is controlled by the Sort Order field of each Language item.

SortOrder

If you somehow don’t see the setting in Sitecore.config, this is how you define it:

<!--  CONTENT EDITOR SORT LANGUAGES
      Indicates whether the Content Editor show languages in the language selection dropdown
      in the same order as /sitecore/system/Languages child items.
      Default value: false
-->
<setting name="ContentEditor.SortLanguages" value="true"/>

Using a decompiler, you can see that this setting is checked in the OnLoad() method from Sitecore.Shell.Applications.ContentManager.Galleries.Languages.GalleryLanguagesForm (Sitecore.Client.dll):

if (Settings.ContentEditor.get_SortLanguages())
{
    languages = languages.OrderBy<Language, Language>(delegate(Language x)
    {
        return x;
    }, new LanguageComparer(currentItem.Database));
}

After changing the setting, I can see the languages in the dropdown ordered as in System->Languages:

LanguageDropdownSorted

If you want even more language dropdown customization, you could do this by opening the file ”<website>\sitecore\shell\Applications\Content Manager\Galleries\Languages\Gallery Languages.xml” and replacing the call to GalleryLanguagesForm (Sitecore.Client.dll) in the CodeBeside tag with your own class containing the custom code.

Custom MVC Controls for WFFM in Sitecore 8.1

The Web Forms For Marketers module is quite customizable, and the styling of the form controls would normally be manipulated from CSS classes, but I found myself in the situation where I had to adjust not only the styles of the controls, but also the html that renders behind. In this case, I investigated how to create my own custom controls and integrate them with the rest of the WFFM module. My project was built on Sitecore XP 8.1 rev.151003 (Initial Release) version, using the WFFM module, version 8.1 rev. 151008. The purpose is to customize a Single-Line Text control so that the field is surrounded by a <div> with a certain CSS class, has a label with a (*) if the field is required, and has some information displayed if filled in by the editor. The same process can then be followed in order to customize any other form control.

The first step is to check how the classic Single-Line Text control is created, and we can start by looking at the code behind. This information can be retrieved from the corresponding item in Sitecore:

SingleLineText

Our custom control will use the same class as the original control, so we do not need to change anything in the ”Assembly” and ”Class” fields. What we are interested to change is the ”MVC Type” used by our control, because we will implement a new rendering and we will need to link it in this field. Using a decompiling tool, we can see how the view model class of this control is implemented in the SingleLineTextField class (namespace Sitecore.Forms.Mvc.ViewModels.Fields, Sitecore.Forms.Mvc.dll). For most of the controls, the view model class has a strong relation with the view containing the actual control rendering, by having the same name (so naming convention is very important, as with everything in MVC). The views, as installed with the WFFM module, can be found in <website>\Views\Form\EditorTemplates. All the view model classes in the namespace Sitecore.Forms.Mvc.ViewModels.Fields inherit from FieldViewModel class (Sitecore.Forms.Mvc.ViewModels). If the view model of a control has a corresponding view with the same name, it will render that one, otherwise it will render the view of the FieldViewModel (implemented in <website>\Views\Form\EditorTemplates\FieldViewModel.cshtml). For example, the view model for the CheckboxField will render the view CheckboxField.cshtml, whereas the view model for SingleLineTextField will render the view FieldViewModel.cshtml.

So let’s create a new view in our project in the same folder as the other WFFM views, and call it MySingleLineTextField.cshtml. This will contain our code for the custom Single-Line Text rendering, and will reference a new view model with the same name. We will add a new ViewModels folder in our project and add the new class MySingleLineTextField.cs there. If we look at the code in FieldViewModel.cshtml, we can see that it makes use of the Html.BeginField() statement, which renders some tags and classes that I want removed and replaced with a different html structure. By removing this code, we need to make sure that we properly manage the label and the validation display. The advantage is that we can completely control how everything will look like by arranging the html and inserting our own CSS classes.

Views/Form/EditorTemplates/MySingleLineTextField.cshtml

@using Sitecore.Forms.Mvc.Html
@using MyProject.ViewModels
@model MySingleLineTextField

<div class="field">
    <label>
        @Html.BootstrapText("Title")
        @if (Model.IsRequired)
        {
            <abbr class="required" title="Required field">*</abbr>
        }
    </label>
    @Html.BootstrapEditor("Value", new string[] { "input-text" })
    @if (!string.IsNullOrEmpty(Model.Information))
    {
        <p class="small-note">
            @Html.BootstrapText("Information")
        </p>
    }
    <p class="small-note">
        @Html.BootstrapValidationMessage("Value")
    </p>
</div>

ViewModels/MySingleLineTextField.cs

using Sitecore.Forms.Mvc.ViewModels.Fields;

namespace MyProject.ViewModels
{
    public class MySingleLineTextField : SingleLineTextField
    {
        public MySingleLineTextField()
        {
        }       

        public override void Initialize()
        {
            base.Initialize();       
        }
    }
}

Now we can create the custom field in Sitecore and link it with the custom rendering. It is easier to copy the existing ”Single-Line Text” item and rename it. We will add the new item under a ”Custom” folder:

MySingleLineText

The custom field can be added on a new or existing form, like in the screenshot below:

CustomFieldOnForm

As a conclusion, we could create variations for any of the existing form controls, customizing the rendering in any way we want, provided that we also implement a view model class with the same name as the rendering. In order to know what to write in the view model class, just look at the decompiled version of the original view model from the Sitecore.Forms.Mvc.dll.