Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: How to get Web Content structure field (Image) from ADT
hello, I have created web content structure with Image field and name it as News_Image and want to get that in application display template (ADT) , Can you please let me how to do that in Liferay 7.2
Hi ,
You may not be able to get structure field directly in Asset publisher Template , it will show the template corresponding to Web content , may be you need to write a custom web service and fetch those image url form template
You may not be able to get structure field directly in Asset publisher Template , it will show the template corresponding to Web content , may be you need to write a custom web service and fetch those image url form template
may be there is a way to read web content structure fields in ADT like below
<#if entries?has_content>
<#list entries as curEntry>
<#assign
renderer = curEntry.getAssetRenderer()
className = renderer.getClassName()
viewURL0 = assetPublisherHelper.getAssetViewURL(renderRequest, renderResponse, curEntry)
viewURL = renderer.getURLViewInContext(renderRequest, renderResponse, viewURL0) />
<#if className == "com.liferay.journal.model.JournalArticle">
<#assign
journalArticle = renderer.getArticle()
document = saxReaderUtil.read(journalArticle.getContent())
rootElement = document.getRootElement()
xPathSelector = saxReaderUtil.createXPath("dynamic-element[@name='subtitle']")
subtitle = xPathSelector.selectSingleNode(rootElement).getStringValue()
link = renderer.getURLViewInContext(renderRequest, renderResponse, '')
>
</#if>
${subtitle}
</#list>
</#if>
<#if entries?has_content>
<#list entries as curEntry>
<#assign
renderer = curEntry.getAssetRenderer()
className = renderer.getClassName()
viewURL0 = assetPublisherHelper.getAssetViewURL(renderRequest, renderResponse, curEntry)
viewURL = renderer.getURLViewInContext(renderRequest, renderResponse, viewURL0) />
<#if className == "com.liferay.journal.model.JournalArticle">
<#assign
journalArticle = renderer.getArticle()
document = saxReaderUtil.read(journalArticle.getContent())
rootElement = document.getRootElement()
xPathSelector = saxReaderUtil.createXPath("dynamic-element[@name='subtitle']")
subtitle = xPathSelector.selectSingleNode(rootElement).getStringValue()
link = renderer.getURLViewInContext(renderRequest, renderResponse, '')
>
</#if>
${subtitle}
</#list>
</#if>
You need to know that the web content data is saved in XML format. Here is an example how to iterate over the assets in an ADT, check if the displayed assets are Web Contents and to read the image and display it.
<#list entries as entry>
<#assign
assetRenderer = entry.getAssetRenderer()
/>
<#if assetRenderer.getClassName()=="com.liferay.journal.model.JournalArticle">
<#assign
docXml = saxReaderUtil.read(assetRenderer.getArticle().getContent())
image = docXml.valueOf("//dynamic-element[@name='News_Image']/dynamic-content/text()")
/>
<img src="${image}">
<!--#if-->
<!--#list-->
how and where can I see that XML format of web content ? and give please provide more ADT examples
Hello, this did not work src is showing/printing as {" classpk":"780056","groupid":"763443","name":"screen="" shot="" 2020-07-01="" at="" 10.41.21="" am.png","alt":"","title":"screen="" am.png","type":"journal","uuid":"2eb5d96d-5010-183b-64be-d494945f3e50","fileentryid":"780062","resourceprimkey":"768337"}, that is not giving image pagth.
Dominik Marks:
Hello, this did not work <img src is showing/printing as {" classpk":"780056","groupid":"763443","name":"screen="" shot="" 2020-07-01="" at="" 10.41.21="" am.png","alt":"","title":"screen="" am.png","type":"journal","uuid":"2eb5d96d-5010-183b-64be-d494945f3e50","fileentryid":"780062","resourceprimkey":"768337"}, that is not giving image pagth.
You need to know that the web content data is saved in XML format. Here is an example how to iterate over the assets in an ADT, check if the displayed assets are Web Contents and to read the image and display it. <#list entries as entry> <#assign assetRenderer = entry.getAssetRenderer() /> <#if assetRenderer.getClassName()=="com.liferay.journal.model.JournalArticle"> <#assign docXml = saxReaderUtil.read(assetRenderer.getArticle().getContent()) image = docXml.valueOf("//dynamic-element[@name='News_Image']/dynamic-content/text()") /> <img src="${image}"> <!--#if--> <!--#list-->
Just take a look at 'view source' in webcontent (the 3 dots at the right top of the screen)
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™