Fuhrmann
Well-known member
This tutorial will explain how to show a forum (thread list) inside a page. I'll not explain why this, or why that, but all the code necessary is commented.
And this is the final result:
I strongly recommend you to read my others tutorials to know better how XenForo works:
How to show a forum in a page
How to read and write into the database (with a page)
How to create your own helpers
Creating a add-on to insert tabs in profile page (using hooks)
[part 2] Creating a add-on to insert tabs in profile page (using hooks)
[part 3] Creating a add-on to insert tabs in profile page (using hooks)
[part 4] Creating a add-on to insert tabs in profile page (using hooks)
How to create a cron entry to move threads (with options!)
How to use tabs to separate your add-on options
How to add a new sidebar in the forum list
Step 1 - Creating the code
The following code will be responsable for making a request to an action of the controller calledXenForo_ControllerPublic_Forum. This action is the actionIndex and you can take a look at it and see what it does opening the file library/XenForo/ControllerPublic/Forum.php and going to the function actionIndex.
The action get a list of threads in a specific forum, and do others things like checking permissions, ordering threads and etc.
Create a new file inside your_forum_root/library and name it as ShowAForum.php.
Open this file and paste the following code:
Simple! But what it does? Well, I'll try to explain:
This function showAForumInPage uses two parameters: $controller (the controller of our page) and $response (the response of our page). We'll be using just the $response, to set a custom parameter. But how to get a list of threads?
Since XenForo already do that (you can see that visiting any forum, you'll get a list of threads), we'll just call to the existing controller and action to get all the threads that we want to display. After that we just set the numbers of threads that we want to show (if we want to limit) and set others things like not showing the total numbers of threads in the template (XenForo does that in the thread_list template).
We set which forum we want to show using this piece of code:
The number 2 is the ID of the forum.
Save the file.
And this is the final result:
I strongly recommend you to read my others tutorials to know better how XenForo works:
How to show a forum in a page
How to read and write into the database (with a page)
How to create your own helpers
Creating a add-on to insert tabs in profile page (using hooks)
[part 2] Creating a add-on to insert tabs in profile page (using hooks)
[part 3] Creating a add-on to insert tabs in profile page (using hooks)
[part 4] Creating a add-on to insert tabs in profile page (using hooks)
How to create a cron entry to move threads (with options!)
How to use tabs to separate your add-on options
How to add a new sidebar in the forum list
Step 1 - Creating the code
The following code will be responsable for making a request to an action of the controller calledXenForo_ControllerPublic_Forum. This action is the actionIndex and you can take a look at it and see what it does opening the file library/XenForo/ControllerPublic/Forum.php and going to the function actionIndex.
The action get a list of threads in a specific forum, and do others things like checking permissions, ordering threads and etc.
Create a new file inside your_forum_root/library and name it as ShowAForum.php.
Open this file and paste the following code:
PHP:
<?php
class ShowAForum
{
public static function showAForumInPage(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response)
{
/* Get the visitor param */
$visitor = XenForo_Visitor::getInstance();
/* Create a new request */
$request = new Zend_Controller_Request_Http();
/* The ID of the forum to show. In this example, we'll show the forum with the ID 2*/
$request->setParam('node_id', 2);
/* Create a new response */
$responseForum = new Zend_Controller_Response_Http();
/* New RouteMatch to use when instance the new ControllerPublic */
$routeMatch = new XenForo_RouteMatch();
/* The controller that holds the action that we want to call: XenForo_ControllerPublic_Forum */
$controllerForum = new XenForo_ControllerPublic_Forum($request, $responseForum, $routeMatch);
/* Pre Dispatch the controller with the actionIndex */
$controllerForum->preDispatch('index');
/* Call the actionIndex in the Controller. This action show a list of threads. (that's what we want) */
$controllerResponse = $controllerForum->{'actionIndex'}();
/* Set the param called 'visitor' in the controller response. This param is used in the thread_list template. */
$controllerResponse->params['visitor'] = $visitor->toArray();
/*
Limit the numbers of thread. In this example we set to onyl show 4 threads: 0,1,2,3,4
The actionIndex in the XenForo_ControllerPublic_Forum will get all threads that it have to get, but now we can limit
*/
$threadsToShow = 4;
$controllerResponse->params['threads'] = array_slice($controllerResponse->params['threads'], 0, $threadsToShow - 1);
/* Unset the param 'totalThreads' so the numbers of totalThreads will not show in the template */
unset($controllerResponse->params['totalThreads']);
/* Create the template to show the threads, with all the params requireds */
$threadList = new XenForo_Template_Public('thread_list', $controllerResponse->params);
/* Set the param 'forum' with the rendered template so we can use in our page */
$response->params['forum'] = $threadList;
/* return the original response to the page, with the new param 'forum' */
return $response;
}
}
?>
Simple! But what it does? Well, I'll try to explain:
This function showAForumInPage uses two parameters: $controller (the controller of our page) and $response (the response of our page). We'll be using just the $response, to set a custom parameter. But how to get a list of threads?
Since XenForo already do that (you can see that visiting any forum, you'll get a list of threads), we'll just call to the existing controller and action to get all the threads that we want to display. After that we just set the numbers of threads that we want to show (if we want to limit) and set others things like not showing the total numbers of threads in the template (XenForo does that in the thread_list template).
We set which forum we want to show using this piece of code:
PHP:
$request->setParam('node_id', 2);
The number 2 is the ID of the forum.
Save the file.