Showing posts with label onRemoveFromLayout. Show all posts
Showing posts with label onRemoveFromLayout. Show all posts

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