collectiveaccess / providence

Cataloguing and data/media management application
GNU General Public License v3.0
290 stars 167 forks source link

Add ability for plugins to define custom expressions … #1449

Closed bri-gaia closed 1 year ago

bri-gaia commented 1 year ago

…for use in display templates.

kehh commented 1 year ago

Very similar code to this has been in use in production for the past couple of years. Similar to #1450 this moves this functionality into a plugin. The plugin can then register custom functions that can be called:

    public function hookRegisterExpressions($pa_options) {
        $fancy_methods = [
            'fancyfyTheText',
        ];
        foreach ($fancy_methods as $method) {
            $custom = function(...$args) use ($method) {
                if (is_callable("FancyClass::{$method}")){
                    $ret = fancyClass::$method(...$args);
                } else {
                    throw new Exception("Method FancyClass::$method does not exist and cannot be called.");
                }
                return $ret;
            };
            $pa_options['expressions']['FancyText' . $method] = xcallable($custom);
        }

        return $pa_options;
    }