Overriding Admin Templates

In this tutorial we will be making every text field in the admin a WYSIWYG field, via the Redactor WYSIWYG editor. To achieve this, we will override the string form template.

This tutorial is based off of the stock Heat Clinic but should be relevant to any Broadleaf project

In the Broadleaf Open Admin project, we have defined a Thymeleaf ServletContextTemplateResolver. The bean definition looks like this:

<bean id="blAdminWebTemplateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <property name="prefix" value="/WEB-INF/templates/admin/" />
    <property name="suffix" value=".html" />
    <property name="templateMode" value="HTML5" />
    <property name="cacheable" value="${}"/>
    <property name="cacheTTLMs" value="${}" />
    <property name="characterEncoding" value="UTF-8" />
    <property name="order" value="200"/>         

This servlet template resolver is configured to execute prior to any of the other template resolvers:

<bean id="blAdminWebTemplateResolvers" class="org.springframework.beans.factory.config.SetFactoryBean">
    <property name="sourceSet">
            <ref bean="blAdminWebCustomTemplateResolver" />
            <ref bean="blAdminWebTemplateResolver" />
            <ref bean="blAdminWebClasspathTemplateResolver" />
            <ref bean="blWebCommonClasspathTemplateResolver" />

which means that the admin will try to include templates from /WEB-INF/templates/admin before any other location.

If you want to use a different folder you can create another template resolver bean and add it to the blAdminWebTemplateResolvers set

In order to determine which template path we should override, let's first look at the different templates that the admin provides.

Admin Templates

All of the admin field templates are in the 'fields' folder under templates. Since we want to modify all text fields, we will be overriding string.html.

The field template is selected by the fieldType metatadata on the field itself

With this information, actually overriding the template itself can be completed with 2 easy steps:

  1. Create your new string template in src/main/webapp/WEB-INF/templates-admin/fields

Admin string template

  1. Copy the contents of the Broadleaf HTML field template into this one

    <div class="field-label inline" th:text="#{${field.friendlyName}}" th:classappend="${field.required ? 'required' : ''}" />
    <div th:substituteby="components/fieldTooltip" />
    <div th:substituteby="components/fieldTranslation" />
    <span class="error" th:errors="*{fields['__${}__'].value}" />
    <div class="twelve indented-form-value"
        th:with="assetAssociationId=${ entityForm.parentId :}">
        <textarea class="redactor" name="content"
              th:attr="data-select-asset-url=@{${'/'+overrideAssetSectionKey+ '/' + assetAssociationId + '/chooseAsset'}}"
        <textarea class="redactor" name="content"
              th:attr="data-select-asset-url=@{${'/'+sectionKey+ '/' + assetAssociationId + '/chooseAsset'}}"

    Note: this is up to date as of Broadleaf 3.1.1-GA. This may differ slightly depending on the version of Broadleaf you are using

You can utilize this same paradigm to override any other templates in the admin, as long as the path matches the same template path that we have defined in Broadleaf. You can find all of the Broadleaf admin-managed templates available for overriding on GitHub.