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

How to get node_id when extending XenForo_ControllerPublic_Abstract

Discussion in 'XenForo Development Discussions' started by AndyB, Oct 27, 2013.

  1. AndyB

    AndyB Well-Known Member

    In my add-on Similar Threads, I would like to get the node_id when a user is creating a thread.

    Currently I'm able to get the thread title (what the user types into the input box) with the following code:

    PHP:
    $newTitle $this->_request->getParam('title');
    however when I try to get the node_id this code doesn't work:

    PHP:
    $currentNodeId $this->_request->getParam('node_id');
    Thank you.
     
  2. Jeremy

    Jeremy XenForo Moderator Staff Member

    Why are you extending ControllerPublic_Abstract, for one?
     
  3. AndyB

    AndyB Well-Known Member

    Hi Jeremy,

    Here's a link which shows the code for the Similar Threads v1.3.

    http://xenforo.com/community/threads/similar-threads-v1-3.61941/

    I need to extend a file that runs when a user is creating a new thread. Is there a better file to extend?
     
  4. Jeremy

    Jeremy XenForo Moderator Staff Member

    You should always extend specific controllers unless you are creating your own route and everything.
     
  5. AndyB

    AndyB Well-Known Member

    In this case I'm creating my own route which is used for the AJAX to function. Does this mean that extending the ControllerPublic_Abstract in this case is correct?
     
  6. Nobita.Kun

    Nobita.Kun Well-Known Member

    Want to get node_id you can extends ControllerPublic_Forum to get that. In case AJAX you can put url: `forums/your-action`
     
  7. AndyB

    AndyB Well-Known Member

    Hi Nobita.Kun,

    Thank you very much for explaining that I should have extended the ControllerPublic_Forum. I have now made the code changes and everything still works great. However I'm not able to get the node_id. I would assume the following code would work, but it does not.

    PHP:
    <?php
     
    class Andy_SimilarThreads_ControllerPublic_Forum extends XenForo_ControllerPublic_Forum
    {
        public function 
    actionIndex()
        {        
            
    // call parent function
            
    parent::actionIndex();
            
            
    $forumId $this->_input->filterSingle('node_id'XenForo_Input::UINT);
    ...
    ?>
     
  8. Nobita.Kun

    Nobita.Kun Well-Known Member

    actionIndex should be actionForums
     
  9. AndyB

    AndyB Well-Known Member

    When I did that, the forum list is return from my AJAX results.
     
    Last edited: Oct 28, 2013
  10. Nobita.Kun

    Nobita.Kun Well-Known Member

    Recommend you create new action. Example: actionTest so in ajax you can put: forums/test
     
  11. AndyB

    AndyB Well-Known Member

    Hi Nobita.Kun,

    Thank you for your help in this thread. I ended up extending the XenForo_ControllerPublic_Forum and using the actionIndex. The reason is if I use any other action besides actionIndex the AJAX would stops working.

    I was never able to get the node_id even after extending XenForo_ControllerPublic_Forum. However I think that extending XenForo_ControllerPublic_Forum is better than extending XenForo_ControllerPublic_Abstract.
     
  12. Milano

    Milano Well-Known Member

    Why are you extending XenForo_ControllerPublic_Forum, you should do this via XFCP_ and code event listener (load_class_controller) so it will work
     
    Last edited: Oct 28, 2013
  13. AndyB

    AndyB Well-Known Member

    Hi Milano,

    Thank you for your assistance.

    I changed my code to the following:

    PHP:
    class Andy_SimilarThreads_ControllerPublic_Forum extends XFCP_Andy_SimilarThreads_ControllerPublic_Forum
    {
        public function 
    actionIndex()
        {
    ...
    I created the Code Event Listener and updated my Listener.php file. However when I try to create a new thread, type something into the title input I get the following error:

    An exception occurred: Cannot load class using XFCP. Load the class using the correct loader first. in /home/southbay/www/forums/library/XenForo/Autoloader.php on line 108
     
  14. Milano

    Milano Well-Known Member

    Maybe there were something wrong in your Listenter.php file or your directory structure
     
  15. AndyB

    AndyB Well-Known Member

    Last edited: Oct 28, 2013
  16. Milano

    Milano Well-Known Member

    Can you post your updated Listener file?
     
  17. AndyB

    AndyB Well-Known Member

    Listener.php

    PHP:
    <?php

    class Andy_SimilarThreads_Listener
    {
        public static function 
    Thread($class, array &$extend)
        {
            
    $extend[] = 'Andy_SimilarThreads_ControllerPublic_Thread';
        }    
        
        public static function 
    Forum($class, array &$extend)
        {
            
    $extend[] = 'Andy_SimilarThreads_ControllerPublic_Forum';
        }    
    }

    ?>
     
  18. Nobita.Kun

    Nobita.Kun Well-Known Member

    Why not merge two controllers into a function?
    if(condition1)
    {
    exe1
    }else if(conditions2) {
    exe2
    }
    ?
     
  19. Milano

    Milano Well-Known Member

    Because he's using Event Hint
    BTW @AndyB did you check your Event Hint class name? In this case it should be XenForo_ControllerPublic_Thread and XenForo_ControllerPublic_Forum
     
    Valhalla likes this.
  20. AndyB

    AndyB Well-Known Member

    Hi Milano,

    This is my Code Event Listener:

    pic001.jpg
     

Share This Page