Aim of this Article:
Understanding the Liferay Custom Field concept.I will explain how to add custom field through Liferay Control Panel.
Use of Custom Field :
Add extra field in database table through UI using Liferay API.
Note: Custom Field or Custom Attributes both are same
Liferay Custom Fields or Liferay Custom Attribute is a feature in Liferay. 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.
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.
Let’s 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.
You will see below list
You will see list of all entities for which you can add custom fields. However you can add your own Entity in this list. I will cover how to add your own entity in another session.
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
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(); %> <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>