Tuesday, May 20, 2014

Adding Custom Attributes Through Control Pannel,for some of out of the box Entities.

To Adding Custom Attributes Through Control Panel the following need to be done.

1 . we have to add an entry in liferay-portlet.xml  for the particular portlet.

In my case i have add custom field for user group.I have added following tag in liferay-portlet.xml  for the user_groups_admin (127) Portlet
<custom-attributes-display>com.liferay.portlet.usergroupsadmin.UserGroupCustomAttributesDisplay</custom-attributes-display> 
2.Write a class to return class name and Icon path.

package com.liferay.portlet.usergroupsadmin;
import com.liferay.portal.model.UserGroup;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portlet.expando.model.BaseCustomAttributesDisplay;
public class UserGroupCustomAttributesDisplay extends BaseCustomAttributesDisplay {
public static final String CLASS_NAME = UserGroup.class.getName();
@Override
public String getClassName() {
return CLASS_NAME;
}
@Override
public String getIconPath(ThemeDisplay themeDisplay) {
return "/html/icons/user_groups_admin.png";
}
}
 3.Now we have to deploy the ext as you have modified out of the box portlet.
4.Now you have to update jsp through the hook to display(view) and edit ,in detailed page of entty
my case:edit_user_group.jsp

 <liferay-ui:custom-attribute-list
className="com.liferay.portal.model.UserGroup"
classPK="<%= userGroup!= null ? userGroup.getUserGroupId() : 0 %>"
editable="<%= true %>"
label="<%= true %>"
/> 
5.Now we have to save custom fields,so we have to add following code in action class before calling update or adding functionality.

ServiceContext serviceContext = ServiceContextFactory.getInstance(
UserGroup.class.getName(), actionRequest);
userGroup.setExpandoBridgeAttributes(serviceContext);
UserGroupLocalServiceUtil.updateUserGroup(userGroup);
Note:if add or update functions are having  serviceContext as parameter then you have to get service Context and pass

Now It will save the usergroup attributes

2 comments:

  1. Thanks for the useful information :-)

    ReplyDelete
  2. Hi:
    I want to add additional fields to the Tasks, possible?

    Thanks

    ReplyDelete