Custom fields are a way to add attributes to many types of assets in the portal.
Its basically help us to add additional fields to existed models or entities.
Adding some more additional attributes to existed entities or models helps us so to meet our requirements.
Liferay provide two way's to allow the creation of Custom Fields.
1. Using Liferay UI
2. Programatically
Lets look these both topic one by one-:
Suppose you have a scenario where its needed to add few fields in your User_ Table
So its simple to add using Liferay UI just you need to follow these steps-:
1. Using Liferay UI-:
Goto-> Control-Panel -> Click on Custom Field
Now you can add your custom fields for any entity in the list. Click on User Entity and a page will be displayed like this-:
Now add these value in respective fields-:
KEY- myContactId
Type- Select Integer
After adding the custom field we can view, update from the Liferay UI as well as we can use in our custom portlet.
Points to remember-:
-> For displaying custom attribute as input fields liferay provide us 2 tags-:
- <liferay:ui-custom-attribute className = "" classPK = "" name ="" />
- This display one attribute field
- <liferay:ui-custom-attribute-list className = "" classPK = "" name ="" />
- This display a list of attributes.
-> className - This is class name of entity in our case its com.liferay.portal.model.User
-> classPK - The primary key of entity instance
-> name - Name of the specific attribute
Display custom field using JSP page
add this code in your jsp page
add this in your portlet class
Its basically help us to add additional fields to existed models or entities.
Adding some more additional attributes to existed entities or models helps us so to meet our requirements.
Liferay provide two way's to allow the creation of Custom Fields.
1. Using Liferay UI
2. Programatically
Lets look these both topic one by one-:
Suppose you have a scenario where its needed to add few fields in your User_ Table
So its simple to add using Liferay UI just you need to follow these steps-:
1. Using Liferay UI-:
A List will be displayed-:
Now add these value in respective fields-:
KEY- myContactId
Type- Select Integer
After adding the custom field we can view, update from the Liferay UI as well as we can use in our custom portlet.
Points to remember-:
-> For displaying custom attribute as input fields liferay provide us 2 tags-:
- <liferay:ui-custom-attribute className = "" classPK = "" name ="" />
- This display one attribute field
- <liferay:ui-custom-attribute-list className = "" classPK = "" name ="" />
- This display a list of attributes.
-> className - This is class name of entity in our case its com.liferay.portal.model.User
-> classPK - The primary key of entity instance
-> name - Name of the specific attribute
Display custom field using JSP page
add this code in your jsp page
<%
User currentUser = themeDisplay.getUser();
%>
<portlet:actionURL var=
"myContactIdURL"
name=
"myContactId"
/>
<aui:form action=
"<%= myContactIdURL %>"
method=
"post"
>
<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>
add this in your portlet class
@ProcessAction
(name=
"myContactId"
)
public
void
myContactId(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);
}
2. Adding Custom field programatically-:
Create a JSP page and add this code-:
<portlet:actionURL var=
"addCustomFieldURL"
name=
"addUserCustomeField"
/>
<aui:form action=
"<%= addCustomFieldURL %>"
method=
"post"
>
<aui:input name=
"customField"
label=
"Custom Field"
/>
<aui:button type=
"submit"
value=
"Add Custom Field"
/>
</aui:form>
Add this code in your portlet class-:
@ProcessAction
(name=
"addUserCustomeField"
)
public
void
addUserCustomeField(ActionRequest actionRequest,
ActionResponse actionResponse)
throws
IOException,
PortletException, PortalException, SystemException
{
String myContactId = ParamUtil.getString(actionRequest,
"customField"
,
""
);
User user = PortalUtil.getUser(actionRequest);
user.getExpandoBridge().addAttribute(myContactId);
}
In the same way we can add Organization, Site, Role
// Adding Custom Field
Organization org = null;
org.getExpandoBridge().addAttribute(myOrganizationId);
Site site = null;
site.getExpandoBridge().addAttribute(mySite);
Role role = null;
role.getExpandoBridge().addAttribute(myRole);
//Update or set attribute
org.getExpandoBridge().setAttribute(key, value);
site.getExpandoBridge().setAttribute(key, value);
role.getExpandoBridge().setAttribute(key, value);
These only are not the way to add custom field one of the way is-:
ExpandoTable table = null;
table = ExpandoTableLocalServiceUtil.addTable(User.class.getName,"CUSTOM-FIELD");
table =
ExpandoTableLocalServiceUtil.getTable(User.class.getName,"CUSTOM-FIELD");
ExpandoColumn column = null;
column = ExpandoColumnLocalServiceUtil.addColumn(table.getTableId(),"CUSTOM-FIELD", "Data Type");
column = ExpandoColumnLocalServiceUtil.getColumn(table.getTableId(),"CUSTOM-FIELD");
Comments
Post a Comment