September 2008
2008-09 Joomla! Community Magazine
Joomla! Community Magazine - Learning
Extending the User Object in Joomla! 1.5
How to easily add additional data elements for Users
The Usermeta plugin makes use of hooks in the JUser library to extend the properties of the user object. This would allow a developer to customize a user registration form to include information not available in the standard form. There are two different methods for extending the user object. The first is by adding to the user parameters which are stored in the params field of the jos_users table. The second method is by creating a database table joined to jos_users by a foreign key.
User parameters are defined in XML setup files which by default are stored in administrator/components/com_user/models. The UserMeta plugin provides an example custom XML setup file located in plugins/system/usermeta. A developer can use this file to define custom parameters for the user object then enable the UserMeta plugin to make them available through the hooks provided in JUser.
In the method JUser::getParameters() it is possible to define the directory path to XML setup files to be used for all user parameters.
JUser::getParameters( [ $loadsetupfile = false[, $path = null ] ] )
The second parameter of the getParameters() method is $path and by defining that parameter when statically calling the method the $parampath will then be defined as a static local variable in the getParameters() method. During subsequent calls to getParameters(), if the $path parameter is left undefined, the previous value will be used.
By taking advantage of the plugin trigger onAfterInitialise(), it is then possible, using a system plugin, to define a directory path to custom XML setup files where user parameters have been defined.
/**
* Define parampath as a directory path to custom XML setup files
*
* @return void
* @access public
*/
function onAfterInitialise()
{
// Enable the custom xml setup file to extend the user parameters
JUser::getParameters(false, ‘path/to/files’);
}
Once this plugin is executed the custom parameters are immediately part of the user object. If you choose to edit a user in the User Manager of the administrator you will see the custom parameters in the parameters box ready to be edited.
The one drawback of using this method to extend user parameters is that all of the custom parameters are stored in one database field. This makes it difficult to retrieve the data for other uses. The UserMeta plugin offers the option to extend the user object by using this parameter method or by using another database table joined by a foreign key.
Similar to the getParameters() method there is a getTable() method available to developers.
JUser::getTable()( [ $type = null[, $prefix = ‘JTable’ ] ] )
The method makes use of a static local variable to define which table class the application will use in building the user object. First the table class needs to be added to the JTable include path and then defined as the class to use in JUser. Again, this is possible by using a system plugin and the onAferInitialize() plugin trigger.
/**
* Define the table name of the user table
*
* @return void
* @access public
*/
function onAfterInitialise()
{
// Add the tables path to the JTable library
JTable::addIncludePath(‘path/to/table/class’);
// Set the custom table object as the user table
JUser::getTable('NameOfTableClass');
}
Obviously when using this method it is necessary to develop a custom table class capable of joining two tables. This is not possible using the current Joomla! framework but a custom class is provided in the UserMeta plugin which joins the jos_users table with a custom table named jos_usermeta. The fields of the jos_usermeta table can be defined by the developer installing the plugin. The plugin then dynamically reads the table information from the database and builds the user object.
It is possible to add these custom parameters and properties to both the user manager in the administrator and the registration form on the frontend. There are full instructions for using the plugin in a README file provided with the plugin.


