Linking Users to Contacts Automatically

Have you ever wanted to extend the Joomla! 1.5 User Registration form to also include those fields in the Contact form? So you can grab your user’s address and phone number information at the registration period and have them available in your Contacts component?

Extending the Joomla! 1.5 User Registration Form

I needed a newsletter function on a site a while back, and earlier I’d written a component to let me send a mass email to every Contact or Category of Contacts. Around the same time I wrote a component to let a user delete his or her account from the site. It occurred to me this week that it sure would be nice if the contact were created when the user registered.

I don’t like to modify core code when I can avoid it because I really hate having to remember I’ve done so when it’s patch time. So, I took a plugin originally written by Kevin Devine and modified it to tie jos_users and jos_contact_details together so that when a user registers, he or she can also fill in the contact details fields. Automagically, then, when he or she creates the account, a contact is also created and linked. Now, when I go to send a mass mail, I’m sure to get it out to all the site users. It will also delete the associated contact detail record when the user is deleted.

Registration Form Fields and Corresponding Fields in jos_users or jos_contact_details

There is a little hitch, though. Every user has to have a contact detail row associated to the user row, or the account will be unable to login. Also, the contact detail is associated to the user before his or her account is activated, but it gets unlinked if any changes are made to the Contact before the User account is activated because unpublished users do not show up in the drop down inside the contact details screen.

This procedure works well for a site with one and only one back-end user. Because you can get things a bit garbled in the database by deleting a contact record which is linked to a user, it could cause problems with a site where a lot of people are managing users and contacts.

Let’s say John Doe has just registered on the site. We go to /administrator -> Site -> User Manager and we find him in the list of users. Note that he is not Enabled yet. He hasn’t checked his email and clicked the verification link yet. We could enable him by clicking the red x and turning it in to a green check.

A New Joomla! 1.5 User

If we click his name, we get his User detail screen.

User Details

The blue arrow indicates information he filled in in the User field portion of the registration form. The green arrow shows a bit of his contact detail information, and there is a button to click to get right to his contact screen. Note, however, the red arrow indicates that he is not active yet. If you want to edit his contact details, set Block User to No and click apply before navigating to the contact details screen.

Inactive User Contact Details

Even though the jos_user and jos_contact_details are linked properly in the database, because our John Doe is not active yet, when you go in this detail screen, his associated user is not in the list. This drop down does not populate with inactive accounts. Wait until the account is activated, and the drop down populates correctly.

Active Account

If you look in the list of Contacts, even when an account is inactive, it shows the linked user name. It’s only in this drop down that the linked inactive user does not appear. Just don’t click save on this screen if the user name is not in the drop down or the user will be unlinked to his or her contact record.

Also, deleting a user will delete the contact record, but deleting a contact will not delete the user. It will disable the user though, because the login requires a user to have a contact record. So, don’t delete contacts – only users.

How To Connect Users with Contacts at Registration

1. Create a Contacts Category called Site User (or Users or Website User or whatever you like).

Creating a new Category in Contacts

Note the ID as indicated by the blue arrow on the Users category in the image above. This is your users category id. You’ll need it later.

2. Associate EVERY user account to a contact in the category you created. After the plugin is enabled, any user account which does not have a contact detail associated to it will not be able to log in. This includes the System Administrator account.

3. Install the plugin. Do not enable it yet.

4. Edit the /plugins/system/usermeta/user.xml file. Find the catid param. Put your user category id number in the default.

       <param name="catid" type="hidden" default="7" label="catid" descripton="The category id of the Site Users category." />

5. Copy /components/com_user/views/register/tmpl/default.php to /templates/your_template/html/com_user/register/default.php. Also copy /components/com_user/views/user/tmpl/form.php to /templates/your_template/html/com_user/user/form.php. Edit these files and add the plugin tag where you want the contact detail fields to appear. Type UserMeta inside {} to add the fields.

Editing the PHP Files

6. Now, enable the plugin. Go to Extensions -> Plugin Manager.

Enable the Plugin


Leave a Reply

Your email address will not be published. Required fields are marked *