留言板

Get the articles categories, using Velocity and article templates?

Johan Svensson,修改在12 年前。

Get the articles categories, using Velocity and article templates?

New Member 帖子: 2 加入日期: 11-10-6 最近的帖子
Hi!

I have the following issue. I need to get an articles categories, that has been set when the article was created.
I have already obtained the articles unique ID using $reserved-article-id.data but I cant find the Velocity code that gets the articles categories.

I want the article template to work like this:
1. I get the articles different elements, from the structure, by using "xxx.getData() etc.
3. (ISSUE) I want the article template to not only get the ID of the article, but also the categories.
2. By using JavaScript I hide and show different textfields and images depending on the articles categorization.

So, any suggestions??
thumbnail
Sandeep Nair,修改在12 年前。

RE: Get the articles categories, using Velocity and article templates?

Liferay Legend 帖子: 1744 加入日期: 08-11-6 最近的帖子
Hi,

You can user serviceLocator's findService method to find appropriate service(in your case i believe AssetCategory) and then use its API method to get the categories that you are interested in.

http://www.liferay.com/community/wiki/-/wiki/Main/Access+to+Liferay+services+in+Velocity

Regards,
Sandeep
thumbnail
Ravi Kumar Gupta,修改在12 年前。

RE: Get the articles categories, using Velocity and article templates?

Liferay Legend 帖子: 1302 加入日期: 09-6-24 最近的帖子
refer this post for java code to get the categories.. use that after locating the service as Sandeep suggested.
http://www.liferay.com/community/forums/-/message_boards/message/10984882
Paul Butenko,修改在11 年前。

RE: Get the articles categories, using Velocity and article templates?

Junior Member 帖子: 38 加入日期: 10-7-2 最近的帖子
Hi,
Here is the code wich can help:

#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

Also you can this post.
thumbnail
Randy Parsons,修改在11 年前。

RE: Get the articles categories, using Velocity and article templates?

Junior Member 帖子: 33 加入日期: 11-1-4 最近的帖子
Hi Paul,
This is exactly what I was looking for but I seem to be getting an error. When I attempt to print out the Categories in my Template, I get the following when using the above code. I reviewed the API for the AssetCategoryLocalService and everything looks right. I have confirmed that I do get values for $articlePrimKey and $scopeId.

Also, every time I refresh the page, I get a different value returned.

Article Categories: [Ljava.lang.String;@4b17e6c4

I'm using Liferay 6.1.2. Any ideas.
John Carter,修改在11 年前。

RE: Get the articles categories, using Velocity and article templates?

Junior Member 帖子: 53 加入日期: 12-10-18 最近的帖子
Randy Parsons:
Hi Paul,
This is exactly what I was looking for but I seem to be getting an error. When I attempt to print out the Categories in my Template, I get the following when using the above code. I reviewed the API for the AssetCategoryLocalService and everything looks right. I have confirmed that I do get values for $articlePrimKey and $scopeId.

Also, every time I refresh the page, I get a different value returned.

Article Categories: [Ljava.lang.String;@4b17e6c4

I'm using Liferay 6.1.2. Any ideas.



Randy,
If problem still exsits then check the below code to get categories name ,as you are getting the string array of CategoryName

#foreach($catName in $articleCatNames)
$catName
#end
thumbnail
Randy Parsons,修改在11 年前。

RE: Get the articles categories, using Velocity and article templates?

Junior Member 帖子: 33 加入日期: 11-1-4 最近的帖子
John,

Thank you for the quick reply. This solved the 'displaying' of the categories.

For anyone else looking for the solution, here is the complete code.

#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)))

<div>Article Categories: 
        <span>
            #foreach ($catName in $articleCatNames)
                $catName 
            #end
        </span>
    </div>



Curious Question:
What is the difference between $articlePrimKey used above and the value returned with the velocity reserved $reserved-article-id.data? Both return different values.
thumbnail
Gaurav Jain,修改在11 年前。

RE: Get the articles categories, using Velocity and article templates?

Regular Member 帖子: 145 加入日期: 11-1-31 最近的帖子
Randy Parsons:


Curious Question:
What is the difference between $articlePrimKey used above and the value returned with the velocity reserved $reserved-article-id.data? Both return different values.



$articlePrimKey is article's resourcePrimaryKey while $reserved-article-id is article's articleId.
Liferay maintains these two as different attributes on article object. The method you are using in AssetCategoryLocalService to getCategoryNames() requires article's resourcePrimaryKey
Fernando Tapia,修改在10 年前。

RE: Get the articles categories, using Velocity and article templates?

New Member 帖子: 3 加入日期: 12-6-6 最近的帖子
Hi ,
I was working in the version 6.1 and it did not work, I search a bit and i got it working like this:


#set ($tagsService = $portal.getClass().forName("com.liferay.portlet.asset.service.AssetTagServiceUtil").getMethod("getService", null).invoke(null, null))
#set ($journalArticleResourceService = $portal.getClass().forName("com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil").getMethod("getService", null).invoke(null, null))
#set ($catLocalService = $portal.getClass().forName("com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil").getMethod("getService", null).invoke(null, null))

#set ($longGroupId = $getterUtil.getLong($groupId))

#set ($articleResourcePK = $journalArticleResourceService.getArticleResourcePrimKey($longGroupId, $reserved-article-id.data))

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

<div>Article Categories:  $articleResourcePK
        <span>
            #foreach ($catName in $articleCatNames)
                $catName
            #end
        </span>
    </div>
shima shima,修改在9 年前。

RE: Get the articles categories, using Velocity and article templates?

Junior Member 帖子: 46 加入日期: 14-4-14 最近的帖子
hi
I convert your code to freemarker but it dosent underestand portal.getlass()