Setting Custom Portlet Titles in Liferay

Liferay Logo

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.

Comments

2 responses to “Setting Custom Portlet Titles in Liferay”

  1. merchues Avatar
    merchues

    It’s better to use:

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

    Instead of:

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

  2. Larry Avatar
    Larry

    Great post.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.