XenForo 2 add-on development reference

Konstantin Bogomolov
3 min readMay 20, 2020

Here is a short reference of some points in source code, which should help you to create XenForo 2 add-on. At first, I recommend you follow the official tutorial with an add-on sample.
You can find it here:
https://xenforo.com/xf2-docs/dev/lets-build-an-add-on/
Below you will find additional information I discovered during writing my add-on.

Prepare environment

I usually use Docker for such kind of app. I worked on an app with Memcached, so you will see it in the files below.
Here is the content of Dockerfile:

FROM php:7.2-apache

RUN apt-get update \
&& apt-get install -y \
zip \
zlib1g-dev \
libz-dev \
libmemcached-dev \
libpng-dev \
libfreetype6-dev \
libjpeg62-turbo-dev \
libgd-dev

RUN a2enmod rewrite \
&& pecl install memcache xdebug-2.6.1 \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install gd mysqli zip \
&& docker-php-ext-enable memcache xdebug \
&& echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini

And the content of docker-compose.yml file:

version: '3'

services:
php:
build: ./docker/php #path to
container_name: php_container
environment:
XDEBUG_CONFIG: "remote_host=172.17.0.1 remote_port=9000"
PHP_IDE_CONFIG: serverName=localhost
volumes:
- ./app:/var/www/html
ports:
- 80:80

mariadb:
image: mariadb:10.4
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: database_name
volumes:
- db:/var/lib/mysql
ports:
- 3306:3306

memcache:
image: memcached:alpine
ports:
- 11211:11211

volumes:
db:

XenForo 2 console commands

Enter a Docker container with command:

docker exec -it -u www-data php_container bash

Then you can list all commands and filter them:

$ php cmd.php | grep addon

Add-on commands located in src/XF/Cli/Command/AddOn folder.

Add-on data

All add-ons stored in src/addons folder and xf_addon table in the database.

Setup script

If you change tables with alters, use available data types from here: src/XF/Db/Schema/Column.php::type.
Change tables with SchemaManager. See available methods in src/XF/Db/SchemaManager.php, i.e. tableExists(), columnExists() etc.

Extend entity

The simplest way to extend an entity is to add it in the admin panel. See DevelopmentClass extensions menu in the admin control panel.
It will write data to xf_class_extension table. Classes extensions will be processed here: src/XF/Extension.php::extendClass. XenForo extends with class_alias PHP function. As a parent class, you should specify a class name with "XFCP_" prefix, see how it works here: forum/src/XF/Extension.php:243 ( resolveExtendedClassToRoot method).

Event listeners

XenForo 2 sends a lot of events through request processing. See the list of events and event listeners in DevelopmentCode events and Code event listeners in the admin control panel. Data will be stored in database in tables xf_code_event and xf_code_event_listener.
In order to see all events, you could use Xdebug. Set a breakpoint in src/XF/Extension.php::fire function and see event names and hints which you could use.

Use composer packages

Here is a good tutorial on how to use it:
https://xenforo.com/community/resources/using-composer-packages-in-xenforo-2-0-addons-tutorial.6588/

Template functions

See the list of default functions you can use in templates in file src/XF/Template/Templater.php:152 (variable $defaultFunctions).

Originally published at https://bogomolov.tech.

--

--