Rashidi Zin
Rashidi Zin

Reputation: 276

Get Layout Friendly URL In Liferay

I want to get a friendly URL for a layout based on layout id. For example, /web/group/page. Currently this is how I do it:

Layout layout = LayoutLocalServiceUtil.getLayout(groupId, false, layoutId);

String groupFriendlyUrl = GroupLocalServiceUtil.getGroup(groupId).getFriendlyURL(); //will output /group
String layoutFriendlyUrl = layout.getFriendlyURL(); //will output /page
String webFriendlyUrl = String.format("/web%s%s", groupFriendlyUrl, layoutFriendlyUrl); //will output /web/group/page

I am wondering if there is a better way to do this where I can the full path, /web/group/page with one method.

Upvotes: 5

Views: 17069

Answers (2)

Khiem Tran
Khiem Tran

Reputation: 66

If you have plid (Page layout Id), use getLayoutFriendlyURL() like below:

Layout selectedLayout = LayoutLocalServiceUtil.getLayout(plid);
String url = PortalUtil.getLayoutFriendlyURL(selectedLayout, themeDisplay);

Upvotes: 4

ACV
ACV

Reputation: 10562

  1. ThemeDisplay theme = (ThemeDisplay) getPortletRequest().getAttribute(WebKeys.THEME_DISPLAY);

  2. final long GROUP_ID = theme.getLayout().getGroupId(); Layout destinationLayout = LayoutLocalServiceUtil.getFriendlyURLLayout(GROUP_ID, false, friendlyUrl);

  3. Layout layout = LayoutLocalServiceUtil.getLayout(destinationLayout.getPlid()); String completeUrl = PortalUtil.getLayoutFullURL(layout, getThemeDisplay());

Upvotes: 1

Related Questions