XF 2.2 How to add template content by plugin/addon to the main forum page(es)?

G00r

New member
Hey guys!

From vbulletin 4 comin, i did my first steps in xenforo by checking out the portal addon demo and watching the YT videos done by kier. I came very far on my first addon, but i hit a wall on the question on how to add template content to the existing forums main pages without altering existing templates and code.

So, is there even a way to insert a custom templates content by addon?
If so, a hint to the right direction would be nice. Some example code even better.

I would like to add content before the forums list and at the top of each forums. In vbulletin 4 there was a hook system, is there something in xenforo to achieve more or less the same?

Thanks!
 

G00r

New member
Thank you, that was easy!

The hard part seems to be to get some SQL data involved. Did try a callback, but only was able to get a string return working. I don't even know if it is possible that way. There is not much information or example code to find on that.

Before i waste more on try and error ... is there some doc about this?

My Listener.php

PHP:
<?php

namespace TestProjects\Test;

use \XF\Pub\Controller\AbstractController;


class Listener
{
    public static function viewData(AbstractController $controller, $params)
    {
        $viewParams = [
            'title' => 'this is a test',
            'test'  => 'more testing'
        ];

        return $controller->view('TestProjects\Test:Test\Index', 'tp_test_index', $viewParams);
    }    
}

Template Compilation Error: Listener::viewData(): Argument #1 ($controller) must be of type XF\Pub\Controller\AbstractController, string given
 

G00r

New member
The examples and tutorials only are for new pages, new addons. But how can i add aditional content to the template forum_list aka the forum index page? I know, as Brogan wrote, i can use template modifications and that is easy and quick for static content. But how can i add dynamic content (SQL based) to it? I thought a callback would be the way and thats what i am trying here in this Listener.php example.

So i basicly have to find out witch is the controler for the froums index and action, so i can extend it in my Listener.php?

To make it quit and easy to understand, a simple example would be nice. I guess i am not the only and first one who aims for a modification by addon for the existing forums content. And certainly not the last. Thanks!
 

Jeremy P

XenForo developer
Staff member
So i basicly have to find out witch is the controler for the froums index and action, so i can extend it in my Listener.php?
You need to find out which controller and action correspond to the page, yes. In debug mode, you can view this information by hovering over the cog icon on the bottom right of the page. You then use the class extension system (instead of Listener.php, which is only for code event listeners) to extend this class with your custom code.

These pages outline the class extension system and how it works, and the latter contains a code sample for extending a controller action:

 

G00r

New member
Thank you for your help but some how i don't get it to work.

PHP:
<?php

namespace TestProjects\Test;

use XF\Mvc\ParameterBag;
use XF\Pub\Controller\Forum;

class Test extends Forum
{
    // XF\Pub\Controller\Forum :: actionList · forum_list

    public function actionList(ParameterBag $params)
    {
        print('<h1>die for nothing!!!</h1>');
        die();

        $viewParams = [

            'title' => 'this is a test',
            'test' => 'more testing'
        ];

        // return $this->view('TestProjects\Test:Test\Index', 'tp_test_index', $viewParams);
        return $this->view('XF\Pub\Controller\Forum', 'forum_list', $viewParams);
    }
}

Nothing happens. I guess i still don't understand the way it works.
 

nocte

Well-known member
Nothing happens. I guess i still don't understand the way it works.
Have you added a class extension in the admin cp?

typically the namespace should be for that file:

namespace TestProjects\Test\XF\Pub\Controller;

the file should be located in src/addons/TestProjects/Test/XF/Pub/Controller/Forum.php then.

you should use the class proxy; the class should have the name of the class you are extending:

class Forum extends XFCP_Forum
 

G00r

New member
Thaks! Got a working code now. It is kinda hard to understand how it works, because there are lots of small but very important steps to do.

My working example looks like this:

PHP:
<?php

namespace TestProjects\Test\XF\Pub\Controller;

use XF\Mvc\ParameterBag;
use XF\Pub\Controller\Forum;

class Test extends Forum
{
    public function actionList(ParameterBag $params)
    {
        $replay = parent::actionList($params);

        $viewParams = [

            'title' => 'this is a test',
            'test' => 'more testing'
        ];

        if($replay instanceof \XF\Mvc\Reply\View)
        {
            $replay->setParams($viewParams, true);
        }

        return $replay;
    }
}
 
Top