From be348cf8c811f8a231d28753ca34abb772c8dd8d Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Mon, 24 Sep 2018 08:16:26 -0400 Subject: [PATCH] Remove addon_hooks template variable - Update addon documentation --- doc/Addons.md | 77 +++++++++++++++++------------- view/templates/head.tpl | 5 -- view/theme/frio/templates/head.tpl | 5 -- 3 files changed, 43 insertions(+), 44 deletions(-) diff --git a/doc/Addons.md b/doc/Addons.md index b8364d9353..d432a9bcbe 100644 --- a/doc/Addons.md +++ b/doc/Addons.md @@ -67,47 +67,61 @@ $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. -## JavaScript addon hooks +## Global stylesheets -### PHP part - -Make sure your JavaScript addon file (addon/*addon_name*/*addon_name*.js) is listed in the document response. - -In your addon install function, add: +If your addon requires adding a stylesheet on all pages of Friendica, add the following hook: ```php -Addon::registerHook('template_vars', __FILE__, '_template_vars'); -``` - -In your addon uninstall function, add: - -```php -Addon::unregisterHook('template_vars', __FILE__, '_template_vars'); -``` - -Then, add your addon name to the *addon_hooks* template variable array: - -```php -function _template_vars($a, &$arr) +function _install() { - if (!array_key_exists('addon_hooks', $arr['vars'])) - { - $arr['vars']['addon_hooks'] = array(); - } - $arr['vars']['addon_hooks'][] = ""; + Addon::registerHook('head', __FILE__, '_head'); + ... +} + + +function _head(App $a) +{ + $a->registerStylesheet(__DIR__ . '/relative/path/to/addon/stylesheet.css'); } ``` -### JavaScript part +## JavaScript -Register your addon hooks in file `addon/*addon_name*/*addon_name*.js`. +### Global scripts + +If your addon requires adding a script on all pages of Friendica, add the following hook: + + +```php +function _install() +{ + Addon::registerHook('footer', __FILE__, '_footer'); + ... +} + +function _footer(App $a) +{ + $a->registerFooterScript(__DIR__ . '/relative/path/to/addon/script.js'); +} +``` + +### 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: ```js document.addEventListener(name, callback); ``` -*name* is the name of the hook and corresponds to a known Friendica JavaScript hook. -*callback* is a JavaScript function to execute. +- *name* is the name of the hook and corresponds to a known Friendica JavaScript hook. +- *callback* is a JavaScript function to execute. + +#### 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 @@ -391,14 +405,9 @@ Hook data: visitor => array with the contact record of the visitor url => the query string -## Current JavaScript hooks - -### postprocess_liveupdate -Called at the end of the live update process (XmlHttpRequest) - ## Complete list of hook callbacks -Here is a complete list of all hook callbacks with file locations (as of 01-Apr-2018). Please see the source for details of any hooks not documented above. +Here is a complete list of all hook callbacks with file locations (as of 24-Sep-2018). Please see the source for details of any hooks not documented above. ### index.php diff --git a/view/templates/head.tpl b/view/templates/head.tpl index a53ae8f747..6c08bc8e45 100644 --- a/view/templates/head.tpl +++ b/view/templates/head.tpl @@ -43,11 +43,6 @@ -{{if is_array($addon_hooks)}} -{{foreach $addon_hooks as $addon_hook}} - -{{/foreach}} -{{/if}} -{{if is_array($addon_hooks)}} -{{foreach $addon_hooks as $addon_hook}} - -{{/foreach}} -{{/if}}