diff --git a/docs/devs/advanced-concepts/thread-and-forum-types.md b/docs/devs/advanced-concepts/thread-and-forum-types.md new file mode 100644 index 0000000..211aad0 --- /dev/null +++ b/docs/devs/advanced-concepts/thread-and-forum-types.md @@ -0,0 +1,177 @@ +# Thread & forum types +Threads and forums are a huge part of Xenforo. +This page will cover the basics of how you can set up your own custom thread and forum type, and how you can give it basic unique functionality. + +## Registering new types +Xenforo stores each type of thread and forum in the database table `xf_thread_type` and `xf_forum_type`. +What we need to do is add a new row to these tables for each new type we want to create and then refresh the cache: `rebuildThreadTypeCache`, and `rebuildForumTypeCache`. + +To do this we can use built in methods in our `setup.php` +```php title="setup.php" +insertThreadType('thread_type_identifier', 'Vendor\Addon:Example', 'Vendor/Addon'); + } + + public function installStep2(): void + { + $this->insertForumType('forum_type_identifier', 'Vendor\Addon:Example', 'Vendor/Addon'); + } +} +``` +What this will do is insert a new row into the `xf_thread_type` and `xf_forum_type` tables with the given identifier, handler and add-on. + +The parameters for `insertThreadType` and `insertForumType` are: + +- `identifier` - The identifier of the thread or forum type. This is a string you will be using to refer to the type in your code. +- `handler` - The handler class for the thread or forum type. This can be in the format `Vendor\Addon:Class` or a full path to the class `Vendor\Addon\ForumType\ExampleHandler`. +- `addon_id` - The add-on ID of the thread or forum type. This is the ID of the add-on that the thread or forum type belongs to. +- `rebuildCache` - Whether to rebuild the cache. This is set to `true` by default. + +### What is a handler? +A handler is a class that is responsible for handling the logic of a specific type of thread or forum. +You can see Xenforo's default handlers in the `XF\ThreadType\*` and `XF\ForumType\*` classes. + +You will always want to extend the `XF\ThreadType\AbstractHandler` or `XF\ForumType\AbstractHandler` class when making your own handler. + +## Creating type handlers +What is a handler responsible for? + +A handler is responsible for handling the logic of a specific type of thread or forum. + +### Thread type handlers +To create a thread type handler, you will need to create a new class that extends `XF\ThreadType\AbstractHandler`. +In your add-ons folder you will need to create a new folder called `ThreadType` and then create a new class called `ExampleHandler`. + +```php title="ThreadType/ExampleHandler.php" +db()->delete('xf_thread_type', 'thread_type_id = ?', 'test'); + + $iconRepo = $this->app->repository(IconRepository::class); + $iconRepo->enqueueUsageAnalyzer('thread_type'); + + \XF::runOnce('rebuildThreadTypeCache', function () + { + $this->app->repository(ThreadTypeRepository::class)->rebuildThreadTypeCache(); + }); + } + + public function uninstallStep2(): void + { + $this->db()->delete('xf_forum_type', 'forum_type_id = ?', 'test'); + + $iconRepo = $this->app->repository(IconRepository::class); + $iconRepo->enqueueUsageAnalyzer('forum_type'); + + \XF::runOnce('rebuildForumTypeCache', function () + { + $this->app->repository(ForumTypeRepository::class)->rebuildForumTypeCache(); + }); + } +``` + +This will remove the row from the `xf_thread_type` and `xf_forum_type` tables and then rebuild the cache. \ No newline at end of file diff --git a/sidebars.ts b/sidebars.ts index da24714..1267e1f 100644 --- a/sidebars.ts +++ b/sidebars.ts @@ -329,6 +329,13 @@ const sidebars: SidebarsConfig = { 'devs/criteria', 'devs/managing-the-schema', 'devs/lets-build-an-add-on', + { + type: 'category', + label: 'Advanced concepts', + items: [ + 'devs/advanced-concepts/thread-and-forum-types', + ], + }, { type: 'category', label: 'Appendix',