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

Set a custom profile field when creating a thread in a specific forum

#1
We got a journal forum where most of the users got their own thread.

Are there any simple way I can update a users custom profile field when they create a new thread there?

Would be great if someone can point me in the right direction
 

Fuhrmann

Well-known member
#2
Extend the XenForo_ControllerPublic_Forum::actionAddThread() and put this:

PHP:
$dwUser = XenForo_DataWriter::create('XenForo_DataWriter_User');
$dwUser->setExistingData($visitor->toArray());
$dwUser->setCustomFields(array('name_of_the_custom_field' => 'value_to_update'));     
 
$dwUser->save();
 
#6
Just started looking at Xen development, but as far as I understand I can create a load_class_controller listener and replace the entire actionAddThread, or am I missing something?
 

Fuhrmann

Well-known member
#7
Just started looking at Xen development, but as far as I understand I can create a load_class_controller listener and replace the entire actionAddThread, or am I missing something?
Yes! :)

Dont forget, inside you own addThread extended function to call the parent:

PHP:
parent::actionAddThread();
 

Fuhrmann

Well-known member
#9
Something like this:


PHP:
public function actionAddThread()
{
    $reponse = parent::actionAddThread();
   
    //Here you can check what you want to check
   
    //And here you update the custom user field
    $visitor = XenForo_Visitor::getInstance()->toArray();
    $dwUser = XenForo_DataWriter::create('XenForo_DataWriter_User');
    $dwUser->setExistingData($visitor);
    $dwUser->setCustomFields(array('name_of_the_custom_field' => 'value_to_update'));     
    $dwUser->save();
   
    //return the original action
    return $response;
}
 
#11
This works. Guess some of the code should be cleaned up a bit

PHP:
class Fitnessbloggen_LoggbokField_ControllerPublic_LoggbokField extends XFCP_Fitnessbloggen_LoggbokField_ControllerPublic_LoggbokField {
 
    public function actionAddThread()
    {
        // store original response
        $response = parent::actionAddThread();
 
        // cleanest way to get node_id?
        $this->_assertPostOnly();
        $forumId = $this->_input->filterSingle('node_id', XenForo_Input::UINT);
 
        if ( $forumId == 2 ) {
 
            // set hostname dynamically? using redirectTarget seems hacky
            $LoggbokUrl = 'http://localhost:8888/xen/' . $response->redirectTarget;
 
            // update the custom user field
            $visitor = XenForo_Visitor::getInstance()->toArray();
            $dwUser = XenForo_DataWriter::create('XenForo_DataWriter_User');
            $dwUser->setExistingData($visitor);
            $dwUser->setCustomFields(array('Loggbok' => $LoggbokUrl));
            $dwUser->save();
 
        }
 
        // return the original action
        return $response;
    }
 
}
 

xfrocks

Well-known member
#12
Probably using this will save some extra queries (tell the data writer that the existing data can be trusted so it won't query the database)

PHP:
$dwUser->setExistingData($visitor->toArray(), true);
If you test this, please report back the result :p
 

Fuhrmann

Well-known member
#13
Probably using this will save some extra queries (tell the data writer that the existing data can be trusted so it won't query the database)

PHP:
$dwUser->setExistingData($visitor->toArray(), true);
If you test this, please report back the result :p
Your tips are so great!
 
#14
Guess I need to update the field when:
  • Users rename their own threads (actionSave?)
  • We delete threads (actionDelete?)
  • We move misplaced threads (actionMove?)
This is turning into a slightly larger project than I imagined.