0

PortletLayoutListener

  • Asked - Nov 2, 2011, 03:48 PM
    / Viewed 25877 Times
    Hi all,

    I'm trying to perform some actions while a porltet is removed from the page. According to the API doc, I know that I can get the portlet id and the plid(BTW, what is a plid?), but the work I want to do here requires to get the page id and some info from the preference of the portlet being removed. So, what APIs can I use to get those info?

    Thanks a lot.

    1 Answers

    0
    Answered - Nov 2, 2011, 06:37 PM
    (Edited)
    Now I sort of know that I can get portlet preferences with PortletPreferencesLocalServiceUtil.getPortletPreferences(plid, portletid) , but there's no detailed doc describing the returned values. Can someone please give some info?

    I did some google and find plid is the page id. But how can I get the corresponding page name with this id to get the same results just as the following code:

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
    themeDisplay.getLayout().getName(themeDisplay.getLocale());

    I know I can use:

    LayoutLocalServiceUtil.getLayout(plid)

    to get the layout then invoke the getName() method to get the page name, but how do I get the themeDisplay.getLocale()?


    Besides, where should I define this listener to make Liferay aware of it? Thanks a lot.
    Replied - Nov 3, 2011, 08:53 AM
    (Edited)
    Hi Derek,

    You can define your portletLayoutListener in liferay-portlet.xml. See the journal content portlet for an example:

    <portlet>
    		<portlet-name>56</portlet-name>
    		<icon>/html/icons/journal_content.png</icon>
    ...
    		<portlet-layout-listener-class>com.liferay.portlet.journalcontent.JournalContentPortletLayoutListener</portlet-layout-listener-class></portlet>


    At this point it is impossible to find themeDisplay.getLocale() because it's the locale of the current user but you can get the default one with LocaleUtil.getDefault().
    Replied - Nov 3, 2011, 03:55 PM
    (Edited)
    Amos Fong:
    Hi Derek,

    You can define your portletLayoutListener in liferay-portlet.xml. See the journal content portlet for an example:

    <portlet>
    		<portlet-name>56</portlet-name>
    		<icon>/html/icons/journal_content.png</icon>
    ...
    		<portlet-layout-listener-class>com.liferay.portlet.journalcontent.JournalContentPortletLayoutListener</portlet-layout-listener-class></portlet>


    At this point it is impossible to find themeDisplay.getLocale() because it's the locale of the current user but you can get the default one with LocaleUtil.getDefault().



    Thanks a lot, Amos. Here comes the last question:

    I found that the preferences data saved in DB was in XML format, is there a simple way to transform into com.liferay.portlet.Preference?
    Replied - Nov 4, 2011, 02:04 AM
    (Edited)
    The code used in JournalContentPortletLayoutListener.java should also work for you:

    		Layout layout = LayoutLocalServiceUtil.getLayout(plid);
    
    		PortletPreferences preferences =
    			PortletPreferencesFactoryUtil.getPortletSetup(
    				layout, portletId, StringPool.BLANK);
    
    		String articleId = preferences.getValue("article-id", null);