5

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Rashidi Zin
  • 266
  • 2
  • 4
  • 18
  • 3
    `themeDisplay.getScopeGroup().getPathFriendlyURL(layout.isPrivateLayout(),themeDisplay)+themeDisplay.getScopeGroup().getFriendlyURL()+layout.getFriendlyURL();` how about this code? – Pankaj Kathiriya Nov 14 '13 at 05:06
  • I was hoping there's a Util class. But this is good too. Thanks! – Rashidi Zin Nov 18 '13 at 01:58
  • I can see your point, expecting a Util function doing this. Unfortunately, you'll have to wrap this code yourself in a custom function, if you need to call this frequently. Personally, I've created a utility Project, and use the exported .jar in all my portlets – yannicuLar Nov 18 '13 at 13:04
  • 1
    Exactly what I'm doing now :) – Rashidi Zin Nov 22 '13 at 03:04

2 Answers2

4

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

Layout selectedLayout = LayoutLocalServiceUtil.getLayout(plid);
String url = PortalUtil.getLayoutFriendlyURL(selectedLayout, themeDisplay);
Parkash Kumar
  • 4,710
  • 3
  • 23
  • 39
Khiem Tran
  • 66
  • 4
1
  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());

ACV
  • 9,964
  • 5
  • 76
  • 81