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
 
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();
 
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?
 
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();
 
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;
}
 
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;
    }
 
}
 
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
 
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!
 
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.
 
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!
 
What about adding your code to _postSave() and _postDelete() for the datawriter? Probably save you some troubles :D
 
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 Bottom