3

I have a web content structure (WCM) with a Field of type "Link to page" (ddm-link-to-page). Then I created an asset publisher template (ADT) to display the link for pages of the web content.

<#assign journalArticle = assetRenderer.getArticle() />
<#assign document = saxReaderUtil.read(journalArticle.getContentByLocale(locale.toString())) />
<#assign URLnode = document.selectSingleNode("/root/dynamic-element[@name='Link_To_Page']/dynamic-content") />
<#assign getURL = URLnode.getText() />

<a href="${getURL}">Go to page</a>

The problem is that the value displayed is a reference to the element and not the value of the link (it shows /348@public@9246542) so URLnode.getText() is not working.

All other field works.

ps. I also tried with URLnode.getStringValue()

Mustapha Aoussar
  • 5,833
  • 15
  • 62
  • 107
  • 1
    First of all, I don' see `selectSingleNode` method from `document` type in API. `saxReaderUtil.read` is returning `com.liferay.portal.kernel.xml.Document` which doesn't contain any `selectSingleNode` method. Also, did you try using `URLnode,getLink()` to get link? – Parkash Kumar Jan 13 '16 at 08:44
  • Thank you! I treid with `URLnode.getLink()` but doesn't work. However, all other fields work except for this field `ddm-link-to-page`. – Mustapha Aoussar Jan 13 '16 at 14:48
  • I also tried with `getFriendlyUrl()` – Mustapha Aoussar Jan 13 '16 at 14:59

2 Answers2

2

The first number before the @ in your UrlNode is the page layout ID. You can use this to get the friendlyURL from within an ADT using the following method:

...
<#list rootElement.elements() as dynamicElement>
    <#if "URLnode" == dynamicElement.attributeValue("name")>
        <#assign getURL = dynamicElement.element("dynamic-content").getData() />

        <#-- split the string to just get the page layout id , groupID and if its a private layout-->
        <#list page_link_data?split("@") as item>
            <#if item_index == 0>
                <#assign linkPageId = item?number>
            </#if>
            <#if item_index == 1>
                <#if item == "private">
                    <#assign linkPrivate = true>
                <#else>
                    <#assign linkPrivate = false>
                </#if>
            </#if>
            <#if item_index = 2>
                <#assign linkGroupId = item?number>
            </#if>
        </#list>

        <#-- get layout -->
        <#assign layoutLocalService = staticUtil["com.liferay.portal.service.LayoutLocalServiceUtil"]>
        <#assign pageLayout = layoutLocalService.getLayout(linkGroupId, linkPrivate, linkPageId)>

        <#-- get Friendly URL -->
        <#assign portalUtil = staticUtil["com.liferay.portal.util.PortalUtil"]>
        <#assign friendly_page_link = portalUtil.getLayoutFriendlyURL(pageLayout, themeDisplay)>

    </#if>
</#list>

${friendly_page_link}

Couple of handy references that got me on the right track: https://web.liferay.com/community/forums/-/message_boards/message/50293771 Programmatically get the url of a page in liferay

Community
  • 1
  • 1
andyxmas
  • 521
  • 4
  • 7
2

Here is working velocity example finally:

#if (!$entries.isEmpty())
    #foreach ($entry in $entries)
        #set($renderer = $entry.getAssetRenderer() )
        #set($className = $renderer.getClassName() )
        #if( $className == "com.liferay.portlet.journal.model.JournalArticle" )
            #set( $journalArticle = $renderer.getArticle() )
            #set( $document = $saxReaderUtil.read($journalArticle.getContent()) )
            #set( $rootElement = $document.getRootElement() )
            #set( $link = $renderer.getURLViewInContext($renderRequest, $renderResponse, '') )
            #set( $xPathSelector = $saxReaderUtil.createXPath("dynamic-element[@name='Link_to_Page']") )
            #set( $Link_to_Page = $xPathSelector.selectSingleNode($rootElement).getStringValue().trim() )
            #set( $at = "@" )
            #set( $issueUrl_Array = $Link_to_Page.split($at) )
            #set( $layoutLocalService = $serviceLocator.findService('com.liferay.portal.service.LayoutLocalService') )
            #set( $val = 0 )
            #set( $group = $val.parseInt($issueUrl_Array.get(2)) )
            #set( $layout = $val.parseInt($issueUrl_Array.get(0)) )
            #set( $pageLayout = $layoutLocalService.getLayout($group, false, $layout) )
            #set( $Link_to_Page_Url = $pageLayout.getFriendlyURL() )

            <p>$Link_to_Page_Url</p>
        #end
    #end
#end
nakrill
  • 720
  • 6
  • 7