With the server up and running, log in as an administrator.
Go to Control Panel → Portal → Custom Fields.
Click on the User link at the Custom Fields page.
Click on the button Add Custom Field.
Enter Interested-in-becoming-an-astronaut in the Key box.
Choose Selection of Text Values in the Type.
Click Save.
You should see the newly created field listed under the User page.
Click on the Interested_in_becoming_an_astronaut link again to return to the Edit view.
Change the Default Value to:
Not interested at all
Somewhat interested
Interested
Very interested
Change the Display Type to Radio.
Click Save.
Click My Account → Custom Fields under Miscellaneous.
You should now see an Interested in Becoming an Astronaut option.
UPDATING CUSTOM FIELDS PERMISSIONS
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.
We'll have to modify the permissions.
Go back to Control Panel → Portal → Custom Fields → User.
Click the Actions button that corresponds to the Interested-in-becoming-an-astronaut custom field and selectPermissions.
Add a check for Update and View on the Guest and User roles.
Click Save.
Now that guests can view and update this field, let's add this field to the Create Account page.
ADDING CUSTOM FIELDS PROGRAMMATICALLY
As we've seen, Custom Fields can be added through the user interface which can be useful for initial testing and set up.
Custom Fields can also be added programmatically using the Expando API.
Note: Custom Fields are called Expandos in the back end API.
This is useful when migrating Custom Fields from one environment to another.
The easiest way to do this is through a custom StartupAction implemented with aHook Plugin.
GET/ADD DEFAULT EXPANDO TABLE
First, we get the default Expando table for the User class if it exists, and add the default Expando table if it does not exist.
Use getDefaultTable and addDefaultTable when working with objects that have Custom Fields in theUI, such as the User and Organization objects (use getTable and addTable for custom Expando tables).
What if we wanted to programmatically update the data in the library-id column for a specific user?
Use ExpandoValueLocalServiceUtil.addValue to add or update the value of theComment-astronauts Expando Column for the specific user.
String emailAddress = "test@liferay.com";
User user = UserLocalServiceUtil.getUserByEmailAddress(companyId, emailAddress);
long classNameId = table.getClassNameId();
long columnId = column.getColumnId();
long classPK = user.getUserId();
String data = "Love them since I was a child!";
ExpandoValue value = ExpandoValueLocalServiceUtil.addValue(
classNameId, tableId, columnId, classPK, data);
classNameId: maps the value of User.class.getName() to an ID (the class name is mapped to an ID for performance, query based on an ID vs. a String)
tableId: the tableId of the default Expando Table for the User class
columnId: the columnId of the "library-id" Expando Column
classPK: the primary key of the class we want to perform an update on (update the data for the usertest@liferay.com, classPK = user.getUserId())
data: data / value to store in the library-id column
EXERCISE: CREATE HOOK PROJECT (I)
Go to File → New → Liferay Hook.
Hook plugin project: training-hook
Select Hook type(s) to create: Portal properties
Click Next.
Click the Add button next to the Define actions to be executed on portal events text box.
Events: Click the Select button.
Select application.startup.events and click OK.
Class: Click the New button.
Classname: ExpandoStartupAction
Java package: com.liferay.training.startupaction
Click Create → OK → Finish.
Open the portal.properties file under docroot/WEB-INF/src. You should find that Liferay Developer Studio has added the following: