Original PortletConfig in Portlet Configuration

Posted on by Charalampos Chrysikopoulos

Lets assume you have a Project with its own Language properties and a portlet that have a configuration page.

Normally, if you want to get a literal value from the language properties in the doView method of your portlet you can use the following code:

String value = LanguageUtil.get(getPortletConfig(), themeDisplay.getLocale(), "literal-key");

But if you want to do the same in the configuration render method then you will encounter a problem with the portletConfig object, that comes as a parameter in the method.

public String render(PortletConfig config, RenderRequest request, RenderResponse response) {
    String value = LanguageUtil.get(config, themeDisplay.getLocale(), "literal-key");
    // ...
}

The reason is that the configuration is a portlet from its own, and the portletConfig that you get is the config of the configuration portlet and not from the original portlet. And the configuration portlet is is not part of your plugin project, so it can't see the custom language properties that you have.

The solution is simple. Just get the portletConfig of your original portlet. For example use the following code:

String origPortletResource = ParamUtil.getString(request, "portletResource");
HttpServletRequest servletRequest = PortalUtil.getHttpServletRequest(request);
ServletContext servletContext = servletRequest.getSession().getServletContext();
Portlet origPortlet = PortletLocalServiceUtil.getPortletById(origPortletResource);
PortletConfig origConfig = PortletConfigFactoryUtil.create(origPortlet, servletContext);
String value = LanguageUtil.get(origConfig, themeDisplay.getLocale(), "literal-key");
This entry was posted in Liferay and tagged Configuration, Liferay, liferay 6.1 by Charalampos Chrysikopoulos