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

Getting user_id from forum post

Discussion in 'XenForo Development Discussions' started by tonymacx86, Jul 21, 2015.

  1. tonymacx86

    tonymacx86 Member

    Hey All!

    Recently purchased a license for XenForo with the intent of migrating from VBulletin 4. Really love the software (XenForo, that is).

    I'm making an add-on that will use the "message_content" content hook within the "message" template to insert a custom template. In the process of that I look up some custom info from a separate table using the poster's user_id, but I'm having trouble getting that from getParams().

    I see that there's an array of posts which lists all of the posts of the thread by post_id, but I can't figure out an efficient way to the user_id of that post within my listener.

    Thanks so much for any insight!

    -adamsmasher
    tonymacx86.com
     
  2. katsulynx

    katsulynx Well-Known Member

    Every entry in that array should consist of an array with the following stuff (including your user_id):

    Unbenannt-2.jpg

    You pretty much have already what you want, just call it.
     
  3. batpool52!

    batpool52! Well-Known Member

    If you mean template hooks, then template hooks are depreciated, make use of template modifications system.

    Here is how I would try to extend:
    PHP:
    <?php

    class Demo_ControllerPublic_Thread extends XFCP_Demo_ControllerPublic_Thread
    {
        public function 
    actionIndex()
        {
            
    $response parent::actionIndex();
           
            if (isset(
    $response->params$response->params['posts']) && count((array)$response->params['posts']) > 0)
            {
                
    // Adding user ids to a array so that there aren't multiple queries
                // for something simple which can be done in a single query
                
    $userIds = array();
                foreach (
    $response->params['posts'] as $postId => $post)
                {
                    if (isset(
    $post['user_id']))
                    {
                        
    $userIds[] = $post['user_id'];
                    }
                }
                if (
    count((array) $userids) > 0)
                {
                    
    // Make sure getMyStuffByUserIds is a array and each key is the user id
                    
    $userStuff $this->_getDemoModel()->getMyStuffByUserIds($userIds);
                    foreach (
    $response->params['posts'] as $postId => &$post)
                    {
                        if (isset(
    $post['user_id'], $userStuff[$post['user_id']]))
                        {
                            
    $post['stuff'] = $userStuff[$post['user_id']];
                        }
                        else
                        {
                            
    $post['stuff'] = false// false or anything else you want to be as default value
                        
    }
                    }
                }
            }
           
            return 
    $response;
        }
       
        protected function 
    _getDemoModel()
        {
            return 
    $this->getModelFromCache('Demo_Model_Demo');
        }
    }
     
    jeffwidman likes this.
  4. tonymacx86

    tonymacx86 Member

    I saw that data, but the index value for each in the array is the id of the post itself, so I wasn't sure how to specify that this post is X post in the array.

    - adamsmasher
    tonymacx86.com
     
  5. tonymacx86

    tonymacx86 Member

    It is? Glad I asked then :) I used template hooks since it was an easier transition for me from vBulletin, being the self-taught PHP hack that I am.

    - adamsmasher
    tonymacx86.com
     

Share This Page