Friday 25 May 2012

Get catgories at Liferay's CMS template

Hello,
    Liferay's CMS structure/template is really good mechanism for customising frontend views. This post I'll show how you can retrieve article's categories into velocity template.


    Before start check this Wiki link. It's describes basic usage of structure/template mecahnism.
    You can retrieve set of article's fields such as reserved-article-id.data, $reserved-article-title.data, etc (full list), but to get article's categories you need to code little bit of velocity.
 
     First, you need to remove serviceLocator from property
          journal.template.freemarker.restricted.variables=
    portal-ext.properties in order to be able to get lifery's services.

    Second, add this velocity code to your CMS template:
  #set($journalLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService"))
  #set($scopeId = $request.theme-display.scope-group-id)

  ## get article's primaryKey
  ## NOTE: be careful about input parameters types should match
  #set($articlePrimKey = $journalLocalService.getArticle($getterUtil.getLong($scopeId), $reserved-article-id.data).resourcePrimKey) 

  #set($catLocalService = $serviceLocator.findService("com.liferay.portlet.asset.service.AssetCategoryLocalService"))

  ## get article's category names by primaryKey
  #set($articleCatNames = $catLocalService.getCategoryNames("com.liferay.portlet.journal.model.JournalArticle", $getterUtil.getLong($articlePrimKey)))

  $articleCatNames

It will display to you all article's categories.
More detailed info related serviceLocator you can find here.

BR,
Paul

1 comment:

  1. you'r article is very helpful
    but i'm facing one problem that it is showing nothing except $articleCatNames in the web content.
    I've created one vocabulary named Articles under it I've created 7 category and wanted to display it through the template.
    thanx in advance.

    ReplyDelete