Part 1
This walk-through will take you step-by-step through the process of creating a working XenForo Add-on.
The Add-on created will allow Admins to prevent the editing of signatures for members with less than a specified amount of posts, and who belong to a particular group. To accomplish this we will need to set up a Listener, an Admin Options Page, and create the PHP files.
I decided to not use screen shots in this walk-through as I want to give the feel that you already know XenForo's Model View Controller (MVC) concept and are about to write your Add-on. Each step will be explained in detailed.
The working Add-on will be available at the end of this walk-through for coders to reference, or for licensed members to use.
If you plan on building the Add-on step by step you will need to enable Debug Mode. It is recommended that you first make a back-up of your config.php file before editing it. To enable debug mode:
open: yourForo -> library -> config.php
And add at the bottom: $config['debug'] = true;
At this time you may also want to add:
// $config['enableListeners'] = false;
Save your config.php file.
Creating the Add-on
Log into your AdminCP, and navigate to: Development -> Create Add-on
The following fields for this Add-on need to be filled with the following information:
Add-on ID: limitSigs
Title: Limit Signatures for new Members
Version String: 1.0
Version ID: 1
The Version ID is used internally to keep track of revisions. Increase this number for each revision.
The rest of the fields we do not need for this Add-on.
Click the Save Add-on button.
As the purpose of our Add-on is to not allow new members to edit (create) a signature, we must locate where members can edit their signature within XenForo, so we can Listen for the class being called. This is handled by a Controller. There are four Controller directories: Admin, Helper, Public, and Response. Editing a signature is not an Admin function. Nor is it a Helper, or a Response, that leaves us with Public. From your filemanager navigate to ControllerPublic. As editing a signature is part of a members account look for Account.php. Open this file and search for signature. The first word highlighted is just above: public function actionSignature(). This is the function we want to override.
Now we must listen for the class which this function belongs to be called (XenForo_ControllerPublic_Account).
XenForo comes with quite a few Listeners that we can choose from. Navigate to: Development -> Code Event Listeners. Click on the Create New Code Event Listener.
The first drop down allows you to select which event to listen to. As we want to listen to a class from the controller being loaded, from the drop down, select Load_Class_Controller.
The load_class_controller listener is tricky, but it is powerful and provides the most flexibility of all the Listeners. When you select this listener you will notice in the explanation of the load_class_controller that the class must be defined as: class My_Class_Name extends XFCP_My_Class_Name. This is true, but it can not be loaded directly. Loading this class directly will result in the AutoLoader to generate this exception: Cannot load class using XFCP. Load the class using the correct loader first.
What the error means is that the new class you want to create must be loaded before it can be resolved. But the question is, where does it get loaded, and how? This is the purpose of the $extend array argument.
This walk-through will take you step-by-step through the process of creating a working XenForo Add-on.
The Add-on created will allow Admins to prevent the editing of signatures for members with less than a specified amount of posts, and who belong to a particular group. To accomplish this we will need to set up a Listener, an Admin Options Page, and create the PHP files.
I decided to not use screen shots in this walk-through as I want to give the feel that you already know XenForo's Model View Controller (MVC) concept and are about to write your Add-on. Each step will be explained in detailed.
The working Add-on will be available at the end of this walk-through for coders to reference, or for licensed members to use.
If you plan on building the Add-on step by step you will need to enable Debug Mode. It is recommended that you first make a back-up of your config.php file before editing it. To enable debug mode:
open: yourForo -> library -> config.php
And add at the bottom: $config['debug'] = true;
At this time you may also want to add:
// $config['enableListeners'] = false;
Save your config.php file.
Creating the Add-on
Log into your AdminCP, and navigate to: Development -> Create Add-on
The following fields for this Add-on need to be filled with the following information:
Add-on ID: limitSigs
Title: Limit Signatures for new Members
Version String: 1.0
Version ID: 1
The Version ID is used internally to keep track of revisions. Increase this number for each revision.
The rest of the fields we do not need for this Add-on.
Click the Save Add-on button.
As the purpose of our Add-on is to not allow new members to edit (create) a signature, we must locate where members can edit their signature within XenForo, so we can Listen for the class being called. This is handled by a Controller. There are four Controller directories: Admin, Helper, Public, and Response. Editing a signature is not an Admin function. Nor is it a Helper, or a Response, that leaves us with Public. From your filemanager navigate to ControllerPublic. As editing a signature is part of a members account look for Account.php. Open this file and search for signature. The first word highlighted is just above: public function actionSignature(). This is the function we want to override.
Now we must listen for the class which this function belongs to be called (XenForo_ControllerPublic_Account).
XenForo comes with quite a few Listeners that we can choose from. Navigate to: Development -> Code Event Listeners. Click on the Create New Code Event Listener.
The first drop down allows you to select which event to listen to. As we want to listen to a class from the controller being loaded, from the drop down, select Load_Class_Controller.
The load_class_controller listener is tricky, but it is powerful and provides the most flexibility of all the Listeners. When you select this listener you will notice in the explanation of the load_class_controller that the class must be defined as: class My_Class_Name extends XFCP_My_Class_Name. This is true, but it can not be loaded directly. Loading this class directly will result in the AutoLoader to generate this exception: Cannot load class using XFCP. Load the class using the correct loader first.
What the error means is that the new class you want to create must be loaded before it can be resolved. But the question is, where does it get loaded, and how? This is the purpose of the $extend array argument.