I would like to extend Forum

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.
 
PHP:
if (!$parent instanceOf XenForo_ControllerResponse_View)
should be
PHP:
if (!($parent instanceof XenForo_ControllerResponse_View))
 
Hi Mr. Goodie2Shoes,

Thank you for the suggestion, but I'm afraid that doesn't appear to be the problem.
 
Last edited:
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);
 
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:
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?
 
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. :)
 
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;
	}
}
 
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;
	}
}
 
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');
 
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;
	}
}
 
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;
    }
}
 
Top Bottom