2

I've developed a plugin that allows me to create a custom user profile. It works and I'm able to add/remove fields as necessary.

The issue I'm facing is: How do I remove and/or change the 'name' field in the registration form so that I can ask for first and last name separately?

Because the users database has name in it I know that I can't just drop the filed. So, I was thinking it might be best to concat the first and last name into the 'name' field for entry into the database. However, when trying to implement this I ran into issues.

To remove the 'name' field from the registration form I added the following to the onContentPrepareForm() function in my profile plugin:

$form->removeField('name');

This successfully removes the field. However, I now need to set name = first name + last name elsewhere. I've read on these forums that I could change the data in the onUserBeforeSave() function. Unfortunately that is not called before I get a validation error message saying that registration failed and that I need to enter a name. I then tried to turn required off in the registration.xml file. That didn't work (or I changed the wrong file).

I then thought that perhaps I could put it in the onContentPrepareData() function. The issue I ran into here is that 'name' was removed from the form and it's not in the $data array.

What should I do? What is the proper way to fix this?

Thanks in advance for your assistance.

2

Let me start by saying I do not condone this method, the next time you update Joomla there is a high probability you will undo these changes completely.

This is however a working method to do what you are trying to accomplish.

Edit the file components > com_users > models > forms > registration.xml

Comment out the name field

<!--<field
    name="name"
    type="text"
    description="COM_USERS_REGISTER_NAME_DESC"
    filter="string"
    label="COM_USERS_REGISTER_NAME_LABEL"
    required="true"
    size="30"
/>-->

Add these 2 fields

<field
    name="firstname"
    type="text"
    description="Users First Name"
    filter="string"
    label="First Name"
    required="true"
    size="30"
/>

<field
    name="lastname"
    type="text"
    description="Users First Name"
    filter="string"
    label="First Name"
    required="true"
    size="30"
/>

In the file components > com_users > controllers > registration.php

Add this line of code around line 127:

$requestData['name'] = $requestData['firstname'].' '.$requestData['lastname'];

Immediately after

$requestData = $this->input->post->get('jform', array(), 'array');

This will concatenate your firstname and lastname fields into the name field required by Joomlas registration.

The other method of doing this you could just simply install Community Builder, setup any custom fields you want to and let CB handle your registration for you instead of com_users. This would also allow you to query any user related fields in the database at a later date if you need to by querying the CB tables in the database using the user id as the filter.

  • Thank you for trying to help. This seems to be a question that doesn't have an easy answer. I'd prefer to not use CB. I've used it with other projects and it's not my favorite extension. Besides, it's overkill for what I need. Is it possible to do what you suggest using an override? I'd rather not have to change registration.xml and registration.php each time Joomla is updated. – nimble Feb 9 '16 at 1:48
  • You could do an override of the user class, but I am honestly not even sure where to point you to get your started with that. – Terry Carter Feb 9 '16 at 3:07
1

You can create another Registration controller and override the register() behavior.

<?php
class UsersControllerRegistration2 extends UsersController
{
    public function register()
    {
        ...
        $requestData = $this->input->post->get('jform', array(), 'array');
        $requestData['name'] = $requestData['firstname'].' '.$requestData['lastname'];
        ...
        $data   = $model->validate($form, $requestData);
        ...
    }
}
?>

You can see the missing parts of this register() method in the original method. Name this new controller file registration2.php and put it in the components/com_users/controllers/ directory.

In your registration form, you have to change the action target and the task input (change it by overriding the registration view on your template).

 <form id="member-registration" action="<?php echo JRoute::_('index.php?option=com_users&task=registration2.register'); ?>" method="post" class="form-validate form-horizontal well" enctype="multipart/form-data">
 ...
  <input type="hidden" name="task" value="registration2.register" />
 ...
 </form>

If I didn't forget details, it should work and without modifying any core files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.