Forums

Home » Liferay Portal » English » 2. Using Liferay » General

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Johan Svensson
Get the articles categories, using Velocity and article templates?
October 6, 2011 5:08 AM
Answer

Johan Svensson

Rank: New Member

Posts: 1

Join Date: October 6, 2011

Recent Posts

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??
Sandeep Nair
RE: Get the articles categories, using Velocity and article templates?
October 6, 2011 9:38 PM
Answer

Sandeep Nair

Rank: Liferay Legend

Posts: 1692

Join Date: November 5, 2008

Recent Posts

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
Ravi Kumar Gupta
RE: Get the articles categories, using Velocity and article templates?
October 7, 2011 1:08 AM
Answer

Ravi Kumar Gupta

Rank: Liferay Legend

Posts: 1181

Join Date: June 23, 2009

Recent Posts

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
RE: Get the articles categories, using Velocity and article templates?
May 25, 2012 5:23 AM
Answer

Paul Butenko

Rank: Junior Member

Posts: 38

Join Date: July 1, 2010

Recent Posts

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.
Randy Parsons
RE: Get the articles categories, using Velocity and article templates?
October 24, 2012 6:46 AM
Answer

Randy Parsons

Rank: New Member

Posts: 23

Join Date: January 4, 2011

Recent Posts

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.
parikshit sharma
RE: Get the articles categories, using Velocity and article templates?
October 25, 2012 5:23 AM
Answer

parikshit sharma

Rank: Junior Member

Posts: 47

Join Date: October 18, 2012

Recent Posts

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
1
2#foreach($catName in $articleCatNames)
3$catName
4#end
Randy Parsons
RE: Get the articles categories, using Velocity and article templates?
October 26, 2012 5:49 AM
Answer

Randy Parsons

Rank: New Member

Posts: 23

Join Date: January 4, 2011

Recent Posts

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.

 1#set($journalLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService"))
 2#set($scopeId = $request.theme-display.scope-group-id)
 3
 4## get article's primaryKey
 5## NOTE: be careful about input parameters types should match
 6#set($articlePrimKey = $journalLocalService.getArticle($getterUtil.getLong($scopeId), $reserved-article-id.data).resourcePrimKey)
 7
 8#set($catLocalService = $serviceLocator.findService("com.liferay.portlet.asset.service.AssetCategoryLocalService"))
 9
10## get article's category names by primaryKey
11#set($articleCatNames = $catLocalService.getCategoryNames("com.liferay.portlet.journal.model.JournalArticle", $getterUtil.getLong($articlePrimKey)))
12
13<div>Article Categories:
14        <span>
15            #foreach ($catName in $articleCatNames)
16                $catName
17            #end
18        </span>
19    </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.
Gaurav Jain
RE: Get the articles categories, using Velocity and article templates?
October 26, 2012 5:59 AM
Answer

Gaurav Jain

Rank: Regular Member

Posts: 141

Join Date: January 31, 2011

Recent Posts

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
RE: Get the articles categories, using Velocity and article templates?
April 26, 2013 3:58 AM
Answer

Fernando Tapia

Rank: New Member

Posts: 3

Join Date: June 6, 2012

Recent Posts

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:

 1
 2#set ($tagsService = $portal.getClass().forName("com.liferay.portlet.asset.service.AssetTagServiceUtil").getMethod("getService", null).invoke(null, null))
 3#set ($journalArticleResourceService = $portal.getClass().forName("com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil").getMethod("getService", null).invoke(null, null))
 4#set ($catLocalService = $portal.getClass().forName("com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil").getMethod("getService", null).invoke(null, null))
 5
 6#set ($longGroupId = $getterUtil.getLong($groupId))
 7
 8#set ($articleResourcePK = $journalArticleResourceService.getArticleResourcePrimKey($longGroupId, $reserved-article-id.data))
 9
10## get article's category names by primaryKey
11#set($articleCatNames = $catLocalService.getCategoryNames("com.liferay.portlet.journal.model.JournalArticle", $articleResourcePK))
12
13<div>Article Categories:  $articleResourcePK
14        <span>
15            #foreach ($catName in $articleCatNames)
16                $catName
17            #end
18        </span>
19    </div>
shima shima
RE: Get the articles categories, using Velocity and article templates?
August 26, 2014 12:07 AM
Answer

shima shima

Rank: Junior Member

Posts: 39

Join Date: April 13, 2014

Recent Posts

hi
I convert your code to freemarker but it dosent underestand portal.getlass()