
Custom Fields and JSP Hooks
Introduction #
Our goal is to take advantage of several new Liferay Portal features to add a field to the Create Account portlet, and persist submitted values to the database. In the past (prior to Liferay 5.2), this would have most likely been accomplished using the EXT environment or a combination of Portal Hook Plugins. In this example, the following Liferay Portal features will be used to accomplish this:
- Custom User Fields
- Plugins SDK Hooks
Note - The steps in this article rely on several fixes that were added after Liferay 5.2.2 were released. You will need to use 5.2.3 when it becomes available , or build from source.
Creating Custom User Fields #
-Login to the portal as an administrator -Select Control Panel from the Dock -Select Users' from the Portal section of the left hand nav bar -Click Custom Fields from the top of the panel -Click the Add Custom Fields button -Enter home-town-newspaper in the Key field -Select Text Field - Indexed in the Type field -Select Save
You have now created a Custom Field that will be persisted to the database. By default, the field won't be available for all users, so we'll need to modify the permissions.
-Click the Actions button that corresponds to the home-town-newspaper custom field and select Permissions. -Add a check for Update and View on the Guest role (You may also want to add Update and View permissions for the User role as well if you want users to be able to update their choice through the My Accounts screen) -Click Submit.
Your custom field is now ready to be used.
Note - By using the hyphenated name for the Key field, Liferay provides some very nice features. Adding the key to the language files allows you to localize the term. Also, if the key is a term that's already been localized then you don't have to add it, it will just work. Lastly, Liferay will apply a parser that will pretty print the key for you, so it will become Home Town Newspaper when displayed as a label in the JSP.
Modifying the Create Accounts page #
Create Portal Hook Plugin #
This section assumes that you have already configured the Liferay Plugins SDK environment and have access to the Liferay Portal source code. If not, please refer to this article http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/Plugins+SDK for information on setting up the Plugins SDK. The portal source code can be downloaded from the downloads http://www.liferay.com/web/guest/downloads/portal section of Liferay.com.
-Create a new folder in your plugins/hooks folder called customjsp-hook -Create a file called build.xml with the following contents:
<?xml version="1.0"?> <project name="hook" basedir="." default="deploy"> <import file="../build-common-hook.xml" /> </project>
-Create a new folder inside of customjsp-hook named docroot -Create a new folder inside of docroot named WEB-INF -Create a new file inside of WEB-INF named liferay-hook.xml with the following contents: Note - there should no square brackets in the 2nd line of the xml file
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 5.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_5_2_0.dtd" > <hook> <custom-jsp-dir>/WEB-INF/jsps</custom-jsp-dir> </hook>
-Create a new file inside of WEB-INF named liferay-plugin-package.properties with the following contents:
name=customjsp module-group-id=liferay module-incremental-version=1 tags= short-description=This hook contains a custom Create Accounts JSP change-log= page-url=http://www.liferay.com author=Liferay, Inc. licenses=MIT required-deployment-contexts=
-Create the following folder structure inside of the WEB-INF folder:
jsps/html/portlet/login
-Copy the file, create_account.jsp from liferay-portal-src-5.x.x/portal-web/docroot/html/portlet/login to customjsp-hook/docroot/WEB-INF/jsps/html/portlet/login
Add Custom Fields to Custom Create Account page #
-Open the create_account.jsp file and find the email address portion of the form (Search for key="email-address") -Insert the following code between the ending </div> tag of the mail section and the ending </fieldset> tag
<div class="exp-ctrl-holder"> <liferay-ui:custom-attribute className="<%= User.class.getName() %>" classPK="<%= 0 %>" editable="<%= true %>" label="<%= true %>" name="home-town-newspaper" /> </div>
- Save the file -From a command prompt, navigate to the plugins/hooks/customjsp-hook folder and deploy your custom hook using the following command:
ant deploy
-Watch the command window and the console to ensure the hook is deployed and registered successfully and available for use. -Log in to Liferay and from the SIgn In portlet, select Create Account to see the updated JSP with your custom field.