How to display Link to Page fields properly in Liferay Dynamic Data Lists?

2.9k Views Asked by At

The context

I am trying to create a link list portlet on a page in my Liferay 6.2 instance. To achieve this, I have put a new Dynamic Data List Display portlet on the page and made a Data Definition that contains a Link to Page (it used to be called Link to Layout before 6.2) field. I am trying to build a custom Display Template using Liferay's guide to display an HTML unordered list with the links, but I cannot find any information regarding how to handle Link to Page field properly.

The question

How can I create a Freemarker template that displays the Link to Page field so that the href attribute contains the smart url of the page and the link text is the localized name of the page?

1

There are 1 best solutions below

0
On

The Display Template editor adds the following code to the Freemarker script when you click your field:

<a href="${ddmUtil.getDisplayFieldValue(themeDisplay, cur_record.getFieldValue("Link_to_Page1632", locale), cur_record.getFieldType("Link_to_Page1632"))}">

Link to Page

</a>

This is a good hint to start displaying the links, just add the small details:

<#-- The record service to retrieve the list of records in this Dynamic Data List -->
<#assign DDLRecordLocalService = serviceLocator.findService("com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalService")>

<#-- The layout service that helps determine the name of the page -->
<#assign layoutService = serviceLocator.findService("com.liferay.portal.service.LayoutService")>

<#-- Get the records in the Dynamic Data List -->
<#assign records = DDLRecordLocalService.getRecords(reserved_record_set_id)>

<ul>
<#if records?has_content>
    <#list records as cur_record>
        <li>
            <#-- Use the snippet provided by the editor -->
            <a href="${ddmUtil.getDisplayFieldValue(themeDisplay, cur_record.getFieldValue("Link_to_Page1632", locale), cur_record.getFieldType("Link_to_Page1632"))}">

            <#-- Get the name of the page with layoutService.getLayoutName() using a temporary JSON object -->
            <#assign jsonObj = jsonFactoryUtil.createJSONObject(cur_record.getFieldValue("Link_to_Page1632"))>
            ${layoutService.getLayoutName(jsonObj.getLong("groupId"), jsonObj.getBoolean("privateLayout"), jsonObj.getLong("layoutId"), localeUtil.toLanguageId(locale))}

            </a>
        </li>
    </#list>
</#if>
</ul>