5

Is there a way to customise the font-style used by a multi-line text field in the Content Editor interface? Note - I don't care about styling for the front-end as this is handled by our regular CSS.

Bonus points if there is a way of using the source property of the template field to "switch this on" just for certain templates/fields.

2
  • 1
    Customize for display in the Content Editor, or the rendered output on your frontend site?
    – jammykam
    Oct 3 '17 at 16:42
  • Customised for the content editor. Edited accordingly. Oct 3 '17 at 16:45
5

The Multi-Line Text field does not read or use the Source field in any way, unlike some of the other fields. The only way to set additional custom CSS classes using this type of field is to create a custom field.

If you just want to style all Multi-Line Text fields then overriding the style is the best option, but I would suggest you create a custom processor to inject the resources into the Content Editor, which keeps it clean for future upgrades:

public class InjectResources
{
    public List<string> Styles { get; private set; }
    public List<string> Scripts { get; private set; }

    public InjectResources()
    {
        this.Styles = new List<string>();
        this.Scripts= new List<string>();
    }

    public void Process(PipelineArgs args)
    {
        if (Sitecore.Context.ClientPage.IsEvent || HttpContext.Current == null || (HttpContext.Current.Handler as Page) == null)
            return;

        var page = HttpContext.Current.Handler as Page;

        foreach (var style in Styles)
        {
            page.Header.Controls.Add(new LiteralControl($"<link href=\"{style}\" rel=\"stylesheet\" />"));
        }

        foreach (var script in Scripts)
        {
            page.Header.Controls.Add(new LiteralControl($"<script src=\"{script}\"></script>"));
        }
    }
}

And patch this in, specifying your override scripts:

<pipelines>
  <renderContentEditor>
    <processor patch:before="*[1]" type="MyProject.Pipelines.InjectResources, MyProject.Custom">
      <styles hint="list">
        <style>/path/to/custom.css</style>
      </styles>
      <scripts hint="list">
        <script>/path/to/custom.js</script>
      </scripts>
    </processor>
  </renderContentEditor>
</pipelines> 

You can use a CSS file like @SitecoreClimber has recommended:

.scContentControlMemo
{
  font-size: 24px; 
  font-family: "Courier New", Courier, monospace;
}

If you still want to control the styling per field but do not want to create a custom field, then you could use a bit of JavaScript trickey along with an existing field property such as the Short Description field. Set the field to something unique like "[Code]" (this also means that the field alt title in EE does not look off since it is still relevant).

Short Description field

Inject in a JS file and observe the sc:contenteditorupdated event which means your code will fire when a different item is selected in the Content Editor:

document.observe("sc:contenteditorupdated", function(event) {
    $sc(".scContentControlMemo").each(function (index, item) {
        var $fieldContainer = $sc(this).parent();
        if ($fieldContainer.prev().immediateText().endsWith("[Code]:")) {
            $fieldContainer.addClass("code");
        };
    });
});

$sc.fn.immediateText = function() {
    return this.contents().not(this.children()).text();
};

The above will add a code class to the parent div of the Multi-Line Text field. Update the CSS from earlier to take this into account:

.code .scContentControlMemo
{
  font-size: 24px; 
  font-family: "Courier New", Courier, monospace;
}

This ensures that any default fields, such as those defined in Standard Values, do not have the custom styling applied.

Updated field styles

4

You need to include your custom css into file :

\sitecore\shell\Applications\Content Manager\Default.aspx

You need to add :

<link href="yourrelativepathtocustom.css" rel="stylesheet"/>

Your css file it will include:

 **.scContentControlMemo
   {
      font-size:50px; 
      font-family: "Times New Roman", Georgia, Serif;

   }**

These class used by css is added in the constructor of Multiline field (memo field)

// Sitecore.Shell.Applications.ContentEditor.Memo
/// <summary>
/// Initializes a new instance of the <see cref="T:Sitecore.Shell.Applications.ContentEditor.Memo" /> class.
/// </summary>
public Memo()
{
    this.Class = "scContentControlMemo";
    base.Wrap = "soft";
    base.Activation = true;
}

Result of these changes on below picture

enter image description here

0
1

You can do this very simply and code-free on a per-field basis by setting the Style field of the item for that template's field you want to change:

Editing the field item's style field

And this will update the appearance of that particular field when editing an item:

The resulting field

For the curious - in the case of a multi-line text-field, the value you provided in the style field will be injected in as style attribute on the textarea by the Content Editor, as shown here:

The injected style attribute

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.