RE: ADT in Liferay 7.1

thumbnail
Gustavo Oliveira, modified 7 Years ago. Junior Member Posts: 98 Join Date: 9/23/15 Recent Posts
Hi, im testing this new version 7.1. Im trying to create an ADT to show images from a structure, this code worked on 7.0 but now the image field is returning a JSON. Is there a new method to get the image url? Thanks


​​​​​​​<#if entries?has_content>

    <#list entries as curEntry>
    
        <#assign renderer = curEntry.getAssetRenderer() />
        <#assign thumbnailPath = renderer.getThumbnailPath(renderRequest) />

        <#assign classe = renderer.getClassName() />
         
         <#if classe == "com.liferay.journal.model.JournalArticle">
                    <#assign journalArticle = renderer.getArticle() />
                    <#assign document = saxReaderUtil.read(journalArticle.getContent()) />
                    <#assign rootElement = document.getRootElement() />
                    <#assign xPathSelector = saxReaderUtil.createXPath("dynamic-element[@name='imagem']") />
                    <#assign image = xPathSelector.selectSingleNode(rootElement).getStringValue() />
                    
                    <#assign xPathSelector2 = saxReaderUtil.createXPath("dynamic-element[@name='conteudo']") />
                    <#assign conteudo = xPathSelector2.selectSingleNode(rootElement).getStringValue() />
         
         
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<p> ${curEntry.getTitle(locale)} ${curEntry.getDescription(locale)} <img src="${thumbnailPath}"> </p>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<p><img src="${image}"></p>
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<p>${conteudo}</p><p>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;</p><p><a href="/web/guest/mais-noticias/-/asset_publisher/oJL9Y5ehvOlQ/content/${renderer.getUrlTitle()}">
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;Link
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </a></p>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <!--#if-->
&nbsp;&nbsp; &nbsp; <!--#list-->

<!--#if-->

Rubén Quinteros, modified 6 Years ago. New Member Post: 1 Join Date: 4/6/18 Recent Posts

Hi,

I had the same problem. This code worked in Liferay 7.1 .
Basicly you need to build the path with the JSON, for this need use the JsonFactoryUtil to convert the String result to JSON and you can access to all atributtes.

 

<#if entries?has_content>
    <#list entries as curEntry>
    
        <#assign renderer = entry.getAssetRenderer()>
        <#assign journalArticle = renderer.getArticle()>
        <#assign document = saxReaderUtil.read(journalArticle.getContent()) >
        <#assign rootElement = document.getRootElement()>
        <#assign xPathSelector = saxReaderUtil.createXPath("dynamic-element[@name='NAME_VAR']")>
        <#assign imgNews = xPathSelector.selectSingleNode(rootElement).getStringValue()>
        <#assign imgJson = jsonFactoryUtil.createJSONObject(imgNews)>
        <#assign url = "/documents/"+ imgJson.groupId +"/" +imgJson.fileEntryId +"/"+ imgJson.name +"/"+imgJson.uuid >
         
    <img src="${url}">
    </#list>
</#if>

 

Regards.

PS: You own me one beer.

Yannik Horndt, modified 6 Years ago. New Member Posts: 9 Join Date: 1/31/19 Recent Posts
Thanks Rubén, this worked for me.
I'm using 7.2 GA1 CE.
I had to edit one field though. 
The imgJson value was:
{
"groupId": "14503",
"alt": "",
"name": "673323",
"title": "673323",
"type": "journal",
"uuid": "15a970d0-1a0e-f067-2910-f513466da6fa",
"resourcePrimKey": "172374"
}

So instead of imgJson.fileEntryId, I had to put resourcePrimKey.

Thanks again!

Sincerely,
Yannik
Arnau Montfort, modified 5 Years ago. New Member Posts: 2 Join Date: 2/24/20 Recent Posts
Sill working in 7.2 
Thanks for that !
emoticon