====== Friendica Addon development ======
Please see the sample addon ‘randplace’ for a working example of using some of these features. Addons work by intercepting event hooks - which must be registered. Modules work by intercepting specific page requests (by URL path).
===== Naming =====
Addon names are used in file paths and functions names, and as such: - Can’t contain spaces or punctuation. - Can’t start with a number.
===== Metadata =====
You can provide human-readable information about your addon in the first multi-line comment of your addon file.
Here’s the structure:
/**
* Name: {Human-readable name}
* Description: {Short description}
* Version: 1.0
* Author: {Author1 Name}
* Author: {Author2 Name} <{Author profile link}>
* Maintainer: {Maintainer1 Name}
* Maintainer: {Maintainer2 Name} <{Maintainer profile link}>
* Status: {Unsupported|Arbitrary status}
*/
You can also provide a longer documentation in a ''%%README%%'' or ''%%README.md%%'' file. The latter will be converted from Markdown to HTML in the addon detail page.
===== Install/Uninstall =====
If your addon uses hooks, they have to be registered in a ''%%_install()%%'' function. This function also allows to perform arbitrary actions your addon needs to function properly.
Uninstalling an addon automatically unregisters any hook it registered, but if you need to provide specific uninstallation steps, you can add them in a ''%%_uninstall()%%'' function.
The install and uninstall functions will be called (i.e. re-installed) if the addon changes after installation. Therefore your uninstall should not destroy data and install should consider that data may already exist. Future extensions may provide for “setup” amd “remove”.
===== PHP addon hooks =====
Register your addon hooks during installation.
\Friendica\Core\Hook::register($hookname, $file, $function);
''%%$hookname%%'' is a string and corresponds to a known Friendica PHP hook.
''%%$file%%'' is a pathname relative to the top-level Friendica directory. This //should// be ‘addon///addon_name/////addon_name//.php’ in most cases and can be shortened to ''%%__FILE__%%''.
''%%$function%%'' is a string and is the name of the function which will be executed when the hook is called.
==== Arguments ====
Your hook callback functions will be called with at least one and possibly two arguments
function _(App $a, &$b) {
}
If you wish to make changes to the calling data, you must declare them as reference variables (with ''%%&%%'') during function declaration.
=== $a ===
$a is the Friendica ''%%App%%'' class. It contains a wealth of information about the current state of Friendica:
* which module has been called,
* configuration information,
* the page contents at the point the hook was invoked,
* profile and user information, etc.
It is recommeded you call this ''%%$a%%'' to match its usage elsewhere.
=== $b ===
$b can be called anything you like. This is information specific to the hook currently being processed, and generally contains information that is being immediately processed or acted on that you can use, display, or alter. Remember to declare it with ''%%&%%'' if you wish to alter it.
===== Admin settings =====
Your addon can provide user-specific settings via the ''%%addon_settings%%'' PHP hook, but it can also provide node-wide settings in the administration page of your addon.
Simply declare a ''%%_addon_admin(App $a)%%'' function to display the form and a ''%%_addon_admin_post(App $a)%%'' function to process the data from the form.
===== Global stylesheets =====
If your addon requires adding a stylesheet on all pages of Friendica, add the following hook:
function _install()
{
\Friendica\Core\Hook::register('head', __FILE__, '_head');
...
}
function _head(App $a)
{
\Friendica\DI::page()->registerStylesheet(__DIR__ . '/relative/path/to/addon/stylesheet.css');
}
''%%__DIR__%%'' is the folder path of your addon.
===== JavaScript =====
==== Global scripts ====
If your addon requires adding a script on all pages of Friendica, add the following hook:
function _install()
{
\Friendica\Core\Hook::register('footer', __FILE__, '_footer');
...
}
function _footer(App $a)
{
\Friendica\DI::page()->registerFooterScript(__DIR__ . '/relative/path/to/addon/script.js');
}
''%%__DIR__%%'' is the folder path of your addon.
==== JavaScript hooks ====
The main Friendica script provides hooks via events dispatched on the ''%%document%%'' property. In your Javascript file included as described above, add your event listener like this:
document.addEventListener(name, callback);
* //name// is the name of the hook and corresponds to a known Friendica JavaScript hook.
* //callback// is a JavaScript anonymous function to execute.
More info about Javascript event listeners: https:%%//%%developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
=== Current JavaScript hooks ===
== postprocess_liveupdate ==
Called at the end of the live update process (XmlHttpRequest) and on a post preview. No additional data is provided.
===== Modules =====
Addons may also act as “modules” and intercept all page requests for a given URL path. In order for a addon to act as a module it needs to declare an empty function ''%%_module()%%''.
If this function exists, you will now receive all page requests for ''%%https://my.web.site/%%'' - with any number of URL components as additional arguments. These are parsed into the ''%%App\Arguments%%'' object. So ''%%https://my.web.site/addon/arg1/arg2%%'' would give this:
DI::args()->getArgc(); // = 3
DI::args()->get(0); // = 'addon'
DI::args()->get(1); // = 'arg1'
DI::args()->get(2); // = 'arg2'
To display a module page, you need to declare the function ''%%_content(App $a)%%'', which defines and returns the page body content. They may also contain ''%%_post(App $a)%%'' which is called before the ''%%_content%%'' function and typically handles the results of POST forms. You may also have ''%%_init(App $a)%%'' which is called before ''%%_content%%'' and should include common logic to your module.
===== Templates =====
If your addon needs some template, you can use the Friendica template system. Friendica uses [[http://www.smarty.net/|smarty3]] as a template engine.
Put your tpl files in the //templates/// subfolder of your addon.
In your code, like in the function addon_name_content(), load the template file and execute it passing needed values:
use Friendica\Core\Renderer;
# load template file. first argument is the template name,
# second is the addon path relative to friendica top folder
$tpl = Renderer::getMarkupTemplate('mytemplate.tpl', __DIR__);
# apply template. first argument is the loaded template,
# second an array of 'name' => 'values' to pass to template
$output = Renderer::replaceMacros($tpl, array(
'title' => 'My beautiful addon',
));
See also the wiki page [[docs:smarty3-templates]].
===== Current PHP hooks =====
==== authenticate ====
Called when a user attempts to login. ''%%$b%%'' is an array containing:
* **username**: the supplied username
* **password**: the supplied password
* **authenticated**: set this to non-zero to authenticate the user.
* **user_record**: successful authentication must also return a valid user record from the database
==== logged_in ====
Called after a user has successfully logged in. ''%%$b%%'' contains the ''%%$a->user%%'' array.
==== display_item ====
Called when formatting a post for display. $b is an array:
* **item**: The item (array) details pulled from the database
* **output**: the (string) HTML representation of this item prior to adding it to the page
==== post_local ====
Called when a status post or comment is entered on the local system. ''%%$b%%'' is the item array of the information to be stored in the database. Please note: body contents are bbcode - not HTML.
==== post_local_end ====
Called when a local status post or comment has been stored on the local system. ''%%$b%%'' is the item array of the information which has just been stored in the database. Please note: body contents are bbcode - not HTML
==== post_remote ====
Called when receiving a post from another source. This may also be used to post local activity or system generated messages. ''%%$b%%'' is the item array of information to be stored in the database and the item body is bbcode.
==== addon_settings ====
Called when generating the HTML for the addon settings page. ''%%$data%%'' is an array containing:
* **addon** (output): Required. The addon folder name.
* **title** (output): Required. The addon settings panel title.
* **href** (output): Optional. If set, will reduce the panel to a link pointing to this URL, can be relative. Incompatible with the following keys.
* **html** (output): Optional. Raw HTML of the addon form elements. Both the ''%%