Reputation: 1421
I'm working on Liferay 6.1
I want to delete a portlet in Liferay 6.1 from my code.
What I have done so far is:
<a onclick="Liferay.Portlet.close('#p_p_id_28_'); return false;">Remove</a>
Above code is working fine. But it is working on the current page only i.e. it can delete the portlet(s) which is there on the current page only.
But I want to delete the portlet(s) which could be some where on the menu of my portal using its layout id.
Please suggest a way out. Thanks in advance.
Regards,
Varun Jain
Upvotes: 1
Views: 1460
Reputation: 1421
public void removePortlets(ActionRequest request, ActionResponse response)
throws PortletException {
ThemeDisplay themeDisplay = (ThemeDisplay) request
.getAttribute(WebKeys.THEME_DISPLAY);
long groupId = themeDisplay.getScopeGroupId();
String friendlyURL = "/demochildpage";
boolean privateLayout = false;
long userId = themeDisplay.getUserId();
try {
Layout layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
groupId, privateLayout, friendlyURL);
LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout
.getLayoutType();
layoutTypePortlet.removePortletId(userId, "28");
LayoutLocalServiceUtil.updateLayout(layout.getGroupId(),
layout.getPrivateLayout(), layout.getLayoutId(),
layout.getTypeSettings());
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 2
Reputation: 4730
Never tried, but I think, you can achieve what you want using Liferay's service.
The process seems to be simple enough as following:
1. Get list of all pages / layouts using com.liferay.portal.service.LayoutLocalServiceUtil.getLayouts(long groupId, boolean privateLayout)
2. Iterate layout list and extract list of portletIds from layout object casted to com.liferay.portal.model.LayoutTypePortlet
3. Iterate portletIds and compare each porteltId with your portletId string.
4. If matched call com.liferay.portal.model.layoutTypePortlet.removePortletId(long userId, String portletId)
and update layout using LayoutLocalServiceUtil.updateLayout(Layout layout)
So, the sample code would look like:
String portletId = "#p_p_id_28_";
long userId = user.getUserId();
ArrayList<Layout> layouts = LayoutLocalServiceUtil.getLayouts(10180, true);
for(Layout layout : layouts){
if(!layout.isHidden()){
LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();
ArrayList<String> layoutPortletIds = layoutTypePortlet.getPortletIds();
for(String layoutPortletId : layoutPortletIds){
if(layoutPortletId.equalsIgnoreCase(portletId)){
layoutTypePortlet.removePortletId(userId, portletId);
LayoutLocalServiceUtil.updateLayout(layout);
}
}
}
}
Upvotes: 1
Reputation: 590
Way to go would be: query the database for all Layouts on which the portlet is on, than iterate and delete. You would need to implement a custom query or a dynamic query if I am not overseeing something. Maybe there already is a service function to get the desired Layout Id's, but I doubt that.
Upvotes: 1