Skip to end of metadata
Go to start of metadata

When we configure the ADT to look for content with more than 1 structure, sometimes sorting the collection of results by a specific dynamic field is needed. We propose this snippet to sort it. The only recommendation is to be sure the sorting field has the same name in all structures.

<#--assign sortFieldName = "dateField" /-->
<#assign sortFieldName = "textField" />
<#--assign sortType = "ASC" /-->
<#assign sortType = "DESC" />
<#assign dynamicElements = [] />
<#if entries?has_content>
    <#assign sortedEntries = sortEntries(sortFieldName, sortType, entries) />
	<h3>Sort By: "${sortFieldName}", Sort Type: ${sortType}</h3>
    <#if sortedEntries?has_content>
        <#list sortedEntries as dynElem>
            <#assign dynSortField = dynElem.sortField />
            <#assign dynDocument = dynElem.document />
            <#assign dynEntry = dynElem.entry />
            <#assign dynTitle = dynEntry.getTitle(locale) />
            
            <div>
                <b>Title:</b> ${dynTitle} 
            </div>
            <div>
                <b>${sortFieldName}:</b> ${dynSortField}
            </div>
            <#if dynEntry.getDescription(locale)?? >
                <#assign dynDescription = dynEntry.getDescription(locale) />
                <div>
                <b>Description:</b> ${dynDescription}
                </div>
            </#if>

            <#if dynDocument.valueOf("//dynamic-element[@name='testText']")?? >
                <#assign testText = dynDocument.valueOf("//dynamic-element[@name='testText']") />
                <div>
                <b>testText:</b> ${testText}
                </div>
            </#if>
            
            <hr>
        </#list>
    </#if>
</#if>



<!-- start functions -->
<#function sortEntries sortFieldName sortType entries>
    <#assign dynamicElements = [] />
    <#if entries?has_content>
        <#list entries as curEntry>
            <#assign renderer = curEntry.getAssetRenderer()>
            <#assign className = renderer.getClassName() >
            <#if className == "com.liferay.journal.model.JournalArticle" >
                <#assign journalArticle = renderer.getArticle() >
                <#assign document = saxReaderUtil.read(journalArticle.getContentByLocale(locale)) >
                
                <#if document.valueOf("//dynamic-element[@name='" + sortFieldName + "']")?? >
                    sortFieldName = ${sortFieldName}
                    <#assign sortField = document.valueOf("//dynamic-element[@name='" + sortFieldName + "']") />
                    sortField = ${sortField}
                    <#assign dynamicElements = dynamicElements + [ {"sortField":sortField, "document":document, "entry": curEntry} ] />
                </#if>
            </#if>
            
        </#list>
    </#if>

    <#if dynamicElements?has_content>
        <#if sortType == "ASC">
            <#assign sortedDynDocument = dynamicElements?sort_by("sortField") />
        </#if>
        <#if sortType == "DESC">
            <#assign sortedDynDocument = dynamicElements?sort_by("sortField")?reverse />
        </#if>
        <#return sortedDynDocument>
    <#else>
        <#return dynamicElements>
    </#if>

</#function>
<#-- end functions -->

  • No labels