Tuesday, 2 June 2015

How to remove Liferay custom portlet's preferences while removing portlet from page layout

There are three simple steps to remove the custom portlet's preferences

1) Add "portlet-layout-listener-class" entry inside "liferay-portlet.xml" file
2) Write custom listner which implements "PortletLayoutListener"
3) Deploy portlet and verify functionality

Here are the steps in detailed 

1) Add "portlet-layout-listener-class" entry inside "liferay-portlet.xml" file

    Here is the sample entry which needs to be configured below "<icon>" tag
    <portlet-layout-listener-class>com.student.listener.StudentLayoutListener</portlet-layout-listener-class>

2) Write custom listner which implements "PortletLayoutListener"

    This is the class which will be called and based on the user action on the frontend, specific method will be called
    Here is the sample class entry

    package com.student.listener;
    import com.liferay.portal.model.PortletPreferences;
    public class StudentLayoutListener implements PortletLayoutListener
    {    
        @Override
        public void onAddToLayout(String portletId, long plid) throws PortletLayoutListenerException {
            // Logic while adding portlet on layout            
        }
        
        @Override
        public void onMoveInLayout(String portletId, long plid) throws PortletLayoutListenerException
        {
         // Logic while moving portlet in layout            
        }
    
        @Override
        public void onRemoveFromLayout(String portletId, long plid) throws PortletLayoutListenerException
        {
         // Logic while removing portlet from the layout        
            List<PortletPreferences> portletPreferencesList = PortletPreferencesLocalServiceUtil.getPortletPreferences(aPlid,aPortletId);
                        
         //Iterate over the "portletPreferencesList" and use below method to delete preferences one by one
            PortletPreferencesLocalServiceUtil.deletePortletPreferences(portletPreferences.getPortletPreferencesId());
        }    
    }

3) Deploy portlet and verify functionality

    So configuring custom "PortletLayoutListner" is done just rebuild portlet and after deployment of the portlet verify the functionality. (i.e. remove portlet from the page)      
    [Key Note]: If custom layout listner is not getting called please make sure that Entry inside "liferay-portlet.xml" is proper with correct package structure.

We are done!

Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics

2 comments:

  1. Nice one I found this post very useful and interesting. I have one more concern. For Custom portlet i have seen duplicate entires in PortletPreferences table but i am not able to map the scenarios.

    can you please help me for the same.

    ReplyDelete
    Replies
    1. Thanks Jay!

      Custom portlet's preferences does not remove automatically. Have you written custom listener for your portlet? if yes, please check is it getting called or not? If you want to check working example please refer /portal-web/docroot/WEB-INF/liferay-portlet.xml file in liferay's source and check entry for the portlet "56" (Journal Content)

      Hope it will be helpful!

      Delete