How to retrieve images associated with Asset Categories from custom code?
How To articles are not official guidelines or officially supported documentation. They are community-contributed content and may not always reflect the latest updates to Liferay DXP. We welcome your feedback to improve How To articles!
While we make every effort to ensure this Knowledge Base is accurate, it may not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with any feedback or concerns.
Legacy Article
You are viewing an article from our legacy "FastTrack"
publication program, made available for informational purposes. Articles
in this program were published without a requirement for independent
editing or verification and are provided"as is" without
guarantee.
Before using any information from this article, independently verify its
suitability for your situation and project.
Issue
- When editing a category, it is possible to set images.
- However, when trying to get those images from my custom code, I am unable to do that. There are no related methods in
AssetCategory
objects.
Resolution
- These images are handled by Liferay Commerce's modules instead of other modules related to the Asset Framework where Asset Categories belong to.
- That's why it is not as easy as access other categories' fields.
-
In order to retrieve the images, you would have to use the Commerce API. For example, calling
CPAttachmentFileEntryServiceUtil.getCPAttachmentFileEntries()
. With this, you can call fetchFileEntry()
on the CPAttachmentFileEntry
objects to get the associated FileEntry
.
- Find below a sample snippet (tested in 7.4 u 76):
import com.liferay.account.constants.AccountConstants
import com.liferay.asset.kernel.model.AssetCategory
import com.liferay.commerce.media.CommerceMediaResolverUtil
import com.liferay.commerce.product.model.CPAttachmentFileEntryConstants
import com.liferay.commerce.product.service.CPAttachmentFileEntryServiceUtil
import com.liferay.document.library.util.DLURLHelperUtil
import com.liferay.portal.kernel.theme.ThemeDisplay
import com.liferay.portal.kernel.util.PortalUtil
import com.liferay.portal.kernel.workflow.WorkflowConstants
def assetCategoryId = 000
def themeDisplay = (ThemeDisplay)actionRequest.getAttribute(com.liferay.portal.kernel.util.WebKeys.THEME_DISPLAY)
def cpAttachmentFileEntries = CPAttachmentFileEntryServiceUtil.getCPAttachmentFileEntries(
PortalUtil.getClassNameId(AssetCategory.class), assetCategoryId,
CPAttachmentFileEntryConstants.TYPE_IMAGE, WorkflowConstants.STATUS_ANY, -1, -1)
for (def cpAttachmentFileEntry in cpAttachmentFileEntries) {
def fileEntry = cpAttachmentFileEntry.fetchFileEntry()
out.println("fileEntry " + fileEntry.getFileName())
def thumbnailSrc = DLURLHelperUtil.getThumbnailSrc(fileEntry, themeDisplay)
out.println("thumbnailSrc " + thumbnailSrc)
// You can also leverage CommerceMediaResolverUtil
def url = CommerceMediaResolverUtil.getURL(AccountConstants.ACCOUNT_ENTRY_ID_ANY,
cpAttachmentFileEntry.getCPAttachmentFileEntryId())
out.println("url " + url)
}
Did this article resolve your issue ?