You can use many different kinds of WYSIWYG editors to edit content in portlets. Depending on the content you’re editing, you may want to modify the editor to provide a customized configuration for your needs. In this tutorial, you’ll learn how to modify the default configuration for Liferay DXP’s supported WYSIWYG editors to meet your requirements.
Updating the Editor’s Configuration
To modify the editor’s configuration, create a module with a component that
implements the
EditorConfigContributor
interface. Follow these steps to modify one of Liferay DXP’s WYSIWYG editors:
-
Open the portlet’s
build.gradle
file and update thecom.liferay.portal.kernel
version
to3.6.2
. This is the version bundled with the Liferay DXP release. -
Create a unique package name in the module’s
src
directory, and create a new Java class in that package that extends theBaseEditorConfigContributor
class: -
Create a component class that implements the
EditorConfigContributor
service:@Component( property = { }, service = EditorConfigContributor.class )
-
Add the following imports:
import com.liferay.portal.kernel.editor.configuration.BaseEditorConfigContributor; import com.liferay.portal.kernel.editor.configuration.EditorConfigContributor; import com.liferay.portal.kernel.json.JSONArray; import com.liferay.portal.kernel.json.JSONFactoryUtil; import com.liferay.portal.kernel.json.JSONObject; import com.liferay.portal.kernel.portlet.RequestBackedPortletURLFactory; import com.liferay.portal.kernel.theme.ThemeDisplay;
-
Specify the editor’s name, editor’s configuration key, and/or the portlet name(s) where the editor resides. These three properties can be specified independently, or together, in any order. See the
EditorConfigContributor
interface’s Javadoc for more information about the available properties and how to use them. The example configuration below modifies the AlloyEditor’s Content Editor, identified by thecontentEditor
configuration key andalloyeditor
name key.Two portlet names are declared (Blogs and Blogs Admin), specifying that the service applies to the content editors in those portlets. Lastly, the configuration overrides the default one by providing a higher service ranking:
@Component( property = { "editor.config.key=contentEditor", "editor.name=alloyeditor", "javax.portlet.name=com_liferay_blogs_web_portlet_BlogsPortlet", "javax.portlet.name=com_liferay_blogs_web_portlet_BlogsAdminPortlet", "service.ranking:Integer=100" }, service = EditorConfigContributor.class )
-
Override the
populateConfigJSONObject()
method to provide the custom configuration for the editor. This method updates the original configuration JSON object. It can also Update or delete existing configurations, or any other configuration introduced by another*EditorConfigContributor
.@Override public void populateConfigJSONObject( JSONObject jsonObject, Map<String, Object> inputEditorTaglibAttributes, ThemeDisplay themeDisplay, RequestBackedPortletURLFactory requestBackedPortletURLFactory) { }
-
In the
populateConfigJSONObject
method, you must instantiate aJSONObject
to hold the current configuration of the editor. For instance, you could use the code snippet below to retrieve the available toolbars for the editor:JSONObject toolbars = jsonObject.getJSONObject("toolbars");
-
Now that you’ve retrieved the toolbar, you can modify it. The example below adds a camera button to the AlloyEditor’s Add toolbar. It extracts the Add buttons out of the toolbar configuration object as a
JSONArray
, and then adds the button to thatJSONArray
:if (toolbars != null) { JSONObject toolbarAdd = toolbars.getJSONObject("add"); if (toolbarAdd != null) { JSONArray addButtons = toolbarAdd.getJSONArray("buttons"); addButtons.put("camera"); } }
The configuration JSON object is passed to the editor with the modifications you’ve implemented in the
populateConfigJSONObject
method. -
Finally, generate the module’s JAR file and copy it to your
deploy
folder. Once the module is installed and activated in the service registry, your new editor configuration is available for use.
Liferay DXP supports several different types of WYSIWYG editors, which include (among others):
Make sure to visit each editor’s configuration API to learn what each editor offers for configuration settings.