Before going to the topic lets have a look in Liferay Entity. If you have used Liferay Service Builder then you are aware of Liferay Entity. An entity is simply a Java Class which is auto generated by Service Builder. An Entity represents model data and persisted in database.
Liferay by default it has many entities. Some of them are User,Page,Role and many more.
Liferay provides UI and API to allow for the creation of customized fields associated with any entity generated by Service Builder. These fields provide extensibility without the need for extending the Liferay DB schema.
Lets explore how to add custom fields through UI. You need to login to your portal as Admin. Go to your control panel. Click Custom Fields as shown in red color in below picture.
Liferay Control Panel |
Custom Fields |
Now you can add your custom fields for any entity in the list. Consider you want to add custom fields for User entity. Click edit for the entity User.
Follow the below screen shot
Adding Custom Field |
After adding the custom field we can view, update from the Liferay UI as well as we can use in our custom portlet.
View and Update Custom Field By Code:
In your jsp add below code
<%@page import="com.liferay.portal.model.User"%>
<%
User currentUser = themeDisplay.getUser();
%>
View / Edit Custom Field<br/>
<portlet:actionURL var="updateCustomField" name="updateUserCustomeField"/>
<aui:form action="<%= updateCustomField %>" method="post" name="fm">
<liferay-ui:custom-attribute-list
className="<%= User.class.getName() %>"
classPK="<%= currentUser != null ? currentUser.getUserId() : 0 %>"
editable="<%= true %>" label="true"/>
<aui:button type="submit" value="update"/>
</aui:form>
In your portlet class file add below code
@ProcessAction(name="updateUserCustomeField")
public void updateUserCustomeField(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException,
PortletException, PortalException, SystemException {
ServiceContext serviceContext = ServiceContextFactory.getInstance(User.class.getName(), actionRequest);
User user = PortalUtil.getUser(actionRequest);
user.setExpandoBridgeAttributes(serviceContext);
UserLocalServiceUtil.updateUser(user);
}
Show all the custom fields for a given Entity(in JSP):
<liferay-ui:custom-attribute-list
className="<%= User.class.getName() %>"
classPK="<%= currentUser != null ? currentUser.getUserId() : 0 %>"
editable="<%= false %>" label="true"/>
Show specific Custom Field Value (in JSP) :
<liferay-ui:custom-attribute
className="<%= User.class.getName() %>"
classPK="<%= currentUser != null ? currentUser.getUserId() : 0 %>"
editable="<%= false %>"
name="secondary-email" label="true"/>
Add Custom Field dynamically by Java Code:
@ProcessAction(name="addUserCustomeField")
public void addUserCustomeField(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException,
PortletException, PortalException, SystemException {
String customField = ParamUtil.getString(actionRequest, "customField","");
User user = PortalUtil.getUser(actionRequest);
user.getExpandoBridge().addAttribute(customField);
}
To call this method add below code in your JSP page
<portlet:actionURL var="addCustomField" name="addUserCustomeField"/>
<aui:form action="<%= addCustomField %>" method="post" name="fm">
<aui:input name="customField" label="Custom Field"/>
<aui:button type="submit" value="Add Custom Field"/>
</aui:form>
Download Code
Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog.
ReplyDeleteIt is very nicely explained. Your article adds best knowledge to our Java Online Training from India.
or learn thru Java Online Training from India Students.