legacy-knowledge-base
公開されました Jun. 30, 2025

カスタムコードからアセットカテゴリーに関連付けられた画像を取得するには?

written-by

Jose L. Bango

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

learn-legacy-article-disclaimer-text

問題

  • カテゴリーを編集する際、画像を設定することができる。
  • しかし、カスタムコードから画像を取得しようとすると、それができない。 AssetCategoryオブジェクトには関連するメソッドはありません。

環境

  • Liferay DXP 7.4

解像度

  • これらの画像は、アセットカテゴリーが属するアセットフレームワークに関連する他のモジュールの代わりに、Liferay Commerceのモジュールによって処理されます。
  • そのため、他のカテゴリーのフィールドにアクセスするほど簡単ではない。
  • 画像を取得するには、Commerce APIを使用する必要があります。 例えばCPAttachmentFileEntryServiceUtil.getCPAttachmentFileEntries()を呼び出す。. これにより、CPAttachmentFileEntry オブジェクトに対して fetchFileEntry()を呼び 出して、関連する FileEntryを取得することができます
  • サンプルスニペットを以下に示します(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)

    }

追加情報

  • 関連する機能要望もある:
    • LPD-924タクソノミー/カテゴリを拡張し、DXP (API Headless) 全体でフレンドリーな URL と画像を使用する。
    • LPD-152サイト管理者として、Liferay Commerceモジュールに依存せずにカテゴリの画像を表示したい
did-this-article-resolve-your-issue

legacy-knowledge-base