Foros de discusión

Get the articles categories, using Velocity and article templates?

Johan Svensson, modificado hace 12 años.

Get the articles categories, using Velocity and article templates?

New Member Mensajes: 2 Fecha de incorporación: 6/10/11 Mensajes recientes
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, modificado hace 12 años.

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

Liferay Legend Mensajes: 1744 Fecha de incorporación: 6/11/08 Mensajes recientes
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, modificado hace 12 años.

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

Liferay Legend Mensajes: 1302 Fecha de incorporación: 24/06/09 Mensajes recientes
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, modificado hace 11 años.

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

Junior Member Mensajes: 38 Fecha de incorporación: 2/07/10 Mensajes recientes
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, modificado hace 11 años.

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

Junior Member Mensajes: 33 Fecha de incorporación: 4/01/11 Mensajes recientes
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, modificado hace 11 años.

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

Junior Member Mensajes: 53 Fecha de incorporación: 18/10/12 Mensajes recientes
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, modificado hace 11 años.

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

Junior Member Mensajes: 33 Fecha de incorporación: 4/01/11 Mensajes recientes
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, modificado hace 11 años.

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

Regular Member Mensajes: 145 Fecha de incorporación: 31/01/11 Mensajes recientes
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, modificado hace 11 años.

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

New Member Mensajes: 3 Fecha de incorporación: 6/06/12 Mensajes recientes
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, modificado hace 9 años.

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

Junior Member Mensajes: 46 Fecha de incorporación: 14/04/14 Mensajes recientes
hi
I convert your code to freemarker but it dosent underestand portal.getlass()