
« Zurück zu Expando
Adding Custom Attributes to Communities
(Umgeleitet von Adding an custom attribute to a Community)
Introduction #
This is an example of adding an Expando attribute called "title" on a com.liferay.portal.model.Group (a community).
Creating the Attribute #
void createAttribute(long companyId) ExpandoBridgeImpl expandoBridge = new ExpandoBridgeImpl(com.liferay.portal.model.Group.class.getName()); if (expandoBridge.hasAttribute("title")) { log.debug("attribute title already exists"); } else { log.info("adding title attribute"); expandoBridge.addAttribute("title"); } }
Setting Permissions for Community Members #
void setPermissions(long companyId) { ExpandoBridgeImpl expandoBridge = new ExpandoBridgeImpl(com.liferay.portal.model.Group.class.getName()); Role guest = RoleLocalServiceUtil.getRole(companyId, RoleConstants.GUEST); Role member = RoleLocalServiceUtil.getRole(companyId, RoleConstants.COMMUNITY_MEMBER); ExpandoColumn column = ExpandoColumnLocalServiceUtil.getColumn( expandoBridge.getClassName(), ExpandoTableConstants.DEFAULT_TABLE_NAME, "title"); String[] actionsRO = new String[] { ActionKeys.VIEW }; String[] actionsRW = new String[] { ActionKeys.VIEW, ActionKeys.UPDATE }; // make sure the resource exists Resource resource = ResourceLocalServiceUtil.addResource( companyId, ExpandoColumn.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(column.getColumnId())); PermissionLocalServiceUtil.setRolePermissions(guest.getRoleId(), actionsRO, resource.getResourceId()); PermissionLocalServiceUtil.setRolePermissions(member.getRoleId(), actionsRW, resource.getResourceId()); }
Using the Attribute #
Java #
void updateTitle(Group group, String title) { group.getExpandoBridge().setAttribute("title", title); log.info("updated group " + group.getName() + ", set title = " + twinspace.getExpandoBridge().getAttribute("")); }
Velocity #
Use the community (=Group) title in the theme, for ex. in portal_normal.vm
#set ($community_title = $themeDisplay.getScopeGroup().getExpandoBridge().getAttribute("title"))
Troubleshooting #
Permissions Error #
- Instead of using the ExpandoBridge you can use ExpandoTableLocalServiceUtil and ExpandoColumnLocalServiceUtil. These services don't use permission checking. This is a bit more code.
- You can temporarily set a permissionChecker with PermissionThreadLocal.setPermissionChecker(pc). Don't forget to reset it to the original one after you are done.
89929 Angesehen