• This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

How to show a forum in a page

Status
Not open for further replies.

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:


2.webp


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.
 
Step 2 - Create the page

To create a new page go to AdminCP -> Applications -> Node Tree -> Create New Page. Fill with this informations (or anything you want. The items with red is required to be exactly the same as I wrote in here, so this tutorial will work for you):

Tab - Basic Information
URL Portion:: showsaforum
Title: Showing a Forum
Description: This page shows a forum.
Parent Node: (root node)
Display Order: 1
Display in the node list: 1
Override user style choice: Unchecked

Tab - Page options
Template HTML:
Code:
<xen:require css="discussion_list.css" />
<div class="discussionList">
{xen:raw $forum}
</div>

Explaining: You'll maybe do not understand this now, but what we are doing is require the .css file for the thread listing, and inside a div we raw the parameter $forum. After calling our function we get the response and use this response to create a new template. This template will be set as the $forum paremeter.

Optional Components: Do not need to check anything.

Tab - PHP Callback
PHP Callback: ShowAForum::showAForumInPage


Save it!
 
Step 3 - Testing

To test, go to your forum list and search for the recente created page. It will be there:

1.webp

Click on it and the forum that you choose will be displayed on the page:

2.webp

The code to call another action in another controller was made by me. This codes really works well. If anyone that knows a little more could contribute to make it better, I would really appreciate that.
 
Tips:
1: You can set thread list to only display threads of a specific prefix using this:
PHP:
$request->setParam('prefix_id', 1);

Which 1 is the ID of the prefix.

2: If you only want to show sticky threads add this code:
PHP:
$controllerResponse->params['threads'] = array();

3: If you only want to show normal threads (not sticky) add this code:

PHP:
$controllerResponse->params['stickyThreads'] = array();
 
Fuhrmann you are WONDERFUL. Also very quick. You only said you'd do this an hour or so ago!

# I have sent you a necessary change by Conversation
- the php file needs to be created and uploaded to the site BEFORE the XF Page is edited to use it.
So the two sections of instruction need to be reversed in order.
# I would find it easier as your student if there is a part one, how to;
then part two for explanations of what the code does.
# I find it nuts that XF doesn't show me the node_id in the admincp anywhere!
This was disconcerting so I added a note for you on how babies like me can find it. It caused me a headache and much searching in different places. XF needs to add this to the admincp display on Edit Page I think.


COSMETIC TWEAKS
The code does not display the title of the forum. I've dealt with that for now by adding the title in html above it.
So it displays like this which is fine:
forumcall2.webp

But what I really want to display is this - from the node tree, wuith a blank cat bar:
forumcall3.webp
I think this is neater for when there is no new post and for Announcements this iso ften going to be the case.
Can you magic that when you have had a rest. I know a blank cat bar can display on a page OK.
When possible I also want an indicator to flag up if there is a new post in this forum since last visit. A graphic like the one on the right.


Lastly I assume if I want to add more than one forum to a Page I make it like this:
A. Create a copy of the php file and name it ShowAForum32
(32 matching the node_id of the forum might be useful)
Edit the node_id line to match the forum's node_id
B. In admincp/ Node Tree/ Mypage/ Edit Page
I add the snippet to the html box as before, in the second position I want a forum to appear
- how does the code know WHICH of the php files to use in this location?
I add a different callback?
PHP Callback: ShowAForum32::showAForumInPage
 
In the PHP Callback the first name is the class, which is the name of the file: ShowAForum32.php
And the second is the name of the function, which in your example is: showAForumInPage.

Thank you for the tips, I'll try to change the steps.
 
this is fantastic
something we used to do in vb to somewhat decent results
this + the xenporta stuff + a little imagination....
 
Hi,
great tutorial :D
This is great for my stuff, I just want to know if there is an easy way to duplicate che params: like $forum and $forum2 to print 2 forums in the same page.
Thanks in adavance
 
Hi,
great tutorial :D
This is great for my stuff, I just want to know if there is an easy way to duplicate che params: like $forum and $forum2 to print 2 forums in the same page.
Thanks in adavance

Yes, you can just repeat the same step from the beggining just chaging the names of the variables.
 
PHP:
/* Set the param 'forum' with the rendered template so we can use in our page */
        $response->params['forum'] = $threadList;

Just change this one or some other codes too?

PHP:
/* Set the param 'forum' with the rendered template so we can use in our page */
        $response->params['forum2'] = $threadList;
 
Is it possible to make this external so you can use it outside the forum as well?
Well, you can use Jake's Addon: Nodes as Tabs to
(1) display a page as a Tab
(2) display a page (which has a forum) as a Tab
(3) display a forum as a tab (no page involvement).

So I'd say, YES !
Addons are easy to find now. Click on the Resources Tab. If you have problems finding an addon ... please leave feedback about the Resource Manager and what's didn't work for you in a certain situation.
 
Well, you can use Jake's Addon: Nodes as Tabs to
(1) display a page as a Tab
(2) display a page (which has a forum) as a Tab
(3) display a forum as a tab (no page involvement).

So I'd say, YES !
Addons are easy to find now. Click on the Resources Tab. If you have problems finding an addon ... please leave feedback about the Resource Manager and what's didn't work for you in a certain situation.

Hmm I don't think you understood I was thinking more of creating an own function that fetches this from the XenForo_Model_Forum or something in that fashion :P
 
Hmm I don't think you understood I was thinking more of creating an own function that fetches this from the XenForo_Model_Forum or something in that fashion :p
Oh yea, you are forum connector guy.
:)
Hopefully that thread helped you.
You would have never found it by searching.
heh.
 
What exactly do you want do to? Maybe I can help! :)
Oh well what my general idea is I want to connect my website to the forum so I can fetch all threads in a specific forum, so I can show news on my website. I tried to look at making my website a template but it's a lot to get into and maybe at later state I will try to make it one. My biggest problem is I cannot figure out how all these connectors and methods work together because there is no real guide to it (I don't think XF had this in mind when they first developed this ^^)

Hope you get the idea otherwise ask again :)
 
Oh well what my general idea is I want to connect my website to the forum so I can fetch all threads in a specific forum, so I can show news on my website. I tried to look at making my website a template but it's a lot to get into and maybe at later state I will try to make it one. My biggest problem is I cannot figure out how all these connectors and methods work together because there is no real guide to it (I don't think XF had this in mind when they first developed this ^^)

Hope you get the idea otherwise ask again :)

Have you already seen this?
http://xenforo.com/community/resources/xenforo-connector.202/
 
Status
Not open for further replies.
Top Bottom