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

I would like to extend Forum

Discussion in 'XenForo Development Discussions' started by AndyB, Sep 23, 2014.

  1. AndyB

    AndyB Well-Known Member

    I would like to extend Forum, but I'm not having any success passing my variable to the forum_view template.

    The following PHP code does not work:

    PHP:
    <?php

    class Andy_ShowModeration_ControllerPublic_Forum extends XFCP_Andy_ShowModeration_ControllerPublic_Forum
    {
        public function 
    actionIndex()
        {
            
    // get parent       
            
    $parent parent::actionIndex();

            
    // return parent action if this is a redirect or other non View response
            
    if (!$parent instanceOf XenForo_ControllerResponse_View)
            {
                return 
    $parent;
            }
                   
            
    // get cookie
            
    $showModeration XenForo_Helper_Cookie::getCookie('show_moderation');
           
            
    // prepare viewParams
            
    if ($parent instanceOf XenForo_ControllerResponse_View)
            {
                
    $viewParams = array(
                    
    'showModeration' => $showModeration
                
    );
               
                
    // add viewParams to parent params
                
    $parent->params += $viewParams;
            }   
           
            
    // return parent
            
    return $parent;
        }
    }
    and also tried using the subView code I learned how to do in this thread:

    https://xenforo.com/community/threads/i-would-like-to-extend-findnew.83748/

    but that doesn't work either.

    Thank you.
     
  2. Mr. Goodie2Shoes

    Mr. Goodie2Shoes Well-Known Member

    PHP:
    if (!$parent instanceOf XenForo_ControllerResponse_View)
    should be
    PHP:
    if (!($parent instanceof XenForo_ControllerResponse_View))
     
  3. AndyB

    AndyB Well-Known Member

    Hi Mr. Goodie2Shoes,

    Thank you for the suggestion, but I'm afraid that doesn't appear to be the problem.
     
    Last edited: Sep 23, 2014
  4. AndyB

    AndyB Well-Known Member

  5. Mr. Goodie2Shoes

    Mr. Goodie2Shoes Well-Known Member

    If you want to just add that variable... how about using the template_create listener for the forum_view template?
     
  6. Nobita.Kun

    Nobita.Kun Well-Known Member

    Hi Andy. Im not sure that you on the true way. If you want to assign an params into template Forum_view so the actionIndex won't work. Instead of that you should extends actionForum.

    Look at the final template return:
    PHP:
    return $this->responseView('XenForo_ViewPublic_Forum_View''forum_view'$viewParams);
     
    AndyB and Chris D like this.
  7. AndyB

    AndyB Well-Known Member

    Hi Nobita.Kun,

    Once again you have provided the correct answer. Thank you so much for taking time to help me. :)

    The following code works perfectly.

    PHP:
    <?php

    class Andy_ShowModeration_ControllerPublic_Forum extends XFCP_Andy_ShowModeration_ControllerPublic_Forum
    {
        public function 
    actionForum()
        {
            
    // get parent      
            
    $parent parent::actionForum();

            
    // return parent action if this is a redirect or other non View response
            
    if (!$parent instanceOf XenForo_ControllerResponse_View)
            {
                return 
    $parent;
            }
                  
            
    // get cookie
            
    $showModeration XenForo_Helper_Cookie::getCookie('show_moderation');
          
            
    // prepare viewParams
            
    if ($parent instanceOf XenForo_ControllerResponse_View)
            {
                
    $viewParams = array(
                    
    'showModeration' => $showModeration
                
    );
              
                
    // add viewParams to parent params
                
    $parent->params += $viewParams;  
            }
          
            
    // return forum_view
            
    return $this->responseView('XenForo_ViewPublic_Forum_View''forum_view'$parent->params);
        }
    }
     
    Last edited: Sep 23, 2014
  8. Chris D

    Chris D XenForo Developer Staff Member

    Why do you need to check if the $parent instanceof twice? You already check it once and return the parent if it's not what you want, so why check it again?

    Why declare the $viewParams variable?
    Why not just set 'showModeration' directly in the params?
    Why are you returning another controller response? This has already been done as per the call to the parent, so why not just return the $parent with your additional params in it?
     
    AndyB likes this.
  9. AndyB

    AndyB Well-Known Member

    Hi Chris,

    I was hoping you or someone else would review the code. I'll try to correct it to the best of my knowledge and post up in a few.

    Thank you. :)
     
  10. AndyB

    AndyB Well-Known Member

    This is much better. Thank you, Chris.

    PHP:
    <?php

    class Andy_ShowModeration_ControllerPublic_Forum extends XFCP_Andy_ShowModeration_ControllerPublic_Forum
    {
        public function 
    actionForum()
        {
            
    // get parent        
            
    $parent parent::actionForum();

            
    // return parent action if this is a redirect or other non View response
            
    if (!$parent instanceOf XenForo_ControllerResponse_View)
            {
                return 
    $parent;
            }
                    
            
    // get cookie
            
    $showModeration XenForo_Helper_Cookie::getCookie('show_moderation');
            
            
    // define viewParams
            
    $viewParams = array(
                
    'showModeration' => $showModeration
            
    );
            
            
    // add viewParams to parent params
            
    $parent->params += $viewParams;         
           
            
    // return parent
            
    return $parent;
        }
    }
     
  11. Chris D

    Chris D XenForo Developer Staff Member

    Much better. But you missed this one:

     
    AndyB likes this.
  12. AndyB

    AndyB Well-Known Member

    Chris, if you help me any more there will be no code left! (laughing)

    Thanks again.

    PHP:
    <?php

    class Andy_ShowModeration_ControllerPublic_Forum extends XFCP_Andy_ShowModeration_ControllerPublic_Forum
    {
        public function 
    actionForum()
        {
            
    // get parent        
            
    $parent parent::actionForum();

            
    // return parent action if this is a redirect or other non View response
            
    if (!$parent instanceOf XenForo_ControllerResponse_View)
            {
                return 
    $parent;
            }
                    
            
    // get cookie
            
    $showModeration XenForo_Helper_Cookie::getCookie('show_moderation');
            
            
    // add showModeration to parent params
            
    $parent->params += array('showModeration' => $showModeration);         
           
            
    // return parent
            
    return $parent;
        }
    }
     
  13. Chris D

    Chris D XenForo Developer Staff Member

    It was already valid, and it still is, but personally I would just do this:
    PHP:
    $parent->params['showModeration'] = XenForo_Helper_Cookie::getCookie('show_moderation');
     
    AndyB likes this.
  14. AndyB

    AndyB Well-Known Member

    Excellent!

    PHP:
    <?php

    class Andy_ShowModeration_ControllerPublic_Forum extends XFCP_Andy_ShowModeration_ControllerPublic_Forum
    {
        public function 
    actionForum()
        {
            
    // get parent        
            
    $parent parent::actionForum();

            
    // return parent action if this is a redirect or other non View response
            
    if (!$parent instanceOf XenForo_ControllerResponse_View)
            {
                return 
    $parent;
            }
                    
            
    // add showModeration to parent params
            
    $parent->params['showModeration'] = XenForo_Helper_Cookie::getCookie('show_moderation');        
           
            
    // return parent
            
    return $parent;
        }
    }
     
  15. Chris D

    Chris D XenForo Developer Staff Member

    And, actually, sorry to be a cretin, but I'd probably do this:

    PHP:
    <?php

    class Andy_ShowModeration_ControllerPublic_Forum extends XFCP_Andy_ShowModeration_ControllerPublic_Forum
    {
        public function 
    actionForum()
        {
            
    // get parent      
            
    $parent parent::actionForum();

            
    // do some view specific stuff if it is a view
            
    if ($parent instanceof XenForo_ControllerResponse_View)
            {
                
    // add showModeration to parent params
                
    $parent->params['showModeration'] = XenForo_Helper_Cookie::getCookie('show_moderation');
            }
         
            
    // return parent
            
    return $parent;
        }
    }
     
    Daniel Hood and AndyB like this.
  16. AndyB

    AndyB Well-Known Member

    I sure do enjoy learning from the best. You're help today, Chris, is greatly appreciated.
     
    FredC and Chris D like this.

Share This Page