• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Update default XenForo user data on registration.

Thomas.B

Well-known member
#2
I'm trying to update a user's data based on a few different conditions.
Have a look a this post for how to get the User ID. Although it's actually an example for threads you can use the same principle for users.

Would it be better to extend XF_CP_AccountConfirmation instead of XF_CP_Register to do this? As then the user data is created (I think....)
AccountConfirmation only changes the user state. The actual user record creation is initiated in the Register controller. So this would probably be the better choice..
 

JamieSinn

Active member
#3
Have a look a this post for how to get the User ID. Although it's actually an example for threads you can use the same principle for users.


AccountConfirmation only changes the user state. The actual user record creation is initiated in the Register controller. So this would probably be the better choice..
Okay.
So, just as a sanity check,
Code:
$userID = XenForo_Application::get('user_id');
will return the user id of the current visitor/user registering?

Full code. I omitted the if conditions due to confidentiality.

PHP:
public function actionRegister()
    {
        $parent = parent::actionRegister();
        $this->_assertRegistrationActive();
        $userID = XenForo_Application::get('user_id');
        $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false;
        if ($ip !== false)
        {
            if (true)
            {
                $writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
                $writer->setExistingData('user_state', 'moderated');
                $writer->save();
            }
        }
        return $parent;
    }
 
Last edited:

Thomas.B

Well-known member
#4
Code:
$userID = XenForo_Application::get('user_id');
will return the user id of the current visitor/user registering?
Only if you previously saved it in the corresponding DataWriter's _postSave() method. But I noticed that there is an easier solution: if you overwrite _completeRegistration() instead of actionRegister() you can get the User ID as usual:
PHP:
protected function _completeRegistration(array $user, array $extraParams = array())
{
  parent::_completeRegistration($user, $extraParams);
  $userId = XenForo_Visitor::getUserId();
  ...
}
Also, $writer->setExistingData('user_state', 'moderated') should be $writer->set('user_state', 'moderated')
 

JamieSinn

Active member
#5
Only if you previously saved it in the corresponding DataWriter's _postSave() method. But I noticed that there is an easier solution: if you overwrite _completeRegistration() instead of actionRegister() you can get the User ID as usual:
PHP:
protected function _completeRegistration(array $user, array $extraParams = array())
{
  parent::_completeRegistration($user, $extraParams);
  $userId = XenForo_Visitor::getUserId();
  ...
}
Also, $writer->setExistingData('user_state', 'moderated') should be $writer->set('user_state', 'moderated')
Why would it be set instead of setexistingdata? The userstate exists does it not? or is it yet to be created?
 

Thomas.B

Well-known member
#6
Oh, I noticed there's still missing something. It should be:
PHP:
$writer->setExistingData($userId);
$writer->set('user_state', 'moderated');
The setExistingData() method will cause the DataWriter to do an update (of a specific record) instead of an insert. And with the set() method you can determine the new value ('moderated') for the specified field ('user_state').