掲示板

get_articles by vocabulary, categories, tags in VM

thumbnail
14年前 に Norbert Bede によって更新されました。

get_articles by vocabulary, categories, tags in VM

Junior Member 投稿: 38 参加年月日: 09/04/19 最新の投稿
hi,

I want to publish articles in content display portlet by velocity engine (structures, templates). I want to to get from the database only those articles, which equal for a specific tag or category - or more of them.

LIke this (fake example)
#set ($document = $saxReaderUtil.readURL("http://@portal_url@/c/journal/get_articles?groupId=@group_id@&type=marketing&delta=1&approved=true&expired=false&orderBy=display-date&VOCABULARY=CEO"))

thanks
norbert
14年前 に Tomasz Grysztar によって更新されました。

RE: get_articles by vocabulary, categories, tags in VM

Junior Member 投稿: 29 参加年月日: 09/05/06 最新の投稿
I had the same problem and I solved it by writing my own servlet (installed as a "web" plugin) that works in a similar way to "get_articles" action, but allows searching by tags (and few other things that I needed).

And if writing a servlet is not an option for you, you may try to work it with out $serviceLocator.
I wrote a small example of CMS template that would do it, this one searches for articles tagged with "xxx" tag:

#set ($tagsService = $serviceLocator.findService("com.liferay.portlet.tags.service.TagsAssetLocalService"))
#set ($tagsEntryService = $serviceLocator.findService("com.liferay.portlet.tags.service.TagsEntryLocalService"))
#set ($journalArticleResourceService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleResourceLocalService"))

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

#set ($tags = ["xxx"])
#set ($entryIds = $tagsEntryService.getEntryIds($longGroupId,$tags.toArray($propsUtil.getArray(""))) )

#set ($notTags = [])
#set ($notEntryIds = $tagsEntryService.getEntryIds($longGroupId,$notTags.toArray($propsUtil.getArray(""))) )

#set ($classNameIds = [$portalUtil.getClassNameId("com.liferay.portlet.journal.model.JournalArticle")])
#set ($classNameIdsArray =$arrayUtil.toArray($classNameIds.toArray($arrayUtil.toArray($entryIds))) )

#set ($assets = $tagsService.getAssets($longGroupId,$classNameIdsArray,$entryIds,$notEntryIds,true,false,0,5) )

#foreach ($asset in $assets)
 $journalArticleResourceService.getArticleResource($asset.getClassPK())
#end


Look at this thread for more info on serviceLocator: http://www.liferay.com/web/guest/community/forums/-/message_boards/message/979976
As it is explained there, if you have serviceLocator disabled for journal templates in your portal, there still is a workaround, you can replace the initial three lines of the code above with:
#set ($tagsService = $portal.getClass().forName("com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil").getMethod("getService", null).invoke(null, null))
#set ($tagsEntryService = $portal.getClass().forName("com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil").getMethod("getService", null).invoke(null, null))
#set ($journalArticleResourceService = $portal.getClass().forName("com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil").getMethod("getService",null).invoke(null, null))


This template just gets the article resources, if you require to get some more information about each article, like title, you would need to use the JournalArticleLocalService aswell to retrieve the article object for given articleId.

PS. There are also some dirty hacks in my code, like using $propsUtil only to get some variable of String[] type - I don't know of any better method to do this.