XF 2.3 Thread Types

Lee

Well-known member
I’m looking to create a new thread type.

Can somebody help me in understanding the steps required to implement a new thread type?

I have inserted it as a thread type in the database

I have also created a thread type handler for my thread type.

How do I get it to be recognised by XenForo?
 
Are you talking about having it appear in the post thread options and in the node edit options?
To get it like this, you need to do a few things.

First off make sure your thread type is being registered correctly.
PHP:
    public function installStep1(): void
    {
        $this->insertThreadType('test', 'Vendor\Addon:Test', 'Vendor/Addon');
    }
What this does is inserts that data into the database and rebuilds the rebuildThreadTypeCache

Before carrying on check the database table xf_thread_type that your new type exists, just to make sure.

Next you'll want to add your thread type handler
PHP:
<?php

namespace Vendor\Addon\ThreadType;

use XF\ThreadType\AbstractHandler;

class TestHandler extends AbstractHandler
{
    public function getTypeIconClass(): string
    {
        return 'fa-user';
    }
}
I don't know a way to make sure this is confirmed working until the next stage which is adding your thread type to existing forum types.
Unless you want to make your own forum type but thats an entirely different thing.

Now make an extension class for the forum type you want to add it to. I believe this only works on the src/XF/ForumType/DiscussionHandler.php though so if you wannt use a different forum type your on your own :P
PHP:
<?php

namespace Vendor\Addon\XF\ForumType;

use XF\Entity\Forum;

class DiscussionHandler extends \XF\ForumType\DiscussionHandler
{
    public function getPossibleCreatableThreadTypes(Forum $forum): array
    {
        return ['article', 'poll', 'question', 'test'];
    }
}

Finally register this with your Admin control panel
Code:
Base class name: XF\ForumType\DiscussionHandler
Extension class name: Vendor\Addon\XF\ForumType\DiscussionHandler

You should now see them here when you go and edit what thread a node can use:
1757700019971.webp

And when you have added that thread type to a node in an post selector
1757700003333.webp

Hope this is what your looking for :)
 
Are you talking about having it appear in the post thread options and in the node edit options?
To get it like this, you need to do a few things.

First off make sure your thread type is being registered correctly.
PHP:
    public function installStep1(): void
    {
        $this->insertThreadType('test', 'Vendor\Addon:Test', 'Vendor/Addon');
    }
What this does is inserts that data into the database and rebuilds the rebuildThreadTypeCache

Before carrying on check the database table xf_thread_type that your new type exists, just to make sure.

Next you'll want to add your thread type handler
PHP:
<?php

namespace Vendor\Addon\ThreadType;

use XF\ThreadType\AbstractHandler;

class TestHandler extends AbstractHandler
{
    public function getTypeIconClass(): string
    {
        return 'fa-user';
    }
}
I don't know a way to make sure this is confirmed working until the next stage which is adding your thread type to existing forum types.
Unless you want to make your own forum type but thats an entirely different thing.

Now make an extension class for the forum type you want to add it to. I believe this only works on the src/XF/ForumType/DiscussionHandler.php though so if you wannt use a different forum type your on your own :P
PHP:
<?php

namespace Vendor\Addon\XF\ForumType;

use XF\Entity\Forum;

class DiscussionHandler extends \XF\ForumType\DiscussionHandler
{
    public function getPossibleCreatableThreadTypes(Forum $forum): array
    {
        return ['article', 'poll', 'question', 'test'];
    }
}

Finally register this with your Admin control panel
Code:
Base class name: XF\ForumType\DiscussionHandler
Extension class name: Vendor\Addon\XF\ForumType\DiscussionHandler

You should now see them here when you go and edit what thread a node can use:
View attachment 326967

And when you have added that thread type to a node in an post selector
View attachment 326966

Hope this is what your looking for :)
Amazing. This is exactly what I was looking for. Thanks so much.
 
Are you talking about having it appear in the post thread options and in the node edit options?
To get it like this, you need to do a few things.

First off make sure your thread type is being registered correctly.
PHP:
    public function installStep1(): void
    {
        $this->insertThreadType('test', 'Vendor\Addon:Test', 'Vendor/Addon');
    }
What this does is inserts that data into the database and rebuilds the rebuildThreadTypeCache

Before carrying on check the database table xf_thread_type that your new type exists, just to make sure.

Next you'll want to add your thread type handler
PHP:
<?php

namespace Vendor\Addon\ThreadType;

use XF\ThreadType\AbstractHandler;

class TestHandler extends AbstractHandler
{
    public function getTypeIconClass(): string
    {
        return 'fa-user';
    }
}
I don't know a way to make sure this is confirmed working until the next stage which is adding your thread type to existing forum types.
Unless you want to make your own forum type but thats an entirely different thing.

Now make an extension class for the forum type you want to add it to. I believe this only works on the src/XF/ForumType/DiscussionHandler.php though so if you wannt use a different forum type your on your own :P
PHP:
<?php

namespace Vendor\Addon\XF\ForumType;

use XF\Entity\Forum;

class DiscussionHandler extends \XF\ForumType\DiscussionHandler
{
    public function getPossibleCreatableThreadTypes(Forum $forum): array
    {
        return ['article', 'poll', 'question', 'test'];
    }
}

Finally register this with your Admin control panel
Code:
Base class name: XF\ForumType\DiscussionHandler
Extension class name: Vendor\Addon\XF\ForumType\DiscussionHandler

You should now see them here when you go and edit what thread a node can use:
View attachment 326967

And when you have added that thread type to a node in an post selector
View attachment 326966

Hope this is what your looking for :)
@QuackieMackie Would you be so kind as to guide me through the steps to creating a new forum type so I can make these threads exclusive to a specific node with no other threads?
 
@QuackieMackie Would you be so kind as to guide me through the steps to creating a new forum type so I can make these threads exclusive to a specific node with no other threads?
Sure no problem :)

So creating a forum type is pretty similar to a thread type. Before I begin, let me just mention I added this method to my ThreadType:
PHP:
<?php

namespace Vendor\Addon\ThreadType;

use XF\Entity\Thread;
use XF\ThreadType\AbstractHandler;

class TestHandler extends AbstractHandler
{
    public function getTypeIconClass(): string
    {
        return 'fa-user';
    }

    public function onThreadPreSave(Thread $thread, bool $isTypeEnter): void
    {
        parent::onThreadPreSave($thread, $isTypeEnter);
        \XF::logError("TestHandler: Wow a thread has been saved!");
    }
}
All it does is create a log when the thread gets edited, pretty handy that it's a default method.


Making a forum type
First off you need to go into your setup.php and add this handy dandy line:
PHP:
    public function installStep2(): void
    {
        $this->insertForumType('test', 'Vendor\Addon:Test', Vendor/Addon');
    }

What this is going to do is insert a new enrty in the table xf_forum_type. You can see this is working if you go and try and make a node. It will mention soming along the lines of Could not find "Vendor\Addon\ForumType\Handler".

Next we're going to create the handler.
PHP:
<?php

namespace Vendor\Addon\ForumType;

use XF\Entity\Forum;
use XF\ForumType\AbstractHandler;

class TestHandler extends AbstractHandler
{
    public function getDefaultThreadType(Forum $forum): string
    {
        return 'test';
    }

    public function getDisplayOrder(): int
    {
        return 1;
    }

    public function getTypeIconClass(): string
    {
        return 'fa-user';
    }
}

What we're doing here is just adding the basic methods that are needed to function. But what if we want to allow another thread type? The method getDefaultThreadType works fine for one. But if we want to use multiple we just need to add this line:
PHP:
    public function getExtraAllowedThreadTypes(Forum $forum): array
    {
        return ['test', 'discussion'];
    }

If you've followed this exactly you should see after posting a thread with your type TestHandler: Wow a thread has been saved! in your error log.



Now just a heads up, this won't make thread types pop up under here, so you can select which ones you want available for that thread.
1757754324026.webp
To do that i'd suggest looking in the ForumType/DiscussionHandler, I think the methods you'd need to add are there, but I did only gloss over it.
 
Back
Top Bottom