简体   繁体   中英

Liferay Freemarker - How to get tags in a Content Template

I have content with a structure and a template and I want to access the tags of the content to show it in the template. In a previous Application Display Template of an Asset Publisher, i got the tags with the ServiceLocator like this:

<#list entries as entry>
  <#assign assetEntryLocalService = serviceLocator.findService("com.liferay.portlet.asset.service.AssetEntryLocalService") />
  <#assign assetTagLocalService = serviceLocator.findService("com.liferay.portlet.asset.service.AssetTagLocalService") />
  <#assign assetTags = assetTagLocalService.getEntryTags(entry.getEntryId()) />
  <#list assetTags as tag>
        <#if tag.getName() != "startseite">
             ${tag.getName()}
        </#if>
  </#list>
</#list>

In my Template, I swapped entry with .vars['reserved-article-id'].data but then I get an error:

Method public final java.util.List com.sun.proxy.$Proxy562.getEntryTags(long) throws com.liferay.portal.kernel.exception.SystemException threw an exception when invoked on com.liferay.portlet.asset.service.impl.AssetTagLocalServiceImpl@6bc73e2b

How do you get this to work in a template?

Tags are associated with the assetEntry using it's resourcePrimKey, so you can do this:

<#assign assetEntryLocalService = serviceLocator.findService("com.liferay.portlet.asset.service.AssetEntryLocalService") />
<#assign journalArticleLocalService = serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService") />

<#assign article = journalArticleLocalService.getArticle(getterUtil.getLong(scopeGroupId), .vars['reserved-article-id'].data)>
<#assign asset = assetEntryLocalService.getEntry('com.liferay.portlet.journal.model.JournalArticle', article.resourcePrimKey) >

<#list asset.getTags() as tag>
    <code>${tag.name}</code>
</#list>

As of Liferay 7, you should be able to use the following:

<#list entries as entry>
    <#assign
        entry = entry
    />

    <#list entry.tagNames as tag>
        ${tag}
    </#list>
</#list>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM