Reputation: 93
I am currently working with Liferay Portal Enterprise Edition 6.1 EE GA1 (Paton / Build 6110 / February 15, 2012) which is deployed on my local machine (Win 7/x64) for developing and testing purposes. It runs in the standard 7.0.25 Tomcat and we are using Spring MVC to develop our portlets.
I am adding a new layout (page) to Liferay programmatically and I am setting the type of the type as "link_to_layout" (LayoutConstants.TYPE_LINK_TO_LAYOUT). The layout is created successfully, but so far I have not figured out how I can set a value for the link, e.g. something like "/login". Below is the code I am using to add the layout:
// Spring specific code for getting the Request
HttpServletRequest request = null;
ServletRequestAttributes sa = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if( null != sa ){
request = sa.getRequest();
}
if (request == null) { break; }
// all values are usually retrieved via special methods from our code
// for better readability I have added the real values here
long userId = 10102;
int groupId = 13056;
boolean privateLayout = false;
long plid = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
String title = "my title";
ServiceContext serviceContext = ServiceContextFactory.getInstance( request );
String layoutType = LayoutConstants.TYPE_LINK_TO_LAYOUT;
boolean hidden = false;
String friendlyURL = "/new-page";
// finally, add the layout
Layout layout = LayoutLocalServiceUtil.addLayout( userId, groupId, privateLayout, plid, title, title, StringPool.BLANK, layoutType, hidden, friendlyURL, serviceContext );
I can now manually verify that the page has been created and the page type has been set to "link_to_layout" by checking the Site Pages in the Control Panel. There I see the new page with an empty "Link to Page" value. I can also verify this programmatically by calling the following code (which is shows the message in the console):
if( layout.isTypeLinkToLayout() ){
log.debug( "type is link to page" );
}
If anyone knows how to set the value of the link to page, please share your knowledge with me. Many thanks for your help and your answers :-)
Best regards, Mathias
Upvotes: 2
Views: 3532
Reputation: 93
After spending more time with the issue and debugging through Liferay, I finally came across a solution: The value of the "link to page" page can be set via a property in the TypeSettingsProperties of a Layout. The key "linkToLayoutId" with the necessary layout id takes care of it.
The following code worked for me and I hope it will be beneficial for other developers:
// Spring specific code for getting the Request
HttpServletRequest request = null;
ServletRequestAttributes sa = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if( null != sa ){
request = sa.getRequest();
}
if (request == null) { break; }
// all values are usually retrieved via special methods from our code
// for better readability I have added the real values here
long userId = 10102;
int groupId = 13056;
boolean privateLayout = false;
long plid = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
String title = "my title";
ServiceContext serviceContext = ServiceContextFactory.getInstance( request );
String layoutType = LayoutConstants.TYPE_LINK_TO_LAYOUT;
boolean hidden = false;
String friendlyURL = "/new-page";
// add the layout
Layout layout = LayoutLocalServiceUtil.addLayout( userId, groupId, privateLayout, plid, title, title, StringPool.BLANK, layoutType, hidden, friendlyURL, serviceContext );
String LinkToPageUrl = "/login";
Layout linkToPageLayout = LayoutLocalServiceUtil.getFriendlyURLLayout( groupId, false, linkToPageUrl);
long linkToPageId = linkToPageLayout.getLayoutId();
// set the value of the "link to page"
UnicodeProperties props = layout.getTypeSettingsProperties();
props.put( "linkToLayoutId", Long.toString( linkToPageId ) );
layout.setTypeSettingsProperties( props );
LayoutLocalServiceUtil.updateLayout( layout ); // crucial, 'cause otherwise the changes will not appear on the layout
Upvotes: 5