Writing Custom Modules and Formatters (PHP)

Sometimes you want to format or transform your data in a way that is not supported with the supplied formatters included with Wappler. This topic will explain you how you can write your own modules and formatters.

Writing your own Formatters

Location

All formatters are being placed in the extensions/server_connect/formatters folder. A single PHP file can contain multiple formatters and you can have multiple PHP files. Best practice it to group formatters that belong together in a single PHP file. For example you to keep all array specific formatters in a file called array.php.

File Structure

Here a sample of how such a formatter file looks like.

<?php

namespace lib\core;

function formatter_uppercase($str) {
  return strtoupper($str);
}

function formatter_lowercase($str) {
  return strtolower($str);
}

?>

Namespace should always be lib\core. Only functions that start with formatter_ will be available as formatter, all other functions are handled as private functions. Naming a formatter the same as one bundled with Server Connect will override it.

Writing your own Modules

Location

All modules are being placed in the extensions/server_connect/modules folder. The name of the file is also the module name and the methods it exposes are the action names. Best practice is to group actions together under a module and not create a file for each action.

File Structure

<?php

namespace modules;

use \lib\core\Module;

class custom extends Module
{
  public function setvalue($options, $name) {
    return $this->app->parseObject($options->value);
  }
}

?>

Class name and file name must be the same. Naming a module the same as a build-in module will override it. $options will have all passed options and $name is the name of the action step. $this->app->parseObject(...) can be used to parse expressions.

5 Likes