How can I get information about the user?

Published: Wednesday, 06 May 2015 09:09

If users are registered on your site and logged in you can get any information that has been saved about them and use that in your forms.

Not registered

If the user is not registered there is very little information available unless you use a form to ask for it.

Registered but not logged in

Until the user logs in they are treated as a unregistered guest. Normally you will invite them to login. If there is a good reason it's also possible to identify them by username name or email.

Registered and logged in

If the user is registered and logged in then you can get all of the User details from the jos_users table by calling the Joomla! User object. Drag a Custom Code action into the Event you are working with and move it up to the right place in the process flow - for example, in an On Submit event it should probably be after any Anti-Spam actions and before any Email or DB Save actions. Open the action to Configure it and add code like this:

$user = \JFactory::getUser();

Then, for example, the user name is available as $user->name, the id as $user->id, email as $user->email, etc.

So, if you want to load information about a user from a database table you can use a Custom Code action to put the User ID in the $form->data array and then use that as the 'token' in a DB Record Loader action. In this case you'd use user_id in the Token box:

$user = \JFactory::getUser();
$form->data['user_id'] = $user->id;

Other users

If you want information about another user and you have their user id then you can also use:

$user = \JFactory::getUser($user_id);

To use this information in another action e.g. an email, you need to add it to the ChronoForms $form->data array. For example to add the user email:

$form->data['user_email'] = $user->email;

You can then add user_email into one of the Email action Dynamic Email Address boxes, or use {user_email} in the Email template.

Newly registered

After the Joomla! Registration action has run the new user profile is available in the $form->data['_PLUGINS_']['joomla_registration'] object. So, for example, if you need to save the new user ID to a database table you can add this code in a Custom Code action

$form->data['user_id'] = $form->data['_PLUGINS_']['joomla_registration']['id'];

This will put the id into the 'user_id' variable so it will save to a user_id column in a table or you can use {user_id} in a Thank You page action.

If you want all the User info, including the user parameters (language, time-zone, etc.) then get my Load User Info [GH] action (CFv4 only) which will do that for you and put all the user info into the $form->data array.