Hi Kyle,
Apologies for giving a hack as my response to the question however having spent some time on solving this I could not get Auto Actions to work on this occasion to solve this problem.
I have this evening again looked at the code and found why I could not get it to work with Auto Actions, this is something you may want to have a look at.
Apologies for this long explanation.
I like, timurdavidov, am not wanting to use the username field, however CB always requires that one is set.
To disable the use of the username on the field page of the CB setup I changed the username field settings to:
Required - No
Profile - No
Registration - No
Searchable - No
Now that part works fine. (I have a separate request/issue though I will ask later).
With these settings on registration the username is created either from the name field or if you do use name field then the email.
Like timurdavidov this is where we get the username warning as two people with the same name can't be registered as it creates a duplicate username.
So I looked at Auto Actions to help, what I found was:
Trigger onAfterUserRegistration is too far along the process you still receive the error when registering as this trigger is done at the end once the data is in the tables.
I then tried the onStartSaveUserRegistration trigger which does get fired before any data is entered.
The field action type wont work here as nothing has been set yet we are still working with the $_POST data.
So I tried using the code type and method of PHP and my code was:
Code:
$_POST['username'] = 'test';
This did not work I still kept getting the username warning. Even though the saveRegistration function in comprofiler.php uses the raw $_POST variable.
This is where I cheated I just did the core hack rather than figure out why that simple auto action did not work.
I have now gone back and had look at why this was not working.
So I followed your functions through the registration process.
I will mention everything I noticed.
comprofiler.php
line 959 saveRegistration function starts
at line 960 the $_POST is brought it and doing a print here I can see that my form submission $_POST is empty.
Code:
line 984
$_PLUGINS->trigger( 'onStartSaveUserRegistration', array() );
auto action triggered and $_POST = 'test' as set in my auto action.
Code:
line 1042
$saveResult = $userComplete->saveSafely( $_POST, $_CB_framework->getUi(), 'register' );
my update $_POST is used here but it was returning false so the javascript alert is show 2 lines below at 1044.
looking at the saveSafely function.
administrator/components/com_comprofiler/library/cb/cb.tables.php starting line 1350
Code:
function saveSafely( &$array, $ui, $reason ) {
this passes the $_POST in as the variable $array.
$array is still correctly set as "test".
Code:
line 1365
$bindResults = $this->bindSafely( $array, $ui, $reason, $oldUserComplete );
this accepts the $array variable with my username but outputs to $bindResults with the username set as empty.
As the username has now been unset, starting at line 1367 the username is set to the name field.
This is the bit I hacked to be the email instead
Up until $this->bindSafely everything was okay but it unsets the username.
the bindSafely function starts line 864
doing a print shows $array is still set with the new username.
lines 928 to 936 is where the $this variable which is at this point just has empty fields in it gets populated with the processed fields and the username becomes empty.
Now I can keeping drilling down in to the code
But I noticed at this point if in the field settings for the username field you select yes to show on registration.
Then the username field is not unset and stays as the value I set in my Auto Action.
So wherever the code goes next between 928 and 936 when it populates the $this variable. There must be a check being done that if we are not showing the username then unset it and let the code as described above change it to the name or email.
I am just guessing from trial and error that the code between 928 and 936 is looking at the user plugins in components/com_comprofiler/plugin/user and in particular the plug_cbcore but I can't get my head around where it unsets it as the file is quite large.
To cut a long story short
To make this work with auto actions and not have to use the hack then it must not unset the username.
Now I guess this unset is done by design so if the site is not using usernames a user can not inject their own username in to the original $_POST and have it set.
As a suggestion instead of the unset being so far down in the registration process bring it right to the start of saveRegistration. If the username is set in the $_POST unset it then let the auto action at onStartSaveUserRegistration set it to whatever.
Apologies again for the long reply but I thought it better to explain what I had found rather than being unhelpful and just saying "it does not work".
Kind Regards
Mike