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

Discussion in 'XenForo Development Discussions' started by Espen Espelund, Nov 24, 2011.

  1. Espen Espelund

    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
     
  2. Fuhrmann

    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 likes this.
  3. Espen Espelund

    Espen Espelund Active Member

    Thanks a lot. I'll give it a try
     
  4. Espen Espelund

    Espen Espelund Active Member

  5. Fuhrmann

    Fuhrmann Well-Known Member

  6. Espen Espelund

    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?
     
  7. Fuhrmann

    Fuhrmann Well-Known Member

    Yes! :)

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

    PHP:
    parent::actionAddThread();
     
  8. Espen Espelund

    Espen Espelund Active Member

    Ah, so I won't need to update the code every time I upgrade. I'll give it a try
     
    Fuhrmann likes this.
  9. Fuhrmann

    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;
    }
     
  10. Espen Espelund

    Espen Espelund Active Member

    Working. Thanks alot! :)

    I'll have a look at the rest of the stuff I need to get it functioning propertly tomorrow.
     
    Fuhrmann likes this.
  11. Espen Espelund

    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 == ) {
     
                
    // 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;
        }
     
    }
     
  12. xfrocks

    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 likes this.
  13. Fuhrmann

    Fuhrmann Well-Known Member

    Your tips are so great!
     
  14. Espen Espelund

    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 likes this.
  15. Fuhrmann

    Fuhrmann Well-Known Member


    Dont forget the inline mod actions!
     
  16. xfrocks

    xfrocks Well-Known Member

    What about adding your code to _postSave() and _postDelete() for the datawriter? Probably save you some troubles :D
     
  17. Espen Espelund

    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
     

Share This Page