Setting Custom Portlet Titles in Liferay

Setting custom portlet titles in Liferay can be a little tricky. Changing the title via the portlet configuration in the frontend is easy but when it comes to reading the custom title property programmatically things seem to get a little messy.

Setting the Title

So, first of set your portlet title as usual via the portlet configuration in the frontend. That’s the easy part.

Determining Portle Title Programmatically

Then, in order to determine custom portlet titles use the following code:

ThemeDisplay themeDisplay = ((ThemeDisplay) request
  .getAttribute(WebKeys.THEME_DISPLAY));
String portletId = themeDisplay.getPortletDisplay().getId();
javax.portlet.PortletPreferences portletSetup = PortletPreferencesFactoryUtil
  .getLayoutPortletSetup(themeDisplay.getLayout(), portletId);
String portletCustomTitle = themeDisplay.getPortletDisplay()
  .getTitle();
portletCustomTitle = portletSetup.getValue("portletSetupTitle_"
  + themeDisplay.getLanguageId(), portletCustomTitle);

This code will determine the current portlet’s ID, get its preferences, determine the current namespace and finally retrieve the custom portlet title.

In case you need to read other custom portlet properties simply look up their keys in the database and use the portletSetup.getValue(“yourKey_”, …) option as shown previously.

You may also like...

2 Responses

  1. merchues says:

    It’s better to use:

    PortletConfigurationUtil.getPortletTitle(portletSetup, themeDisplay.getLanguageId())

    Instead of:

    portletCustomTitle = portletSetup.getValue(“portletSetupTitle_”
    + themeDisplay.getLanguageId(), portletCustomTitle);

  2. Larry says:

    Great post.

Leave a Reply

Your email address will not be published. Required fields are marked *