• 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

AndyB

Well-known member
#1
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.
 

AndyB

Well-known member
#3
Hi Mr. Goodie2Shoes,

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

truonglv

Formerly Nobita.Kun
#6
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

Well-known member
#7
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:

Chris D

XenForo developer
Staff member
#8
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

Well-known member
#9
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. :)
 

AndyB

Well-known member
#10
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;
	}
}
 

AndyB

Well-known member
#12
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;
	}
}
 

Chris D

XenForo developer
Staff member
#13
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

Well-known member
#14
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;
	}
}
 

Chris D

XenForo developer
Staff member
#15
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;
    }
}