3.3. Templating

3.3.1. javascript_register

Modules can provide JavaScript code that has to be added to the Phorum pages. Modules that make use of this facility should register the JavaScript code using this hook.

Call time:

At the start of the javascript.php script.

Hook input:

An array of registrations. Modules can register their JavaScript code for inclusion by adding a registration to this array. A registration is an array, containing the following fields:

  • module
    The name of the module that adds the registration.
  • source
    Specifies the source of the JavaScript data. This can be one of:
    • file(<path to filename>)
      For including a static JavaScript file. The path should be absolute or relative to the Phorum install directory, e.g. "file(mods/foobar/baz.js)". Because this file is loaded using a PHP include() call, it is possible to include PHP code in this file. Mind that this code is stored interpreted in the cache.
    • template(<template name>)
      For including a Phorum template, e.g. "template(foobar::baz)"
    • function(<function name>)
      For calling a function to retrieve the JavaScript code, e.g. "function(mod_foobar_get_js)"
  • cache_key
    To make caching of the generated JavaScript code possible, the module should provide a cache key using this field. This cache key needs to change if the module will provide different JavaScript code.

    Note: in case "file" or "template" is used as the source, you are allowed to omit the cache_key. In that case, the modification time of the involved file(s) will be used as the cache key.

    It is okay for the module to provide multiple cache keys for different situations (e.g. if the JavaScript code depends on a group). Keep in mind though that for each different cache key, a separate cache file is generated. If you are generating different JavaScript code per user or so, then it might be better to add the JavaScript code differently (e.g. through a custom JavaScript generating script or by adding the code to the $PHORUM['DATA']['HEAD_DATA'] variable). Also, do not use this to only add JavaScript code to certain phorum pages. Since the resulting JavaScript data is cached, it is no problem if you add the JavaScript code for your module to the code for every page.

Hook output:

The same array as the one that was used as the hook call argument, possibly extended with one or more registrations.