Message Boards

How to fetch categories in web content display

Shounak Nandi, modified 3 Years ago.

How to fetch categories in web content display

New Member Posts: 2 Join Date: 5/14/20 Recent Posts
Hi,
We are trying to fetch and display categories in web content display for a content.Can someone guide me how it can be done using FTL in Liferay 7.3 CE.
Regards,Shounak
thumbnail
Mohammed Yasin, modified 3 Years ago.

RE: How to fetch categories in web content display

Liferay Master Posts: 591 Join Date: 8/8/14 Recent Posts
Hi,
You can try  using taglib for showing asset Categories of selected asset  in web content  ftl
<@liferay_ui["asset-categories-summary"]
   className=entry.getClassName()
                    classPK=entry.getClassPK()
/>
Shounak Nandi, modified 3 Years ago.

RE: How to fetch categories in web content display

New Member Posts: 2 Join Date: 5/14/20 Recent Posts
Hi,
This is working in asset publisher and webcontents opened in asset publisher portlet.
How can we achieve this is web content display portet.
Regards,
​​​​​​​Shounak
thumbnail
Mohammed Yasin, modified 3 Years ago.

RE: How to fetch categories in web content display

Liferay Master Posts: 591 Join Date: 8/8/14 Recent Posts
In Webcontent  Display you may need to  get the asset of selected webcontent then fetch the categories, you may refer below code snippet (Liferay 7.1), You can also fetch the list of categories from asset object (asset.getCategories()) and  use that 
<#assign articleId =  .vars['reserved-article-id'].data /> 
<#assign assetEntryLocalService = serviceLocator.findService("com.liferay.asset.kernel.service.AssetEntryLocalService") /> 
<#assign journalArticleLocalService = serviceLocator.findService("com.liferay.journal.service.JournalArticleLocalService") /> 
<#assign article = journalArticleLocalService.getArticle(getterUtil.getLong(scopeGroupId), articleId)> 
<#assign asset = assetEntryLocalService.getEntry('com.liferay.journal.model.JournalArticle', article.resourcePrimKey) > 
​​​​​​​<@liferay_asset["asset-categories-summary"]   className='${asset.getClassName()}' classPK=article.resourcePrimKey /> 


thumbnail
Christoph Rabel, modified 3 Years ago.

RE: How to fetch categories in web content display

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
I fear, you have to use something like this: (works only after allowing the use of serviceLocator in system settings)
    journalArticleResourceLocalService = serviceLocator.findService("com.liferay.journal.service.JournalArticleResourceLocalService")
    assetEntryLocalService = serviceLocator.findService("com.liferay.asset.kernel.service.AssetEntryLocalService")

    journalArticleId = .vars['reserved-article-id'].data
    articleResourcePrimaryKey = journalArticleResourceLocalService.getArticleResourcePrimKey(themeDisplay.getScopeGroupId(), journalArticleId)
    assetEntry = assetEntryLocalService.getEntry("com.liferay.journal.model.JournalArticle", articleResourcePrimaryKey)
    assetCategories = assetEntry.getCategories()

I wished, the whole JournalArticle Object or at least some nice helper objects were available in the template.
Lee Jordan, modified 3 Years ago.

RE: How to fetch categories in web content display

Expert Posts: 449 Join Date: 5/26/15 Recent Posts
I read this and I think why can't I do this ... ${article.categories} ???

Instead I have to do this ...
<#assign articleId =  .vars['reserved-article-id'].data /> 
<#assign assetEntryLocalService = serviceLocator.findService("com.liferay.asset.kernel.service.AssetEntryLocalService") /> 
<#assign journalArticleLocalService = serviceLocator.findService("com.liferay.journal.service.JournalArticleLocalService") /> 
<#assign article = journalArticleLocalService.getArticle(getterUtil.getLong(scopeGroupId), articleId)> 
<#assign asset = assetEntryLocalService.getEntry('com.liferay.journal.model.JournalArticle', article.resourcePrimKey) > <@liferay_asset["asset-categories-summary"]   className='${asset.getClassName()}' classPK=article.resourcePrimKey /> 


WTH?? Time matters, I don't have two hours to spend researching, another 4 hours trying to get something working and then a week goes by and I haven't delivered or finished my template. For personal projects yes fine. But when someone in an enterprise needs something by the end of the day I'm sorry but Liferay truly sucks for delivering solutions quickly.