Reputation: 3133
I've been working on a programmatic creation of Liferay actionUrls in the MVCPortlet
Controller.
So far, I have successfully managed to create a working link to the action of another portlet,
placed in another page (Layout
in Liferay terms) by using this code:
LiferayPortletResponse rr = PortalUtil.getLiferayPortletResponse( response);
Layout layout = ... // I manage to get the correct one using LayoutLocalServiceUtil
LiferayPortletURL ddUrl = rr.createActionURL("portlet_WAR_name") ;
if(Validator.isNotNull(layout)){
// Setting info to the other portlet
ddUrl.setPlid(layout.getPlid());
// Setting the action Name
ddUrl.setParameter(ActionRequest.ACTION_NAME, "actionFunctionNameOf_MVCPortlet");
// adding any action-related params
ddUrl.setParameter("someParam1", ...) );
ddUrl.setParameter("someParam2", ...) );
return ddUrl;
}
My problem is that this will not work on an instanceable Portlet.
Is there some parameter I could add, to make this url link to -any-
instance of the target Portlet?
Or do I need to know the instanceId
of the target Portlet? If so, how can I set this to the LiferayPortletURL
instance?
Thanx in advance, any help would be really appreciated
P.S.: I'm working with Liferay 6.1 GA1
Upvotes: 2
Views: 2808
Reputation: 3133
ok, I found a way, so I'm sharing:
LiferayPortletResponse rr = PortalUtil.getLiferayPortletResponse( response);
// I get the correct layout using LayoutLocalServiceUtil
Layout layout = ... ;
LiferayPortletURL ddUrl = rr.createActionURL("portlet_WAR_name");
if(layout != null) {
// Setting info to the other portlet
ddUrl.setPlid(layout.getPlid());
// Setting the action Name
ddUrl.setParameter(ActionRequest.ACTION_NAME, "actionFunctionNameOf_MVCPortlet");
//Finding the full portletId of the instanceable Portlet
LayoutTypePortlet layoutTypePortlet =
LayoutTypePortletFactoryUtil.create(
LayoutLocalServiceUtil.getFriendlyURLLayout(
themeDisplay.getLayout().getGroupId(),
false, "page_friendly_url")
);
List<String> portletIdList = layoutTypePortlet.getPortletIds();
for(String prtId : portletIdList){
if(prtId.contains("portlet_WAR_name")){
ddUrl.setPortletId(prtId);
}
}
// adding any action-related params
ddUrl.setParameter("someParam1", ...) );
ddUrl.setParameter("someParam2", ...) );
return ddUrl;
}
Thanx to Tony Rad for the hint on setPortletId
.
I also found this answer very useful.
Upvotes: 1
Reputation: 2509
You can set the portletid to the instanceable portlet id:
String portletId = (String) request.getAttribute(WebKeys.PORTLET_ID);
ddUrl.setPortletId(portletId);
Upvotes: 3