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

Espen Espelund

Active member
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
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();
 

Espen Espelund

Active member
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
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
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;
}
 

Espen Espelund

Active member
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
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
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!
 

Espen Espelund

Active member
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.
 

Fuhrmann

Well-known member
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.

Dont forget the inline mod actions!
 

xfrocks

Well-known member
What about adding your code to _postSave() and _postDelete() for the datawriter? Probably save you some troubles :D
 

Espen Espelund

Active member
Can anyone think of a slightly easier of checking if the user has a journal topic and linking to it? I guess I can code all this, but it seems kinda clumsy
 
Top