Liferay’s asset framework facilitates discussions on individual pieces of content posted in a plugin. This is great because it lets discuss any posted content. Fortunately, most of the work is already done for you so you don’t have to spend time developing a commenting system from scratch.
In order to implement the comments feature on your custom entity, it must be asset enabled. This tutorial shows you how to add the comment feature for your application’s content. A custom Insults portlet is used as an example: a community discussion definitely helps to bring about insults of the highest quality! The completed Insults portlet code that uses this feature is on Github.
Without any further ado, go ahead and get started enabling comments in your portlet!
You can display the comments component in your portlet’s view or, if you’ve implemented asset rendering, you can display it in the full content view in the Asset Publisher portlet.
As an example, the Insult portlet’s view JSP file
shows an insult entity and this asset feature.
In your entity’s view JSP, you can use
ParamUtil to get the ID of the entity
from the render request. Then you can create an entity object using your
<% long insultId = ParamUtil.getLong(renderRequest, "insultId"); Insult ins = InsultLocalServiceUtil.getInsult(insultId); %>
Next, you need to add the code that handles the discussion. First you’ll create
a collapsible panel for the comments, so the discussion area doesn’t visually
take precedence over the content. This is done with the
liferay-ui:panel tags. Collapsible panels let
your users obscure any potentially long content on a page. Now that this panel
is in place, you need to create a URL for the discussion. This is done with the
portlet:actionURL tag. You also need to get the current URL from the
object, so that your user can return to the JSP after adding a comment. This is
PortalUtil. Last, but certainly not least, is the implementation of
the discussion itself with the
liferay-ui:discussion tag. The following block
of code shows the collapsible panel, URLs, and the discussion:
<liferay-ui:panel-container extended="<%=false%>" id="insultCommentsPanelContainer" persistState="<%=true%>"> <liferay-ui:panel collapsible="<%=true%>" extended="<%=true%>" id="insultCommentsPanel" persistState="<%=true%>" title='<%=LanguageUtil.get(pageContext, "comments")%>'> <portlet:actionURL name="invokeTaglibDiscussion" var="discussionURL" /> <% String currentUrl = PortalUtil.getCurrentURL(request); %> <liferay-ui:discussion className="<%=Insult.class.getName()%>" classPK="<%=ins.getInsultId()%>" formAction="<%=discussionURL%>" formName="fm2" ratingsEnabled="<%=true%>" redirect="<%=currentUrl%>" subject="<%=ins.getInsultString()%>" userId="<%=ins.getUserId()%>" /> </liferay-ui:panel> </liferay-ui:panel-container>
Awesome! Now you have a JSP that lets your users comment on content in your portlet.
If you haven’t already connected your portlet’s view to the JSP for your entity, you can refer here to see how to connect your portlet’s main view JSP to your entity’s view JSP.
Now redeploy your portlet and refresh the page so that the your plugin’s UI reloads. The comments section appear at the bottom of the page.
Great! Now you know how to let users comment on content in your asset enabled portlets.
Before moving on, another thing you might want to do is perform permissions
checks to control who can access the discussion. For example, you can surround
the collapsible panel
c:if tags that only reveal their contents to users that are signed in to
<c:if test="<%=themeDisplay.isSignedIn()%>"> [Collapsible Panel Here] </c:if>
This is just one way of controlling access to the discussion. You
could also perform more specific permissions checks as the Insults
portlet does for the Add Insults and Permissions buttons in its
For more information, see the learning path
Checking for Permissions in the UI.
Asset Enabling Custom Entities