1. 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.

Discussion in 'XenForo Development Discussions' started by JamieSinn, Aug 13, 2016.

  1. JamieSinn

    JamieSinn Active Member

  2. Thomas.B

    Thomas.B Well-Known Member

    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..
     
  3. JamieSinn

    JamieSinn Active Member

    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: Aug 14, 2016
  4. Thomas.B

    Thomas.B Well-Known Member

    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')
     
  5. JamieSinn

    JamieSinn Active Member

    Why would it be set instead of setexistingdata? The userstate exists does it not? or is it yet to be created?
     
  6. Thomas.B

    Thomas.B Well-Known Member

    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').
     

Share This Page