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.
If you have plid
(Page layout Id), use getLayoutFriendlyURL()
like below:
Layout selectedLayout = LayoutLocalServiceUtil.getLayout(plid);
String url = PortalUtil.getLayoutFriendlyURL(selectedLayout, themeDisplay);
ThemeDisplay theme = (ThemeDisplay) getPortletRequest().getAttribute(WebKeys.THEME_DISPLAY);
final long GROUP_ID = theme.getLayout().getGroupId();
Layout destinationLayout = LayoutLocalServiceUtil.getFriendlyURLLayout(GROUP_ID, false, friendlyUrl);
Layout layout = LayoutLocalServiceUtil.getLayout(destinationLayout.getPlid());
String completeUrl = PortalUtil.getLayoutFullURL(layout, getThemeDisplay());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With