如何在 Liferay 应用程序显示模板 (ADT) 中访问 "Link to Page" 类型的字段?

How to access a field of type "Link to Page" in Liferay Application Display Templates (ADT)?

我有一个网页内容结构 (WCM) with a Field of type "Link to page" (ddm-link-to-page). Then I created an asset publisher template (ADT) 来显示网页内容页面的 link。

<#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>

问题是显示的值是对元素的引用而不是 link 的值(它显示 /348@public@9246542)所以 URLnode.getText() 不工作。

所有其他领域作品。

ps。我也试过 URLnode.getStringValue()

您的 UrlNode 中 @ 之前的第一个数字是页面布局 ID。您可以通过以下方法使用它从 ADT 中获取 friendlyURL:

...
<#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}

一些方便的参考资料让我走上了正确的轨道: https://web.liferay.com/community/forums/-/message_boards/message/50293771 Programmatically get the url of a page in liferay

最后是工作速度示例:

#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