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

actionForum running twice

Discussion in 'XenForo Development Discussions' started by AndyB, Mar 10, 2015.

  1. AndyB

    AndyB Well-Known Member

    Hello,

    In my add-on Forum Views:

    https://xenforo.com/community/resources/forum-views.4089/

    I'm counting how many times the forum_list is displayed. This currently works great as I'm extending the the actionIndex like this:

    PHP:
    <?php

    class Andy_ForumViews_ControllerPublic_Forum extends XFCP_Andy_ForumViews_ControllerPublic_Forum
    {    
        public function 
    actionIndex()
        {    
            
    // get parent        
            
    $parent parent::actionIndex();    

    <
    code removed for brevity>

            
    // return parent
            
    return $parent;
        }
    Now I would like to also count how many times the forum_view is displayed, so I'm extending the actionForum like this:

    PHP:
    public function actionForum()
        {
            
    // get parent        
            
    $parent parent::actionForum();    

    <
    code removed for brevity>

            
    // return parent
            
    return $parent;
        }
    The problem I'm seeing is each time the forum_view is displayed, the actionForum appears to run twice. What am I missing?

    Thank you.
     
  2. Nobita.Kun

    Nobita.Kun Well-Known Member

    Your code should be above the parent when it called.
     
  3. AndyB

    AndyB Well-Known Member

    Thank you, Nobita.Kun.

    This works perfect.

    PHP:
    public function actionForum()
    {
        <
    code removed for brevity>

        
    // return parent
        
    return parent::actionForum();
    }
     
    Nobita.Kun likes this.
  4. AndyB

    AndyB Well-Known Member

    Unfortunately further testing shows the solution in post #3 is not working.

    What I have noticed is the problem only occurs when the forum has more than one page. So for example if the forum has 5 total threads, the actionForum() runs once, but if the forum has 500 threads, the actionForum() runs twice.
     
  5. Nobita.Kun

    Nobita.Kun Well-Known Member

    SO you want to the code just running with once time with page = 1?
     
  6. Jon W

    Jon W Well-Known Member

    Do you just need to check that it is not counting redirects?

    PHP:
    public function actionForum()
    {
        
    $response parent::actionForum();

        if (
    $response instanceof XenForo_ControllerResponse_View) {
         <
    code removed for brevity>
        }

        return 
    $response;
    }
     
  7. AndyB

    AndyB Well-Known Member

    Hi Jon W,

    Thank you for taking the time to look at this issue, unfortunately checking for XenForo_ControllerResponse_View didn't work. I found the solution by adding the following code.

    PHP:
    // return parent action if response is not viewing first page
    if ($parent->params['page'] != 1)
    {
        return 
    $parent;
    }
     
  8. Nobita.Kun

    Nobita.Kun Well-Known Member

    If sometime the parent not responseView. You will get an error. You should ensure that it return as view and do it.
     
  9. AndyB

    AndyB Well-Known Member

    Hi Nobita.Kun,

    This is excellent advice. Thank you very much taking the time to share your knowledge.
     
    Nobita.Kun likes this.

Share This Page