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: Getting web content image in Liferay 7.2.1 DXP
Hello,
I have an issue after migrating part of my project to Liferay 7.2.1 DXP from 7.0.6. Basically we used to get the web content image path using following code (more or less)
ocument myDocument = SAXReaderUtil.read(article.getContentByLocale(locale.toString()));
Node imageField = NodeContent.getSingleNode(document, "Image");
NodeContent.getStringContentValue(imageField)
And it worked fine but unfortunately it doesn't in 7.2.1 DXP. After trying to figure out what's going on I found that the XML looks different for images in web contents in Liferay 7 and Liferay 7.2.
Basically in Liferay 7 I have something like:
I guess it might have changed because of the adaptive images but anyway I'd like to know how can I get the image source from the code? I guess I could parse the JSON and fetch the image based on the informations there but is there any better way? Parsing JSON String to get values for getting image path seems a little bit complicated comparing to an old way
Best regards
I have an issue after migrating part of my project to Liferay 7.2.1 DXP from 7.0.6. Basically we used to get the web content image path using following code (more or less)

Node imageField = NodeContent.getSingleNode(document, "Image");
NodeContent.getStringContentValue(imageField)
And it worked fine but unfortunately it doesn't in 7.2.1 DXP. After trying to figure out what's going on I found that the XML looks different for images in web contents in Liferay 7 and Liferay 7.2.
Basically in Liferay 7 I have something like:
<dynamic-content language-id="en_US" alt="" name="XXX.png" title="XXX.png" type="document" [url=http://fileEntryId="12345"><![CDATA[/documents/111/222/XXX.png/QQQQQQ-WWWW-EEEE-CCCC-DDDDDDD?t=1588696601862]]></dynamic-content>And in Liferay 7.2.1 I have:
<dynamic-content [url=http://language-id="en_US"><![CDATA[{"groupId":"126435","alt":"","name":"XXX.png","title":"XXX.png","type":"journal","uuid":"QQQQ-WWW-EEE-CCC-ec48068bbf39","fileEntryId":"716022","resourcePrimKey":"716460"}]]></dynamic-content>So before the image source was there and that's why my code worked. Now instead of the source I have JSON with informations about the image.
I guess it might have changed because of the adaptive images but anyway I'd like to know how can I get the image source from the code? I guess I could parse the JSON and fetch the image based on the informations there but is there any better way? Parsing JSON String to get values for getting image path seems a little bit complicated comparing to an old way
Best regards
Krzysztof Gołębiowski, modified 5 Years ago.
Liferay Master
Posts: 549
Join Date: 6/25/11
Recent Posts
Hi Rafał,First of all, it's good to see a new Liferay face from Poland 
They changed the way of storing document data in Web Contents at some point, I think now it's actually better because it gives you more data about stored file right in the template context. I retrieved the image from Web Content in one of the FTL templates in the following way:
It is easily translatable to java, as it does not use any freemarker-specific features, only regular Java stuff. All the URL generation magic happens in getDownloadURL method.
KG

They changed the way of storing document data in Web Contents at some point, I think now it's actually better because it gives you more data about stored file right in the template context. I retrieved the image from Web Content in one of the FTL templates in the following way:
<#assign JSONFactoryUtil = staticUtil["com.liferay.portal.kernel.json.JSONFactoryUtil"] />
<#assign DLAppLocalServiceUtil = staticUtil["com.liferay.document.library.kernel.service.DLAppLocalServiceUtil"]>
<#assign DLUtil = staticUtil["com.liferay.document.library.kernel.util.DLUtil"] />
...
<#assign jsonDlFileEntryAsString = docXml.valueOf("//dynamic-element[@name='library_document']/dynamic-content/text()") />
<#assign jsonDlFileEntry = JSONFactoryUtil.createJSONObject(jsonDlFileEntryAsString) />
<#assign fileEntry = DLAppLocalServiceUtil.getFileEntryByUuidAndGroupId(
jsonDlFileEntry.getString("uuid"),
jsonDlFileEntry.getString("groupId")?number) >
<#assign viewURL = DLUtil.getDownloadURL(fileEntry, fileEntry.getLatestFileVersion(true), themeDisplay, "") />
It is easily translatable to java, as it does not use any freemarker-specific features, only regular Java stuff. All the URL generation magic happens in getDownloadURL method.
KG
Hello,
haha yes, Liferay is not that popular in Poland but I can see it gets some popularity recently (I myself use it for almost four years now).
Thank you for the answer. I just found a blog article there https://liferay.dev/blogs/-/blogs/oh-no-my-urls-disappeared-and-how-to-get-them-back- (even with your comments inside with which I totally agree!) and just came back to this thread to post this as an answer
Thank you anyway
haha yes, Liferay is not that popular in Poland but I can see it gets some popularity recently (I myself use it for almost four years now).
Thank you for the answer. I just found a blog article there https://liferay.dev/blogs/-/blogs/oh-no-my-urls-disappeared-and-how-to-get-them-back- (even with your comments inside with which I totally agree!) and just came back to this thread to post this as an answer
Thank you anyway
Krzysztof Gołębiowski, modified 5 Years ago.
Liferay Master
Posts: 549
Join Date: 6/25/11
Recent Posts
Ah, I completely forgot about that article. Then it's much better to follow Pavel's advice as he's a Liferay employee and he actually worked on that matter.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™