Tuesday 30 July 2013

Using liferay-ui:discussion tag for your custom entity for Comments functionality

                  Its usual in many scenario's that we provide facility for user/viewer's comment on any article/asset or any other entity like news. Liferay provides it for many of it's asset like Articles, Blogs etc.

                Now what if you want this functionality of commenting and subscribing for your custom entity.?? Its quit easy with liferay's tag liferay-ui:discussion.

This tag is responsoble for showing comments, adding new comments and comments subscription functionality.

This will render as shown below.













Now lets see how to write code for that.

Below is jsp code we have to write.

 <liferay-ui:discussion redirect="<%= themeDisplay.getURLCurrent() %>"  
  classPK="CLASSPK" userId="USERID" className="ENTITY CLASS NAME"   
  subject="" formAction="<%=discussionURL %>" formName="fm">  
 </liferay-ui:discussion>  

Here:
CLASSPK is  primary key of your custom entity or if you are using it with Liferay's asset  then classPK field of asset.

USERID is current logged in user's userId
ENTITY CLASS NAME is class name of entity for example User.class.getName() or customEntity.class.getName()

By this, comments for entity specified by classPK and className would be shown . Make sure you do not write liferay-ui:discussion  tag inside form tag.

In controller of your portlet, you have to write one method for handling actionURL which we have given in liferay-ui:discussion tag.

This will call processAction method of EditDiscussionAction which will internally handle storing comments for your entity.

 @ProcessAction(name = "addDiscussion")  
   public void addDiscussion(ActionRequest actionRequest,ActionResponse actionResponse){  
     try {  
       PortletActionInvoker.processAction("com.liferay.portlet.messageboards.action.EditDiscussionAction", null, actionRequest, actionResponse);  
     } catch (Exception e) {  
       // TODO Auto-generated catch block  
     }  
   }  

Done!!!

Thus, we can achieve Comments functionality for any custom entity in custom portlet.

Happy Learning!!
Hope this helps.
Regards,

3 comments:

  1. Hello, everything is working great except the Edit and Delete buttons are always available for everyone. Any idea what step I'm missing.

    ReplyDelete
  2. Can we apply workflow in our custom asset..??

    ReplyDelete